{"record":{"id":"af84a67c952b8d40","repo":"QuantConnect/Lean","slug":"index-is-not-tradable","errorCode":null,"errorMessage":"Index is not tradable.","messagePattern":"Index is not tradable\\.","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/BasicTemplateIndexAlgorithm.cs","lineNumber":112,"sourceCode":"        }\n\n        /// <summary>\n        /// Asserts indicators are ready\n        /// </summary>\n        /// <exception cref=\"RegressionTestException\"></exception>\n        protected void AssertIndicators()\n        {\n            if (!_emaSlow.IsReady || !_emaFast.IsReady)\n            {\n                throw new RegressionTestException(\"Indicators are not ready!\");\n            }\n        }\n\n        public override void OnEndOfAlgorithm()\n        {\n            if (Portfolio[Spx].TotalSaleVolume > 0)\n            {\n                throw new RegressionTestException(\"Index is not tradable.\");\n            }\n            AssertIndicators();\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 virtual 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 virtual 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 virtual long DataPoints => 16199;","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/BasicTemplateIndexAlgorithm.cs#L94-L130","documentation":"Thrown in OnEndOfAlgorithm when Portfolio[Spx].TotalSaleVolume is greater than zero, meaning the Lean engine filled at least one order directly on the SPX index symbol. Index securities are non-tradable by design — they serve as price references and option underlyings only. A fill on the index itself indicates the engine incorrectly treated the index as a tradable security.","triggerScenarios":"Code calls MarketOrder(Spx, qty) or SetHoldings(Spx, target) on the raw index symbol, the engine's security initialization fails to set IsTradable=false for index types, or a brokerage model change inadvertently allows index order fills.","commonSituations":"Accidentally passing the index Symbol instead of an option Symbol to an order method, a regression introduced in SecurityService or BrokerageModel that changes the default tradability of Index securities, or modifying the algorithm to trade the index directly without using SetIsTradable(true) first.","solutions":["Ensure all order calls use SpxOption (or another tradable derivative), never Spx directly.","Verify Securities[Spx].IsTradable is false after Initialize — add a debug log to confirm.","Check IndexSecurity constructor and SecurityService for changes that may have reset IsTradable to true.","Inspect the algorithm's OnData logic to confirm no Liquidate(Spx) or MarketOrder(Spx) call paths exist."],"exampleFix":"// before — accidentally trading the index\nMarketOrder(Spx, 1);\n\n// after — trade the option on the index\nMarketOrder(SpxOption, 1);","handlingStrategy":"validation","validationCode":"// Before running, verify index is non-tradable\nif (Securities[Spx].IsTradable)\n{\n    Log($\"WARNING: {Spx} is marked tradable — index should not be tradable\");\n}","typeGuard":"bool IsIndexNonTradable(Symbol sym) =>\n    Securities[sym].Type == SecurityType.Index && !Securities[sym].IsTradable;","tryCatchPattern":"try\n{\n    if (Portfolio[Spx].TotalSaleVolume > 0)\n        throw new RegressionTestException(\"Index is not tradable.\");\n}\ncatch (RegressionTestException ex)\n{\n    Log($\"Index fill detected: volume={Portfolio[Spx].TotalSaleVolume}\");\n    throw;\n}","preventionTips":["Never pass an index Symbol directly to MarketOrder, SetHoldings, or Liquidate.","Add a code review check for any order method call with an index-typed symbol.","Log Securities[index].IsTradable after Initialize to verify default behavior.","Use option or ETF symbols derived from the index for all trades."],"tags":["quantconnect","index","tradability","regression-test","portfolio"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}