erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.81k stars 753 forks source link

Cannot modify bracket order - can’t find order with orderId #647

Closed Blair129 closed 8 months ago

Blair129 commented 12 months ago

Hi, I'm unable to modify existing orders (take profit/ stop loss in a bracket order) due to an error stating 'can’t find order with orderId = xx'. This xx refers to a parent order that was previously filled. It didn't happen everytime, in some cases it works straightaway. Here is my code snippet. Any clue of this? parent_trd = ib.placeOrder(contract, bracket_order.parent) tp_trd = ib.placeOrder(contract, bracket_order.takeProfit) sl_trd = ib.placeOrder(contract, bracket_order.stopLoss) After reach a certain price sl_trd.order.auxPrice = new_price sl_trd = ib.placeOrder(contract, sl_trd.order)

09/24/2023 23:29:47 placeOrder: Modify order Trade(contract=Future(conId=495512552, symbol='ES', lastTradeDateOrContractMonth='20231215', multiplier='50', exchange='CME', currency='USD', localSymbol='ESZ3', tradingClass='ES'), order=StopOrder(orderId=22, clientId=1, permId=154097116, action='SELL', totalQuantity=1.0, lmtPrice=0.0, auxPrice=4370.0, parentId=20, adjustedOrderType='STP', triggerPrice=4370.25, adjustedStopPrice=4370.0), orderStatus=OrderStatus(orderId=22, status='PreSubmitted', filled=0.0, remaining=1.0, avgFillPrice=0.0, permId=154097116, parentId=20, lastFillPrice=0.0, clientId=1, whyHeld='trigger', mktCapPrice=0.0), fills=[], log=[TradeLogEntry(time=datetime.datetime(2023, 9, 24, 22, 29, 2, 362195, tzinfo=datetime.timezone.utc), status='PendingSubmit', message='', errorCode=0), TradeLogEntry(time=datetime.datetime(2023, 9, 24, 22, 29, 2, 908207, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='', errorCode=0), TradeLogEntry(time=datetime.datetime(2023, 9, 24, 22, 29, 3, 152297, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='', errorCode=0), TradeLogEntry(time=datetime.datetime(2023, 9, 24, 22, 29, 47, 775994, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='Modify', errorCode=0)], advancedError='') 09/24/2023 23:29:47 Error 135, reqId 22: Can't find order with id = 20 09/24/2023 23:29:47 Canceled order: Trade(contract=Future(conId=495512552, symbol='ES', lastTradeDateOrContractMonth='20231215', multiplier='50', exchange='CME', currency='USD', localSymbol='ESZ3', tradingClass='ES'), order=StopOrder(orderId=22, clientId=1, permId=154097116, action='SELL', totalQuantity=1.0, lmtPrice=0.0, auxPrice=4370.0, parentId=20, adjustedOrderType='STP', triggerPrice=4370.25, adjustedStopPrice=4370.0), orderStatus=OrderStatus(orderId=22, status='Cancelled', filled=0.0, remaining=1.0, avgFillPrice=0.0, permId=154097116, parentId=20, lastFillPrice=0.0, clientId=1, whyHeld='trigger', mktCapPrice=0.0), fills=[], log=[TradeLogEntry(time=datetime.datetime(2023, 9, 24, 22, 29, 2, 362195, tzinfo=datetime.timezone.utc), status='PendingSubmit', message='', errorCode=0), TradeLogEntry(time=datetime.datetime(2023, 9, 24, 22, 29, 2, 908207, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='', errorCode=0), TradeLogEntry(time=datetime.datetime(2023, 9, 24, 22, 29, 3, 152297, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='', errorCode=0), TradeLogEntry(time=datetime.datetime(2023, 9, 24, 22, 29, 47, 775994, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='Modify', errorCode=0), TradeLogEntry(time=datetime.datetime(2023, 9, 24, 22, 29, 47, 777992, tzinfo=datetime.timezone.utc), status='Cancelled', message="Error 135, reqId 22: Can't find order with id = 20", errorCode=135)], advancedError='')

nuxy69 commented 11 months ago

Exactly the same issue here! I have a similar setup for a bracket order, like so:

    parent = LimitOrder(direction, buy_amount, buyprice)
    parent.algoStrategy = 'Adaptive'
    parent.algoParams = listTagValues
    parent.orderId = ib.client.getReqId()
    parent.clientId = client
    parent.outsideRth = True
    parent.transmit = False

    takeProfit = LimitOrder(oppositeDirection, buy_amount, profittakePrice)
    takeProfit.orderId = parent.orderId + 1
    takeProfit.parentId = parent.orderId
    takeProfit.clientId = client
    takeProfit.transmit = False

    stopLoss = StopOrder(oppositeDirection, buy_amount, stoplossPrice)
    stopLoss.orderId = parent.orderId + 2
    stopLoss.parentId = parent.orderId
    stopLoss.clientId = client
    stopLoss.outsideRth = True
    stopLoss.transmit = True

    bracketOrder = [parent, takeProfit, stopLoss]

    es_runningStopLossorder = stopLoss

    for o in bracketOrder:
        boughtOrder = ib.placeOrder(contract, o)
        ib.sleep(0)
        boughtOrders.append(boughtOrder)

Then when I try to update the SL part of this bracket:

        es_runningStopLossorder.auxPrice = newStop

        ib.placeOrder(es_contract, es_runningStopLossorder)  

I get the same error as described above. That it cant find the order with the ID of the parent.

This might be only for futures, as I think it works for options (needs retest).

nuxy69 commented 11 months ago

As an added note, this seems to be linked to the client (version)? I have tried my python app with the TWS Gateway and it didn't throw this error yet. Might be something in TWS 10.25?

FarzanT commented 10 months ago

I have the same issue, and I'm using the TWS Gateway version 10.19. @nuxy69 @Blair129 were you able to fix reliably fix this issue on your end?

nuxy69 commented 10 months ago

I have the same issue, and I'm using the TWS Gateway version 10.19. @nuxy69 @Blair129 were you able to fix reliably fix this issue on your end?

Sadly no. Haven't found a fix yet...

FarzanT commented 10 months ago

Ok, would you try submitting a bracket order during market hours while ensuring that all prices are rounded to 2 significant digits? You can also turn on the API logging in the TWS Gateway. It said in the log that I have to enter a valid number. I was entering floating point numbers for the order prices with many significant digits, e.g. 43.1295343. Funny enough, 0 doesn't work either, minimum is 0.01 which is 1 cent. Hope it helps!

nuxy69 commented 10 months ago

Ok, would you try submitting a bracket order during market hours while ensuring that all prices are rounded to 2 significant digits? You can also turn on the API logging in the TWS Gateway. It said in the log that I have to enter a valid number. I was entering floating point numbers for the order prices with many significant digits, e.g. 43.1295343. Funny enough, 0 doesn't work either, minimum is 0.01 which is 1 cent. Hope it helps!

That didn't do it sadly. I rounded everything off to .25 increments, as those are the ticks on ES and NQ. So all my prices for the bracket and adjusted stoploss too. Can't check logs as I was using the TWS Workstation. Will try again with Gateway, to see if the logs give me any extra information.

Edit: The gateway (still) works without troubles. As described above. (I don't have this issue with the gateway, only workstation)

erdewit commented 8 months ago

The log indicates that an order modification is attempted on an order that has already been canceled.

nuxy69 commented 8 months ago

The log indicates that an order modification is attempted on an order that has already been canceled.

Sorry for re-opening. But that is the whole point. The order is not cancelled. It's the running StopLoss order that we're trying to change. But in doing so, we get that it can't find the parent (which we are not even touching) and then it says the StopLoss order is cancelled, which it is not.

Especially the part "Error 135, reqId 22: Can't find order with id = 20" is weird, because there is no action involving this OrderId.

tridolphin commented 7 months ago

Same issue. Has anyone solved this?

tridolphin commented 7 months ago

I fixed it by setting the parentID of the modification order to None:

stopLoss.parentId=None

nuxy69 commented 7 months ago

I fixed it by setting the parentID of the modification order to None:

stopLoss.parentId=None

Thank you! This solved it for me as well!