{"record":{"id":"f0a1fa8ca8092ad1","repo":"QuantConnect/Lean","slug":"time-unexpected-buy-order-event-status-order","errorCode":null,"errorMessage":"{Time} - Unexpected buy order event status: {orderEvent.Status}","messagePattern":"(.+?) - Unexpected buy order event status: (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/BasicTemplateEurexFuturesAlgorithm.cs","lineNumber":127,"sourceCode":"                    }\n                }\n            }\n        }\n\n        public override void OnOrderEvent(OrderEvent orderEvent)\n        {\n            if (orderEvent.Symbol != _contractToTrade)\n            {\n                throw new RegressionTestException($\"{Time} - Unexpected order event symbol: {orderEvent.Symbol}. Expected {_contractToTrade}\");\n            }\n\n            if (orderEvent.Direction == OrderDirection.Buy)\n            {\n                if (orderEvent.Status == OrderStatus.Filled)\n                {\n                    if (_boughtQuantity != 0 && _liquidatedQuantity != 0)\n                    {\n                        throw new RegressionTestException($\"{Time} - Unexpected buy order event status: {orderEvent.Status}\");\n                    }\n                    _boughtQuantity = orderEvent.Quantity;\n                }\n            }\n            else if (orderEvent.Direction == OrderDirection.Sell)\n            {\n                if (orderEvent.Status == OrderStatus.Filled)\n                {\n                    if (_boughtQuantity <= 0 && _liquidatedQuantity != 0)\n                    {\n                        throw new RegressionTestException($\"{Time} - Unexpected sell order event status: {orderEvent.Status}\");\n                    }\n                    _liquidatedQuantity = orderEvent.Quantity;\n\n                    if (_liquidatedQuantity != -_boughtQuantity)\n                    {\n                        throw new RegressionTestException($\"{Time} - Unexpected liquidated quantity: {_liquidatedQuantity}. Expected: {-_boughtQuantity}\");\n                    }","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/BasicTemplateEurexFuturesAlgorithm.cs#L109-L145","documentation":"The order-event handler is a small state machine: a buy fill is only valid as the FIRST fill (when neither a buy nor a sell has been recorded). If _boughtQuantity != 0 AND _liquidatedQuantity != 0, a second buy fill arrived after both legs completed — a duplicate or unexpected re-entry fill that violates the single-buy/single-sell lifecycle.","triggerScenarios":"An OrderDirection.Buy with OrderStatus.Filled arrives when _boughtQuantity is already non-zero and _liquidatedQuantity is already non-zero. Concretely, a duplicate buy fill after the position was already opened and closed.","commonSituations":"A partial fill followed by another fill that the state machine treats as a second full buy; a re-entry order the algorithm didn't intend; engine fill event duplication during delisting liquidation; order updates/cancels producing extra filled events.","solutions":["Track fill accumulation (sum partial fills) instead of overwriting _boughtQuantity on every filled event.","Guard against duplicate fills by checking orderEvent.OrderId or a processed-fill set.","Confirm only one Buy(_contractToTrade, 1) call executes (the _boughtQuantity == 0 gate at line 95).","If partial fills are expected, accumulate quantity until OrderStatus.Filled terminal and then record once."],"exampleFix":"// before\nif (_boughtQuantity != 0 && _liquidatedQuantity != 0) { throw ...; }\n_boughtQuantity = orderEvent.Quantity;\n\n// after: accumulate partial buys, record the buy total once\n_boughtQuantity += orderEvent.Quantity;","handlingStrategy":"validation","validationCode":"// Accumulate fills instead of overwriting; detect the first buy only\nif (orderEvent.Direction == OrderDirection.Buy && orderEvent.Status == OrderStatus.Filled)\n{\n    _boughtQuantity += orderEvent.Quantity;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Accumulate partial buy fills into a running total.","Track processed OrderIds to ignore duplicate fills.","Gate the single Buy() call on _boughtQuantity == 0."],"tags":["futures","order-event","state-machine","regression-test","eurex","fills"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}