【问题标题】:Requesting nextOrderID in TWS API not working在 TWS API 中请求 nextOrderID 不起作用
【发布时间】:2021-05-26 15:01:45
【问题描述】:

我正在尝试使用 python 通过 TWS API 下订单。我的问题是获取下一个有效的订单 ID。

这是我正在使用的:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import TickerId
from ibapi import contract, order, common
from threading import Thread

class ib_class(EWrapper, EClient):

    def __init__(self, addr, port, client_id):
        EClient.__init__(self, self)
        self.connect(addr, port, client_id) # Connect to TWS
        thread = Thread(target=self.run, daemon=True)  # Launch the client thread
        thread.start()

    def error(self, reqId:TickerId, errorCode:int, errorString:str):
        if reqId > -1:
            print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)

    def nextValidId(self, orderId: int):
        self.nextValidId = orderId

ib_api = ib_class("127.0.0.1", 7496, 1)

orderID = ib_api.nextValidId(0)

这给了我:

TypeError: 'int' object is not callable

【问题讨论】:

    标签: python api interactive-brokers


    【解决方案1】:

    nextValidId 方法是一个包装器方法。在客户端,您需要致电reqIds 以获取订单 ID。

    ib_api.reqIds(-1)
    

    reqIds 的参数无关紧要。此外,它没有返回值。相反,reqIds 向 IB 发送消息,当收到响应时,将调用包装器的 nextValidId 方法。

    【讨论】:

      【解决方案2】:

      reqIds 之后添加一些延迟

      在用于接收来自ibapi 的更新的包装类中, 覆盖 nextValidId 函数,因为 ib_api.reqIds 使用它来响应。

      from ibapi.wrapper import iswrapper
      
      @iswrapper
      def nextValidId(self, orderId: int):
          super().nextValidId(orderId)
          print("setting nextValidOrderId: %d" % orderId)
          self.nextValidOrderId = orderId
      

      然后,在调用reqIds 之后添加一些延迟,因为TWS Api 可能需要一些时间来响应并通过ib_api.nextValidOrderId 访问它

      ib_api.reqIds(ib_api.nextValidOrderId)
      time.sleep(3)
      print("Next Order ID: ", ib_api.nextValidOrderId)
      

      注意:ib_api 持有上面wrapper class 的对象

      【讨论】:

        猜你喜欢
        • 2021-08-12
        • 1970-01-01
        • 2019-12-05
        • 1970-01-01
        • 2016-07-27
        • 2016-09-21
        • 1970-01-01
        • 1970-01-01
        • 2016-08-22
        相关资源
        最近更新 更多