So I have a list of instruments in a csv that looks like this:
EUR_HUF
EUR_DKK
USD_MXN
GBP_USD
CAD_CHF
EUR_GBP
GBP_CHF
...
To submit an order on an individual instrument is done by doing this api request:
import asyncio
import aiohttp
import logging
import json
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer 83da58ee4d8b630115ba04368ed6916c-db53699ac5f596bfd495b835571ed878"
}
async def post(session,instrument):
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer 83da58ee4d8b630115ba04368ed6916c-db53699ac5f596bfd495b835571ed878"
},
data = {
"order": {
"units": 1,
"instrument": instrument,
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT"
}
}
async with session.post(f"https://api-fxpractice.oanda.com/v3/accounts/101-004-22347481-001/orders") as response:
return await response.text()
async def pre_post(instruments):
data = {
"order": {
"units": 1,
"instrument": instruments,
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT"
}
}
async with aiohttp.ClientSession(headers=headers) as session:
results = await asyncio.gather(*[post(session,instrument) for instrument in instruments])
return results
if __name__ == '__main__':
loop = asyncio.get_event_loop()
instruments = [ "EUR_HUF",
# "EUR_DKK",
# "USD_MXN",
# "GBP_USD",
# "CAD_CHF",
# "EUR_GBP",
# "GBP_CHF"
]
response = loop.run_until_complete(pre_post(instruments))
print(response)
Now I want to submit an order concurrently for all instruments in my instrument csv. I have looked into threading and AIOhttp but I am not sure how to apply it to my scenario.
Any guidance or example would be appreciated
Thanks
Below you'll see an example of how you can send the requests concurrently. Do note that this code isn't complete.
acc_numandtokenare missing since they weren't specified in your question. This should give you an idea and won't require much to modify for your needs.Borrowed some code from https://stackoverflow.com/a/51728016/18777481.