{"record":{"id":"52fd66e1a17fa89f","repo":"QuantConnect/Lean","slug":"portfolio-should-be-invested","errorCode":null,"errorMessage":"Portfolio should be invested","messagePattern":"Portfolio should be invested","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/DuplicateOptionAssignmentRegressionAlgorithm.cs","lineNumber":193,"sourceCode":"            if (!_optionDelisted)\n            {\n                throw new RegressionTestException(\"Option was not delisted\");\n            }\n\n            if (!_orderCanceled)\n            {\n                throw new RegressionTestException(\"Order was not canceled\");\n            }\n\n            var openOrders = Transactions.GetOpenOrders();\n            if (openOrders.Count != 0)\n            {\n                throw new RegressionTestException(\"There should be no open orders\");\n            }\n\n            if (!Portfolio.Invested)\n            {\n                throw new RegressionTestException(\"Portfolio should be invested\");\n            }\n\n            // We should have the stock since the option was assigned\n            if (Portfolio.Positions.Groups.Single().Single().Symbol != _stock)\n            {\n                throw new RegressionTestException(\"Portfolio should have the stock\");\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 };","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/DuplicateOptionAssignmentRegressionAlgorithm.cs#L175-L211","documentation":"Asserts Portfolio.Invested is true at algorithm end: the assignment should have left a non-zero stock position. If Invested is false, the stock position is flat/zero, meaning the assignment did not result in a held stock position.","triggerScenarios":"The stock position was never opened (assignment failed, see 104/105) or was closed/liquidated by a later step. Quantity rounded to zero due to lot sizing.","commonSituations":"Assignment quantity zero; position-group modeling cleared the position on delisting; an unintended liquidation ran before end.","solutions":["Check Portfolio[stock].Quantity at end; if zero, trace why assignment did not deliver shares.","Confirm errors 104/105 did not abort first (they gate this invariant).","Verify no auto-liquidation or margin call closed the assigned position."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"var qty = Portfolio[_stock].Quantity;\nif (qty == 0) Log($\"Stock position is flat; assignment did not deliver shares (qty={qty})\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Resolve the assignment-delivery invariants (104/105) before this check.","Log the stock position quantity at assignment time and at end.","Confirm no auto-liquidation closed the assigned position."],"tags":["quantconnect","lean","regression-test","portfolio","positions","postcondition"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}