{"record":{"id":"3cdfbc1c01af6009","repo":"QuantConnect/Lean","slug":"time-unexpected-liquidated-quantity-liquida","errorCode":null,"errorMessage":"{Time} - Unexpected liquidated quantity: {_liquidatedQuantity}. Expected: {-_boughtQuantity}","messagePattern":"(.+?) - Unexpected liquidated quantity: (.+?)\\. Expected: (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/BasicTemplateEurexFuturesAlgorithm.cs","lineNumber":144,"sourceCode":"                    {\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;\n                }\n            }\n        }\n\n        public override void OnEndOfAlgorithm()\n        {","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/BasicTemplateEurexFuturesAlgorithm.cs#L126-L162","documentation":"After liquidation, the absolute sell quantity must equal the buy quantity (_liquidatedQuantity == -_boughtQuantity). A mismatch means the position was not fully closed in one opposing fill — a partial liquidation, a quantity scaling error, or the engine liquidating a different size than was bought.","triggerScenarios":"OrderDirection.Sell filled where _liquidatedQuantity != -_boughtQuantity — e.g. buy filled 1 but sell filled 0 (partial), or sell filled a different magnitude than the held position.","commonSituations":"Partial fills make the recorded sell not the full inverse of the buy; delisting liquidation rounds/limits quantity; commission or split adjustments alter quantity; the buy recorded gross quantity while sell netted differently.","solutions":["Accumulate both buy and sell fill quantities so the comparison reflects total filled, not a single event's quantity.","Confirm the delisting liquidation sells the entire holdings quantity (Portfolio[_contractToTrade].Holdings.Quantity).","If partial liquidation is expected, compare running totals rather than per-event equality.","Log orderEvent.Quantity and Holdings at fill time to trace the divergence."],"exampleFix":"// before\nif (_liquidatedQuantity != -_boughtQuantity) { throw ...; }\n\n// after: compare accumulated totals after all fills settle\nif (_liquidatedRunningTotal != -_boughtRunningTotal) { throw ...; }","handlingStrategy":"validation","validationCode":"// Compare accumulated totals after all fills, not per-event\nif (orderEvent.Status == OrderStatus.Filled && _boughtRunningTotal + _liquidatedRunningTotal != 0)\n{\n    Log($\"{Time} - Net position nonzero: bought={_boughtRunningTotal} sold={_liquidatedRunningTotal}\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Accumulate both buy and sell fill totals before comparing.","Confirm delisting liquidation sells the full holdings quantity.","Log orderEvent.Quantity and Holdings.Quantity at each fill."],"tags":["futures","order-event","quantity","regression-test","eurex","liquidation"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}