如何使用Web API实施简单的IBKR买卖
我目前正在Python开发一个简单的IBKR交易机器人。
我希望该机器人每天进行这些操作:
- 出售我所有的股票(当市场开放
- )
时 't有一个帐户,但是为了最大程度地减少我在IBKR平台上的不活动时间,我想知道如何事先实施我想要的东西。
从我有限的理解中,我拥有的是:
def main():
sell_stock(previous_day_stock)
buy_stock(daily_recommended_stock)
def get_available_money():
return requests.get(f"https://localhost:5000/v1/api/portfolio/{account_id}/summary").json()["availablefunds"]["amount"]
def get_owned_stocks_value():
return requests.get(f"https://localhost:5000/v1/api/portfolio/{account_id}/positions/").json()["avgPrice"]
def buy_stock(symbol):
account_id = 1234
payload = {
"conid": get_contract_id(symbol),
"secType": "STK",
"orderType": "MKT",
"cashQty": get_available_money(),
"side": "BUY",
"tif": "DAY"
}
return requests.post(f"https://localhost:5000/v1/api/iserver/account/{account_id}/orders", payload)
def sell_stock(symbol):
account_id = 1234
payload = {
"conid": get_contract_id(symbol),
"secType": "STK",
"orderType": "MKT",
"cashQty": get_owned_stocks_value(),
"side": "SELL",
"tif": "DAY"
}
return requests.post(f"https://localhost:5000/v1/api/iserver/account/{account_id}/orders", payload)
def get_contract_id(symbol):
return requests.get(f"https://localhost:5000/v1/api/trsrv/stocks/symbols={symbol}").json()["contracts"]["conid"]
我觉得自己的操作方式是不正确的,而且我无法访问API来了解我应该做什么,并且我的代码甚至有意义。我很想知道做我想做的正确的方法。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您可以无需拥有IB帐户即可访问IBAPI库 - 它只是从PYPI下载的Python库()或互动经纪人()。您应该这样做,因为它提供了与API/TWS交互所需的所有对象,函数和回调。
您概述的任务将需要进行大量的编程工作,但是在粗略的轮廓中,您需要的步骤是:
reqacccountupdates
或reqpositions
位置接收位置信息
合同
<代码>订单对象放置您的出口为每个位置订购。plotorder
reqopenorder
和openordorder
回调updateAccountValue
回调合同
和订单
inploteorder
所有这些详细信息的详细信息此处的API文档 https://interactivebrokers.github.github.io/tws-api/
应仔细注意,即使对这组简单的程序进行编程也将是一项非平凡的练习,而且您编程的越多,您就会发现您从未想到的事情就越需要额外的时间/编程。
我不确定这是在设置帐户之前尝试对所有编码进行编码的正确方法。只有在与TWS一起使用代码时,您才会发现大多数问题。
Firstly, you can access the ibapi library without having an IB account - it's just a python library downloaded from pypi (https://pypi.org/project/ibapi/) or interactive brokers (https://interactivebrokers.github.io). You should do this as it provides all the objects, functions and callbacks you will need to interact with the API/TWS.
The tasks you outline will require quite a bit of work to program, but in rough outline the steps you will need are:
reqAccountUpdates
orreqPositions
position
callbackContract
and anOrder
object to place your exit order for each position.placeOrder
reqopenOrder
andopenOrder
callbackupdateAccountValue
callbackContract
andOrder
objects inplaceOrder
Details on all of these are available in the API documentation here https://interactivebrokers.github.io/tws-api/
You should note carefully that it will very much be a non-trivial exercise to program even this simple set of procedures, and the more you program the more you will discover things you hadn't thought of that will require additional time/programming.
I'm not sure it's the right approach to try to code it all before setting up an account. You will only discover most of the issues when you start using your code with the TWS.