Creating an Account¶
Setting up environment¶
- Install async_v20 as per installation
- Create a new environment variable with the name OANDA_TOKEN and value as the token generated from Creating an Account.
Lets first take a look at this code example, then go though it line by line.
import asyncio from async_v20 import OandaClient async def get_account(): async with OandaClient() as client: return await client.account() loop = asyncio.get_event_loop() account = loop.run_until_complete(get_account()) # pandas Series print(account.series())
We then import OandaClient which provides us the means to interact with OANDA
from async_v20 import OandaClient
Because OandaClient returns coroutines we use async def. This allows the use of the await syntax
async def get_account():
OandaClient is a context manager, we use async with to instantiate a client instance. Doing so will automatically close the http session when we’re done
async with OandaClient() as client:
return await client.account()
Now we have defined our coroutine we need to execute it. To do so we need an event loop. Achieved using asyncio. get_event_loop()
loop = asyncio.get_event_loop()
The value returned by executing the account coroutine is accessed through the event loop.
account = loop.run_until_complete(get_account())
Each application should only instantiate ONE OandaClient instance per account. See Best Practices.