QuantConnect / Lean.Brokerages.InteractiveBrokers

InteractiveBrokers Brokerage Plugin
Apache License 2.0
27 stars 17 forks source link

Update Broker Id of Combo Limit Order #66

Closed AlexCatarino closed 6 months ago

AlexCatarino commented 1 year ago

Expected Behavior

Be able to update the order tickets generated by combo limit order.

Actual Behavior

We cannot update order tickets generated by combo limit orders since we don't have their broker ids.

Potential Solution

N/A

Reproducing the Problem

Place Combo Limit Order, then try to update the tickets.

var openOrderTickets = Transactions.GetOpenOrderTickets();

foreach (var ticket in openOrderTickets) {
     var direction = Math.Sign(ticket.Quantity);

     var midpoint = (slice.QuoteBars[ticket.Symbol].Ask.Close + slice.QuoteBars[ticket.Symbol].Bid.Close)/2;
     var limitResponse = ticket.UpdateLimitPrice(midpoint * direction);

The tickets will not be updated.

Checklist

AlexCatarino commented 1 year ago

In a particular case, the combo order was placed in the previous deployment, so the algorithm loads it:

2023-07-10T19:27:28.6927106Z TRACE:: InteractiveBrokersBrokerage.HandleOpenOrder(): OrderId: -2, Contract: BAG SPX USD SMART, OrderStatus: Submitted

Note -2 as OrderId. Lean created two tickets:

2023-07-10T19:27:29.1093764Z TRACE:: BrokerageSetupHandler.Setup(): Has open order: OrderId: 1 (BrokerId: -2) None ComboLimit order for -1 units of SPXW YA03PSXE5JXQ|SPX 31

2023-07-10T19:27:29.1195465Z TRACE:: BrokerageSetupHandler.Setup(): Has open order: OrderId: 2 (BrokerId: -2) None ComboLimit order for 1 unit of SPXW YA03PUR1QADQ|SPX 31

with -2 as BrokerId.

We can see the logs for the update request:

2023-07-10T19:27:39.1979780Z TRACE:: InteractiveBrokersBrokerage.UpdateOrder(): Symbol: SPXW 230710C04400000 Quantity: -1 Status: None Id: 1

2023-07-10T19:27:39.2137833Z TRACE:: InteractiveBrokersBrokerage.UpdateOrder(): Symbol: SPXW 230710C04410000 Quantity: 1 Status: None Id: 2

but we don't get a response. Probably because the BokerId is invalid.

AlexCatarino commented 12 months ago

Second report. In this case, we get an error 105:

2023-07-11T16:23:10.8375173Z TRACE:: InteractiveBrokersBrokerage.HandleError(): RequestId: 3448 ErrorCode: 105 - Order being modified does not match original order.. Origin: [Id=3448] IBPlaceOrder: GOOG 230721C00119000 (BAG GOOG USD Smart 20230721 118 C )

2023-07-11T16:23:10.8385340Z TRACE:: InteractiveBrokersBrokerage.HandleError.InvalidateOrder(): IBOrderId: 3448 ErrorCode: 105 - Order being modified does not match original order.. Origin: [Id=3448] IBPlaceOrder: GOOG 230721C00119000 (BAG GOOG USD Smart 20230721 118 C )

2023-07-11T16:23:10.8404499Z TRACE:: LiveTradingResultHandler.OrderEvent(): Time: 07/11/2023 16:23:10 OrderID: 1 EventID: 2 Symbol: GOOG 230721C00118000 Status: Invalid Quantity: -1 Message: 105 - Order being modified does not match original order.. Origin: [Id=3448] IBPlaceOrder: GOOG 230721C00119000 (BAG GOOG USD Smart 20230721 118 C ) IsAssignment: False BrokerId: 3448

2023-07-11T16:23:10.8411418Z TRACE:: LiveTradingResultHandler.OrderEvent(): Time: 07/11/2023 16:23:10 OrderID: 2 EventID: 2 Symbol: GOOG 230721C00119000 Status: Invalid Quantity: 1 Message: 105 - Order being modified does not match original order.. Origin: [Id=3448] IBPlaceOrder: GOOG 230721C00119000 (BAG GOOG USD Smart 20230721 118 C ) IsAssignment: False BrokerId: 3448

2023-07-11T16:23:10.8418935Z TRACE:: Brokerage.OnMessage(): Warning - Code: 105 - Order being modified does not match original order.. Origin: [Id=3448] IBPlaceOrder: GOOG 230721C00119000 (BAG GOOG USD Smart 20230721 118 C )

2023-07-11T16:23:11.1472538Z TRACE:: Debug: New Order Event: Time: 07/11/2023 16:23:10 OrderID: 1 EventID: 2 Symbol: GOOG 230721C00118000 Status: Invalid Quantity: -1 Message: 105 - Order being modified does not match original order.. Origin: [Id=3448] IBPlaceOrder: GOOG 230721C00119000 (BAG GOOG USD Smart 20230721 118 C ) IsAssignment: False

2023-07-11T16:23:11.1476719Z TRACE:: Debug: New Order Event: Time: 07/11/2023 16:23:10 OrderID: 2 EventID: 2 Symbol: GOOG 230721C00119000 Status: Invalid Quantity: 1 Message: 105 - Order being modified does not match original order.. Origin: [Id=3448] IBPlaceOrder: GOOG 230721C00119000 (BAG GOOG USD Smart 20230721 118 C ) IsAssignment: False

2023-07-11T16:23:11.1484004Z ERROR:: Brokerage Warning: Order being modified does not match original order.. Origin: [Id=3448] IBPlaceOrder: GOOG 230721C00119000 (BAG GOOG USD Smart 20230721 118 C )

Martin-Molinero commented 10 months ago

Update: this bug was raised to IB updating combo orders is failing at the moment