{"record":{"id":"730e6cce03c893de","repo":"QuantConnect/Lean","slug":"contract-was-not-delisted","errorCode":null,"errorMessage":"Contract was not delisted","messagePattern":"Contract was not delisted","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/BasicTemplateEurexFuturesAlgorithm.cs","lineNumber":170,"sourceCode":"            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        {\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 };","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/BasicTemplateEurexFuturesAlgorithm.cs#L152-L188","documentation":"OnEndOfAlgorithm asserts the traded contract was actually delisted (_delisted == true). _delisted is only set when slice.Delistings for _contractToTrade reports DelistingType.Delisted (line 102-104). If it never fired, the contract did not expire in-window, so the delisting/liquidation test path was never exercised.","triggerScenarios":"OnEndOfAlgorithm runs with _delisted == false — no DelistingType.Delisted event arrived for _contractToTrade during the backtest. The chosen contract (the previously-mapped, sooner-expiring one) did not reach its delisting date within the window.","commonSituations":"Date window ended before the held contract's expiry; the delisting data event is missing; _contractToTrade was never assigned because no mapping occurred; the wrong (later-expiring) contract was held.","solutions":["Confirm _contractToTrade was set (a mapping occurred) and that contract's expiry falls within the end date.","Verify the contract's delisting data (warning + delisted events) is present.","Extend SetEndDate past the held contract's expiration if the window was shortened.","Ensure the held contract is the sooner-expiring previous-mapped one (line 90), not the new mapped one."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"public override void OnEndOfAlgorithm()\n{\n    if (!_delisted)\n    {\n        Log(\"Contract was not delisted in-window; verify _contractToTrade expiry vs EndDate.\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Hold the sooner-expiring previous-mapped contract so delisting falls in-window.","Set EndDate past the held contract's expiry.","Confirm delisting data events are present."],"tags":["futures","delisting","regression-test","eurex","invariant"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}