【发布时间】:2023-01-05 16:27:34
【问题描述】:
到目前为止,IBKR 给我的结果非常不一致,我希望这只是因为我不明白某些事情。
这是我的代码,用于尝试获取我的帐户头寸,但它仅在我第一次运行时有效,并且不再有效。我发现我尝试在 IBKR 上做的一半事情都是这样的……有没有更简单的 API 不需要我实例化类来简单地获取我账户中的头寸列表?多谢你们。
def read_positions(): #读取所有账户头寸并返回 带有信息的DataFrame
from ibapi.client import EClient from ibapi.wrapper import EWrapper from ibapi.common import TickerId import pandas as pd class ib_class(EWrapper, EClient): def __init__(self): EClient.__init__(self, self) self.all_positions = pd.DataFrame([], columns = ['Account','Symbol', 'Quantity', 'Average Cost']) def position(self, account, contract, pos, avgCost): index = str(account)+str(contract.symbol) self.all_positions.loc[index]=account,contract.symbol,pos,avgCost def error(self, reqId:TickerId, errorCode:int, errorString:str): if reqId > -1: print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString) def positionEnd(self): super().positionEnd() self.disconnect() ib_api = ib_class() ib_api.connect("127.0.0.1", 7496, 0) ib_api.reqPositions() current_positions = ib_api.all_positions ib_api.run() return(current_positions)
【问题讨论】:
-
您可以分享 IBKR api 文档链接吗?
-
您的代码按预期工作。如果它只工作一次,那么您要么没有正确断开连接,要么与网关的其他连接仍然连接。您收到的错误是什么? IB API 本身非常一致,这种问题并不常见。
标签: python algorithmic-trading interactive-brokers