Best Practices¶
One OandaClient per application¶
Using async_v20 example, used OandaClient as a context manager. This is correct when the client does not need to be shared between multiple coroutines.
This is a an example of multiple coroutines using the One OandaClient instance:
import asyncio
from async_v20 import OandaClient
client = OandaClient()
async def poll_account(poll_interval=6, client=client):
while True:
account = await client.account()
print(account)
await asyncio.sleep(poll_interval)
async def stream(instruments, client=client):
async for price in await client.stream_pricing(instruments):
print(price)
loop = asyncio.get_event_loop()
loop.run_until_complete(
asyncio.gather(poll_account(), stream('EUR_USD'))
)
client.close()
Initialize first¶
OandaClient requires initialization. The initialization procedure can delay execution of OandaClient. methods
If this is a concern for you, it is recommended to preemptively initialize the OandaClient instance.
import asyncio
from async_v20 import OandaClient
client = OandaClient()
loop = asyncio.get_event_loop()
response = loop.run_until_complete(client.initialize())
# Write your code here
loop.run_until_complete(client.close())
Check Services¶
It is encouraged to check the service you wish to consume is available. See Health API.