Python-如何提交并发API请求以进行交易目的

发布于 2025-01-27 14:12:59 字数 1789 浏览 7 评论 0原文

因此,我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫南 2025-02-03 14:12:59

在下面,您将看到一个示例,说明如何同时发送请求。
请注意,此代码尚未完成。
acc_num令牌缺少,因为它们在您的问题中未指定。这应该给您一个想法,并且不需要太多来满足您的需求。

import asyncio
import aiohttp


async def post(session, token, acc_num, instrument):
    headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer " + token
    }
    data = {
        "order": {
            "units": 1,
            "instrument": instrument,
            "timeInForce": "FOK",
            "type": "MARKET",
            "positionFill": "DEFAULT"
        }
    }
    async with session.post(f"https://api-fxpractice.oanda.com/v3/accounts/{acc_num}/orders") as response:
        return await response.text()


async def pre_post(instruments):
    async with aiohttp.ClientSession() as session:
        results = await asyncio.gather(*[fetch(session, token, acc_num, 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)

Below you'll see an example of how you can send the requests concurrently.
Do note that this code isn't complete.
acc_num and token 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.

import asyncio
import aiohttp


async def post(session, token, acc_num, instrument):
    headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer " + token
    }
    data = {
        "order": {
            "units": 1,
            "instrument": instrument,
            "timeInForce": "FOK",
            "type": "MARKET",
            "positionFill": "DEFAULT"
        }
    }
    async with session.post(f"https://api-fxpractice.oanda.com/v3/accounts/{acc_num}/orders") as response:
        return await response.text()


async def pre_post(instruments):
    async with aiohttp.ClientSession() as session:
        results = await asyncio.gather(*[fetch(session, token, acc_num, 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)

Borrowed some code from https://stackoverflow.com/a/51728016/18777481.

征棹 2025-02-03 14:12:59

多亏了Kevinssadev,我设法修复了它。这是完整的解决方案

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",data=json.dumps(data)) 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)

Thanks to KevinssaDev, I manage to fix it. Here is the complete solution

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",data=json.dumps(data)) 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文