When using ibpy trying to close some positions placed on TWS manually, it turns our we could not do this. Specifically, when performing:
self._tws.reqAllOpenOrders()
sleep(0.2)
We get the orders with id 0 (probably because i placecd it manually on 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>
When trying to close it with:
self._tws.cancelOrder(0)
or
self._tws.cancelOrder(134994568)
I get the error:
<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>
Any idea how could we close them? Thank you.
You have to 'bind' the orders to the new client. Use this method
tws.reqAutoOpenOrders(True)
. From the docs,The callback will look like
<openOrder orderId=-3,...
and then you just calltws.cancelOrder(-3)
Note that you don't get orders placed previously in TWS, only those placed after the call to
reqAutoOpenOrders
.