IBpy 无法取消手动下达的未结订单

IBpy cannot cancel open orders placed manually

当使用 ibpy 尝试手动关闭 TWS 上的一些头寸时,我们无法做到这一点。具体来说,执行时:

    self._tws.reqAllOpenOrders()
    sleep(0.2)

我们得到了 id 为 0 的订单(可能是因为我在 TWS 上手动放置了它)

<openOrder orderId=0, contract=<ib.ext.Contract.Contract object at 0x103b78ad0>, order=<ib.ext.Order.Order object at 0x103b78a50>, orderState=<ib.ext.OrderState.OrderState object at 0x103b78b10>>
<orderStatus orderId=0, status=Submitted, filled=0, remaining=100, avgFillPrice=0.0, permId=134994568, parentId=0, lastFillPrice=0.0, clientId=0, whyHeld=None>
<openOrder orderId=0, contract=<ib.ext.Contract.Contract object at 0x103b78c90>, order=<ib.ext.Order.Order object at 0x103b78c50>, orderState=<ib.ext.OrderState.OrderState object at 0x103b78cd0>>
<orderStatus orderId=0, status=Submitted, filled=0, remaining=1, avgFillPrice=0.0, permId=134994562, parentId=0, lastFillPrice=0.0, clientId=0, whyHeld=None>
<openOrderEnd>

当试图关闭它时:

    self._tws.cancelOrder(0)

    self._tws.cancelOrder(134994568)

我收到错误:

<error id=0, errorCode=135, errorMsg=Can't find order with id =0>
<error id=134994562, errorCode=135, errorMsg=Can't find order with id =134994562>

知道如何关闭它们吗?谢谢。

您必须'bind'将订单发送给新客户。使用此方法 tws.reqAutoOpenOrders(True)。从文档中,

Finally, IBApi.EClient.reqAutoOpenOrders will allow to obtain those orders manually placed using the TWS itself. This method also allows the client application to take over these orders and modify them by setting the autoBind parameter to true. If successfully bound, The orders will be assigned (i.e. bound to) an API order id and as such be eligible for modification.

client.reqAutoOpenOrders(true); Important: only those applications connecting with client Id 0 will be able to take over manually submitted orders

Through the TWS' API settings it is possible to configure this method's behaviour to some extent. As shown in the image below, manually placed orders can be given a negative order Id which can serve to easily tell manual from API submitted orders. The TWS' tooltip elaborates further:

回调看起来像 <openOrder orderId=-3,...,然后您只需调用 tws.cancelOrder(-3)

请注意,您不会收到之前在 TWS 中下达的订单,只会收到在调用 reqAutoOpenOrders.

之后下达的订单