{"record":{"id":"7854bcee726f7f3c","repo":"QuantConnect/Lean","slug":"time-unexpected-sell-order-event-status-orde","errorCode":null,"errorMessage":"{Time} - Unexpected sell order event status: {orderEvent.Status}","messagePattern":"(.+?) - Unexpected sell order event status: (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/BasicTemplateEurexFuturesAlgorithm.cs","lineNumber":138,"sourceCode":"\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                    }\n                }\n            }\n        }\n\n        public override void OnSecuritiesChanged(SecurityChanges changes)\n        {\n            foreach (var addedSecurity in changes.AddedSecurities)\n            {\n                if (addedSecurity.Symbol.SecurityType == SecurityType.Future && addedSecurity.Symbol.IsCanonical())\n                {\n                    _mappedSymbol = _continuousContract.Mapped;","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/BasicTemplateEurexFuturesAlgorithm.cs#L120-L156","documentation":"The sell-leg invariant: a sell fill is only valid when a prior buy exists (_boughtQuantity > 0) and no sell has been recorded yet (_liquidatedQuantity == 0). If _boughtQuantity <= 0 and _liquidatedQuantity != 0, a sell fill arrived without a preceding buy or after liquidation already happened — an out-of-sequence or duplicate sell fill.","triggerScenarios":"An OrderDirection.Sell with OrderStatus.Filled arrives while _boughtQuantity <= 0 and _liquidatedQuantity != 0 — e.g. a delisting liquidation sell when the buy was never recorded, or a second sell fill after liquidation.","commonSituations":"The buy fill event was missed/not recorded so _boughtQuantity stays 0 when the liquidation sell fires; delisting auto-liquidation emits a sell before the buy event was processed; a duplicate liquidation sell after the position is already closed.","solutions":["Verify the buy fill was recorded (_boughtQuantity > 0) before any sell is expected — check event ordering.","Accumulate sell fills instead of treating each as the sole liquidation.","Confirm delisting liquidation fires only once (no duplicate sell events).","If using OnOrderEvent for a sell that can legitimately precede state, restructure the state machine to allow it."],"exampleFix":"// before\nif (_boughtQuantity <= 0 && _liquidatedQuantity != 0) { throw ...; }\n\n// after: tolerate liquidation that arrives without a recorded buy (e.g. engine-forced)\n_liquidatedQuantity += orderEvent.Quantity;","handlingStrategy":"validation","validationCode":"if (orderEvent.Direction == OrderDirection.Sell && orderEvent.Status == OrderStatus.Filled)\n{\n    _liquidatedQuantity += orderEvent.Quantity;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify a buy fill was recorded before expecting a sell.","Accumulate sell fills into a running total.","Confirm delisting liquidation fires once."],"tags":["futures","order-event","state-machine","regression-test","eurex","liquidation"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}