Lean.Brokerages.InteractiveBrokers icon indicating copy to clipboard operation
Lean.Brokerages.InteractiveBrokers copied to clipboard

Try to Reconcile State

Open AlexCatarino opened this issue 11 months ago • 0 comments

Expected Behavior

The algorithm state matches the IB portfolio,

Actual Behavior

We have observed the following case:

No positions at 5:21Z:

2024-11-26T05:21:37.3783447Z TRACE:: InteractiveBrokersBrokerage.HandlePortfolioUpdates(): Contract: FUT MNQ USD , ConId: 654503320, Position: 0, MarketPrice: 20873.5, MarketValue: 0, AverageCost: 0, UnrealisedPnl: 0, RealisedPnl: 0, AccountName: U8888888

Sell 1 unit at 14:30:00Z

2024-11-27T14:30:00.6864126Z TRACE:: BrokerageTransactionHandler.Process(): 11/27/2024 14:30:00 UTC: Submit Order: (51) - OrderId: 51 (BrokerId: ) New Market order for -1 units of MNQ YOGVNNAOI1OH Status: Unprocessed

Rejected by IB at 14:30:01Z

2024-11-27T14:30:01.0118024Z TRACE:: LiveTradingResultHandler.OrderEvent(): Time: 11/27/2024 14:30:01 OrderID: 51 EventID: 2 Symbol: MNQ20Z24 Status: Invalid Quantity: -1 Message: 201 - Order rejected - reason:The Available Funds in your Commodities segment are insufficient to cover the change in the margin requirements in your Commodities segment should this order execute. In order to obtain the desired position, your Commodities Net Liquidation Value [2673.60 USD] must exceed the new total initial Margin of [4348.07 USD].. Origin: [Id=52] IBPlaceOrder: MNQ20Z24 (FUT MNQ USD CME 20241220 0 ) BrokerId: 52

By then, IB tell us there is an open short position.

2024-11-27T14:30:01.5744480Z TRACE:: InteractiveBrokersBrokerage.HandlePortfolioUpdates(): Contract: FUT MNQ USD , ConId: 654503320, Position: -1, MarketPrice: 20936.9003906, MarketValue: -41873.8, AverageCost: 41873.88, UnrealisedPnl: 0, RealisedPnl: 0.08, AccountName: U8888888

At 16:45, we place an order to open a long position:

2024-11-27T16:45:00.2486565Z TRACE:: LiveTradingResultHandler.OrderEvent(): Time: 11/27/2024 16:45:00 OrderID: 53 EventID: 2 Symbol: MNQ20Z24 Status: Filled Quantity: 1 FillQuantity: 1 FillPrice: $20725.75 OrderFee: 0.62 USD Message: Interactive Brokers Order Fill Event BrokerId: 54

which closes the position:

2024-11-27T16:45:01.2083719Z TRACE:: InteractiveBrokersBrokerage.HandlePortfolioUpdates(): Contract: FUT MNQ USD , ConId: 654503320, Position: 0, MarketPrice: 20726.25, MarketValue: 0, AverageCost: 0, UnrealisedPnl: 421.76, RealisedPnl: 0, AccountName: U8888888

We should see the mismatch that occurred at 2024-11-27T14:30:01.5744480Z.

Potential Solution

Check HandlePortfolioUpdates for inconsistencies. Exit the algorithm.

Reproducing the Problem

Underterminitic. Bug on IB side.

Checklist

  • [x] I have completely filled out this template
  • [x] I have confirmed that this issue exists on the current master branch
  • [x] I have confirmed that this is not a duplicate issue by searching issues
  • [x] I have provided detailed steps to reproduce the issue

AlexCatarino avatar Dec 17 '24 16:12 AlexCatarino