toloco / pyoanda

Oanda’s API python wrapper. Robust and Fast API wrapper for your Forex bot Python library that wraps Oanda API. Built on top of requests, it’s easy to use and makes sense.
http://oanda.com
MIT License
65 stars 24 forks source link

Implement support for account creation and retrieval. #13

Closed elyobo closed 9 years ago

elyobo commented 9 years ago

The Oanda sandbox environment doesn't support users' normal account ID, so in order to work in the sandbox we need to support the account creation API.

This change adds create_account (only useful in the sandbox) and get_accounts (useful in all environments).

This can be used to work in the sandbox as follows.

from pyoanda import Client, SANDBOX

# Create a client without credentials
c = Client(SANDBOX, None, None)

# Create a new account, which gives you a user ID; may want to save this
# for future runs!
user = c.create_account()

# Set the account ID for the client, as many methods require it
c.account_id = user['accountId']

# Now you're good to go with other methods, e.g.
orders = c.get_orders()
elyobo commented 9 years ago

@toloco It might be an idea to change the main README.md as well, as the instructions given don't work for the sandbox environment. The account ID that you get from Oanda doesn't seem to exist in the sandbox environment, so it's necessary to create an account using the API and then use that account ID.

I can update the PR to include README.md changes as well to mention the process for the sandbox if you like.

elyobo commented 9 years ago

Updated the readme.

toloco commented 9 years ago

Good one!

But rather to add another parameter for the environ I prefer to check if account_id and access_token are None to not check the credentials. Looks a bit cleaner to me.

What do you think?

elyobo commented 9 years ago

Makes sense. I've stripped out the extra parameter, changed the account check in init to only run if an account ID is provided (as this does work in the sandbox as well, once you've created a valid account) and changed the session stabiliser to only add the header if an access token was provided.

Tests likewise corrected and the commits squashed down to one for merging.