Python-如何提交并发API请求以进行交易目的
因此,我在CSV中有一系列看起来像这样的工具的清单:
EUR_HUF
EUR_DKK
USD_MXN
GBP_USD
CAD_CHF
EUR_GBP
GBP_CHF
...
通过执行此API请求,可以在单个工具上提交订单:
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)
现在我想同时提交仪器CSV中所有工具的订单。我已经研究了螺纹和AIOHTTP,但我不确定如何将其应用于我的情况。
任何指导或示例都将不胜
感激
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在下面,您将看到一个示例,说明如何同时发送请求。
请注意,此代码尚未完成。
acc_num
和令牌
缺少,因为它们在您的问题中未指定。这应该给您一个想法,并且不需要太多来满足您的需求。从
Below you'll see an example of how you can send the requests concurrently.
Do note that this code isn't complete.
acc_num
andtoken
are 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.
多亏了Kevinssadev,我设法修复了它。这是完整的解决方案
Thanks to KevinssaDev, I manage to fix it. Here is the complete solution