{"record":{"id":"764e97a6c332a771","repo":"QuantConnect/Lean","slug":"time-portfolio-should-not-be-invested-after-th","errorCode":null,"errorMessage":"{Time} - Portfolio should not be invested after the traded contract is delisted.","messagePattern":"(.+?) - Portfolio should not be invested after the traded contract is delisted\\.","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/BasicTemplateEurexFuturesAlgorithm.cs","lineNumber":108,"sourceCode":"                _contractToTrade = _mappedSymbol;\n                _mappedSymbol = _continuousContract.Mapped;\n            }\n\n            // Let's trade after the mapping is done\n            if (_contractToTrade != null && _boughtQuantity == 0 && Securities[_contractToTrade].Exchange.ExchangeOpen)\n            {\n                Buy(_contractToTrade, 1);\n            }\n\n            if (_contractToTrade != null && slice.Delistings.TryGetValue(_contractToTrade, out var delisting))\n            {\n                if (delisting.Type == DelistingType.Delisted)\n                {\n                    _delisted = true;\n\n                    if (Portfolio.Invested)\n                    {\n                        throw new RegressionTestException($\"{Time} - Portfolio should not be invested after the traded contract is delisted.\");\n                    }\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                    {","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/BasicTemplateEurexFuturesAlgorithm.cs#L90-L126","documentation":"When the traded EUREX contract reaches its DelistingType.Delisted event, Lean is expected to auto-liquidate any open position. This assertion verifies Portfolio.Invested is false at that moment — if the portfolio still holds the position, delisting liquidation did not occur as designed. It guards the core guarantee that expired/delisted futures positions are closed by the engine.","triggerScenarios":"slice.Delistings for _contractToTrade reports DelistingType.Delisted while Portfolio.Invested is still true. The contract was bought (Buy(_contractToTrade, 1)) earlier and the position was never liquidated before/at delisting.","commonSituations":"Engine delisting/liquidation logic changed; a fill on the buy order never settled so the position is in an unexpected state; the delisting data for the contract is missing the price warning stage; algorithm logic interfered with auto-liquidation.","solutions":["Confirm the traded contract's delisting data is present (warning + delisted events).","Verify the engine's delisting-time liquidation path is intact (no custom Holdings/settlement logic that prevents auto-close).","If adapting, explicitly Liquidate() on DelistingType.WarningTarget to close before the forced delisted stage.","Check that the buy fill (_boughtQuantity) was recorded so the position is in the expected invested state prior to delisting."],"exampleFix":"// before: rely solely on engine auto-liquidation at delisted stage\n// after: liquidate on the warning stage to avoid a stranded position\nif (delisting.Type == DelistingType.WarningTarget && Portfolio.Invested)\n{\n    Liquidate();\n}","handlingStrategy":"validation","validationCode":"// Close the position at the warning stage, before forced delisting\nif (_contractToTrade != null && slice.Delistings.TryGetValue(_contractToTrade, out var dl))\n{\n    if (dl.Type == DelistingType.WarningTarget && Portfolio.Invested)\n    {\n        Liquidate(_contractToTrade);\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Liquidate explicitly on DelistingType.WarningTarget rather than relying solely on forced delisting.","Confirm delisting data (warning + delisted) is present for the contract.","Avoid custom Holdings logic that blocks engine auto-liquidation."],"tags":["futures","delisting","liquidation","regression-test","eurex","portfolio"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}