0

我是 ibapi 新手并使用库“ https://pypi.org/project/ibapi/ ”。我想下载图片中红框内的所有订单详情。

在此处输入图像描述

代码是

from ibapi.client import EClient 
from ibapi.wrapper import EWrapper 
from ibapi.contract import Contract 

class TestApp(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self) 

    def error(self, reqId, errorCode, errorString):
        print("Error: ", reqId, " ", errorCode, " ", errorString)

    def contractDetails(self, reqId, contractDetails):
        print("contractDetails: ", reqId, " ", contractDetails) 

    def openOrder(self):
        super().openOrder()
        print("OpenOrder. PermId: ", order.permId, "ClientId:", order.clientId, " OrderId:", orderId, "Account:", order.account, "Symbol:", contract.symbol, "SecType:", contract.secType,"Exchange:", contract.exchange, "Action:", order.action, "OrderType:", order.orderType,"TotalQty:", order.totalQuantity, "CashQty:", order.cashQty, "LmtPrice:", order.lmtPrice, "AuxPrice:", order.auxPrice, "Status:", orderState.status)
        order.contract = contract
        self.permId2ord[order.permId] = order



def main():
    app = TestApp() 
    app.connect("127.0.0.1", 1111, 0)
    app.reqAllOpenOrders()
    app.run() 
    app.disconnect()


main()

如果您有,请帮助我找出错误或分享一些完全不同的方法。

4

1 回答 1

1

openOrder 函数的参数不正确,因此它可能不会覆盖 EWrapper 函数并且不会收到回调。它应该是:

 def openOrder(self, orderId, contract, order, orderState): 

(如果openOrder(self)在您的示例中曾经调用过它会引发错误,因为order.contract未定义)

其他问题:

  • 在 EClient 中连接和调用任何其他函数之间应该有一个暂停(最好等待 nextValidID 回调)
  • run() 循环是无限的,所以你程序中的 disconnect() 永远不会被调用。

您可能会对IBKR 网站上交易者学院的 Python API 课程感兴趣。

于 2020-04-01T21:29:00.127 回答