{"record":{"id":"d9727421796d5cfc","repo":"QuantConnect/Lean","slug":"trade-volume-should-be-greater-than-zero-by-the-en","errorCode":null,"errorMessage":"Trade volume should be greater than zero by the end of this algorithm","messagePattern":"Trade volume should be greater than zero by the end of this algorithm","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/BasicTemplateIndexOptionsAlgorithm.cs","lineNumber":110,"sourceCode":"                    }\n                    else if (_emaFast < _emaSlow && contract.Right == OptionRight.Put)\n                    {\n                        Liquidate(InvertOption(contract.Symbol));\n                        MarketOrder(contract.Symbol, 1);\n                    }\n                }\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            if (Portfolio.TotalSaleVolume == 0)\n            {\n                throw new RegressionTestException(\"Trade volume should be greater than zero by the end of this algorithm\");\n            }\n            AssertIndicators();\n        }\n\n        public Symbol InvertOption(Symbol symbol)\n        {\n            return QuantConnect.Symbol.CreateOption(\n                symbol.Underlying,\n                symbol.ID.Market,\n                symbol.ID.OptionStyle,\n                symbol.ID.OptionRight == OptionRight.Call ? OptionRight.Put : OptionRight.Call,\n                symbol.ID.StrikePrice,\n                symbol.ID.Date);\n        }\n\n        /// <summary>\n        /// Asserts indicators are ready\n        /// </summary>","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/BasicTemplateIndexOptionsAlgorithm.cs#L92-L128","documentation":"Thrown in OnEndOfAlgorithm when Portfolio.TotalSaleVolume equals zero, meaning no option orders were filled during the entire algorithm run. This assertion verifies that the EMA-cross trading logic actually generated and filled option orders on SPX contracts.","triggerScenarios":"The EMA fast/slow cross condition never triggers because indicators are not ready, the option chain filter (CallsOnly) returns no contracts, slice.OptionChains is always empty, or MarketOrder calls are rejected/invalid before filling.","commonSituations":"Option data files missing for the date range, option filter too restrictive, EMA indicators never reaching the crossover state within the backtest window, or a brokerage model change rejecting option orders.","solutions":["Verify SPX option data exists for the full date range (2021-01-04 to 2021-02-01).","Check that slice.OptionChains is populated in OnData — log chain count per slice.","Ensure EMA crossover occurs: log _emaFast and _emaSlow values to confirm the condition triggers.","Relax or verify the CallsOnly filter does not exclude all available contracts.","Inspect order tickets for rejection messages via ticket.Status and ticket.CancelMessage."],"exampleFix":"// before — no log visibility into why no trades\nif (Portfolio.TotalSaleVolume == 0)\n{\n    throw new RegressionTestException(\"Trade volume should be greater than zero\");\n}\n\n// after — diagnose before asserting\nif (Portfolio.TotalSaleVolume == 0)\n{\n    Log($\"EMA Fast Ready: {_emaFast.IsReady}, Slow Ready: {_emaSlow.IsReady}\");\n    Log($\"Cross current: fast={_emaFast} slow={_emaSlow}\");\n    throw new RegressionTestException(\"Trade volume should be greater than zero\");\n}","handlingStrategy":"validation","validationCode":"// Before asserting volume, log diagnostic state\nif (Portfolio.TotalSaleVolume == 0)\n{\n    Log($\"EMA fast ready={_emaFast.IsReady}, slow ready={_emaSlow.IsReady}\");\n    Log($\"Cross state: fast={_emaFast}, slow={_emaSlow}, fast>slow={_emaFast > _emaSlow}\");\n    Log($\"Option chains received: check OnData logs\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Log option chain availability in OnData to confirm data flows.","Verify the option filter (CallsOnly) returns contracts for the date range.","Ensure EMA crossover condition triggers within the backtest window.","Check order tickets for rejections via OnOrderEvent logging."],"tags":["quantconnect","options","trade-volume","ema-cross","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}