{"record":{"id":"9ee35e89550ae417","repo":"QuantConnect/Lean","slug":"unexpected-holdings","errorCode":null,"errorMessage":"Unexpected holdings","messagePattern":"Unexpected holdings","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/EmitInsightNoAlphaModelAlgorithm.cs","lineNumber":99,"sourceCode":"                {\n                    throw new RegressionTestException(\"Expected open order for emitted insight\");\n                }\n                if (order.Direction != OrderDirection.Sell\n                    || order.Symbol != _symbol)\n                {\n                    throw new RegressionTestException($\"Unexpected open order for emitted insight: {order}\");\n                }\n\n                SetHoldings(_symbol, 1);\n            }\n        }\n\n        public override void OnEndOfAlgorithm()\n        {\n            var holdings = Securities[_symbol].Holdings;\n            if (Math.Sign(holdings.Quantity) != -1)\n            {\n                throw new RegressionTestException(\"Unexpected holdings\");\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 };\n\n        /// <summary>\n        /// Data Points count of all timeslices of algorithm\n        /// </summary>\n        public long DataPoints => 48;\n","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/EmitInsightNoAlphaModelAlgorithm.cs#L81-L117","documentation":"In OnEndOfAlgorithm this asserts the final holdings quantity for _symbol is negative: Math.Sign(holdings.Quantity) == -1. Since the Down insight drove a Sell (short) position, the portfolio must end short. A non-negative sign means the position was not established as short (or was flipped/closed).","triggerScenarios":"The sell order never filled (quantity stays 0), or the position was later liquidated/flipped before algorithm end. A Buy direction error (118) would also cause this.","commonSituations":"Order not filled by end of backtest (no data/no liquidity); short-selling blocked by the brokerage model (borrowing not allowed) leaving quantity 0; a later OnData step reversed the position.","solutions":["Check Securities[_symbol].Holdings.Quantity at end and the fill events for the sell order.","Confirm the brokerage model permits short selling for the security.","Ensure no subsequent insight/order flipped the position before OnEndOfAlgorithm."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"var qty = Securities[_symbol].Holdings.Quantity;\nif (Math.Sign(qty) != -1)\n{\n    Log($\"Expected short position, got qty={qty}. Check fill events and brokerage short-sell permission.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Confirm the brokerage model permits short selling for the security.","Verify the sell order filled before algorithm end.","Ensure no later insight/order reversed the position."],"tags":["quantconnect","lean","regression-test","insights","portfolio","short-selling","postcondition"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}