{"record":{"id":"09089c33dddf1c3e","repo":"QuantConnect/Lean","slug":"unexpected-sold-quantity-boughtquantity-and-li","errorCode":null,"errorMessage":"Unexpected sold quantity: {_boughtQuantity} and liquidated quantity: {_liquidatedQuantity}","messagePattern":"Unexpected sold quantity: (.+?) and liquidated quantity: (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/BasicTemplateEurexFuturesAlgorithm.cs","lineNumber":176,"sourceCode":"            }\n        }\n\n        public override void OnEndOfAlgorithm()\n        {\n            if (_mappingsCount == 0)\n            {\n                throw new RegressionTestException($\"Unexpected number of symbol changed events (mappings): {_mappingsCount}. Expected 1.\");\n            }\n\n            if (!_delisted)\n            {\n                throw new RegressionTestException(\"Contract was not delisted\");\n            }\n\n            // Make sure we traded and that the position was liquidated on delisting\n            if (_boughtQuantity <= 0 || _liquidatedQuantity >= 0)\n            {\n                throw new RegressionTestException($\"Unexpected sold quantity: {_boughtQuantity} and liquidated quantity: {_liquidatedQuantity}\");\n            }\n        }\n\n        /// <summary>\n        /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.\n        /// </summary>\n        public bool CanRunLocally { get; } = true;\n\n        /// <summary>\n        /// This is used by the regression test system to indicate which languages this algorithm is written in.\n        /// </summary>\n        public List<Language> Languages { get; } = new() { Language.CSharp, Language.Python };\n\n        /// <summary>\n        /// Data Points count of all timeslices of algorithm\n        /// </summary>\n        public long DataPoints => 94326;\n","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/BasicTemplateEurexFuturesAlgorithm.cs#L158-L194","documentation":"OnEndOfAlgorithm final trade check: the algorithm must have bought (_boughtQuantity > 0) and liquidated (_liquidatedQuantity < 0). If either is wrong sign/zero, the algorithm did not complete a full buy-then-liquidate cycle — it never traded, or never closed the position.","triggerScenarios":"OnEndOfAlgorithm runs with _boughtQuantity <= 0 (never bought) OR _liquidatedQuantity >= 0 (never liquidated). The order-event state machine never recorded both a positive buy fill and a negative sell fill.","commonSituations":"Exchange was never open when the buy gate (line 95) was checked, so no Buy() fired; the contract delisted before a buy; liquidation sell never arrived; buy/sell fills recorded under a different symbol and rejected by the line-116 guard.","solutions":["Verify Securities[_contractToTrade].Exchange.ExchangeOpen is true at some point after _contractToTrade is set so Buy() executes.","Confirm the buy fill event passed the line-116 symbol guard and recorded _boughtQuantity.","Confirm delisting liquidation produced the sell fill that recorded _liquidatedQuantity.","Trace OnOrderEvent to ensure both buy and sell filled events reach the recording branches."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"public override void OnEndOfAlgorithm()\n{\n    if (_boughtQuantity <= 0) Log(\"Never bought — check ExchangeOpen gate and order fills.\");\n    if (_liquidatedQuantity >= 0) Log(\"Never liquidated — check delisting liquidation sell fill.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Confirm Exchange.ExchangeOpen is true so Buy() executes.","Trace OnOrderEvent so both buy and sell fills are recorded.","Ensure order-event symbol guards don't reject the legitimate fills."],"tags":["futures","order-event","invariant","regression-test","eurex","trading"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}