{"record":{"id":"0deab67c16539d67","repo":"QuantConnect/Lean","slug":"ondata-was-not-called","errorCode":null,"errorMessage":"OnData was not called","messagePattern":"OnData was not called","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/CustomUniverseWithBenchmarkRegressionAlgorithm.cs","lineNumber":158,"sourceCode":"                        _benchmarkPriceDidNotChange = 0;\n                    }\n                }\n            }\n            _previousBenchmarkValue = currentValue;\n            _previousTime = slice.Time;\n\n            // assert algorithm security is the correct one - not the internal one\n            if (security.Leverage != ExpectedLeverage)\n            {\n                throw new RegressionTestException($\"Leverage error - expected: {ExpectedLeverage}, actual: {security.Leverage}\");\n            }\n        }\n\n        public override void OnEndOfAlgorithm()\n        {\n            if (!_onDataWasCalled)\n            {\n                throw new RegressionTestException(\"OnData was not called\");\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 => 3500;\n","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/CustomUniverseWithBenchmarkRegressionAlgorithm.cs#L140-L176","documentation":"This RegressionTestException is thrown in OnEndOfAlgorithm. It asserts that _onDataWasCalled is true, meaning OnData was invoked at least once during the algorithm's execution. LEAN uses it as a terminal check to confirm the data pipeline delivered at least one Slice to the algorithm.","triggerScenarios":"_onDataWasCalled is false, meaning OnData never executed. This happens when no data was fed to the algorithm (all subscriptions failed to produce data), when the data-feed pipeline is broken, or when the algorithm ended before any data point arrived.","commonSituations":"Data files missing for the entire test date range, the data-feed subscription manager failed to create any valid subscriptions, a LEAN version change broke the data-enqueue loop, or the algorithm's start date equals end date with no data in between.","solutions":["Check that data files exist for all subscribed securities within the SetStartDate/SetEndDate range.","Verify the data-feed subscription creation succeeded — look for subscription errors in logs.","Trace the algorithm time-forward loop to confirm slices are being pumped to OnData.","If testing engine changes, check that the IAlgorithm.PublishToCallbacks or equivalent data-delivery path is intact."],"exampleFix":"// before\npublic override void OnEndOfAlgorithm()\n{\n    if (!_onDataWasCalled)\n    {\n        throw new RegressionTestException(\"OnData was not called\");\n    }\n}\n\n// after — check is fine; fix the root cause (missing data):\n// Verify in Initialize that subscriptions are created:\nAddEquity(\"SPY\", Resolution.Hour); // ensure this succeeds and data exists for 2013-10-04 to 2013-10-11","handlingStrategy":"validation","validationCode":"// In OnData, set the flag at the very top\npublic override void OnData(Slice slice)\n{\n    _onDataWasCalled = true;\n    // rest of logic\n}\n// In OnEndOfAlgorithm, log if not called\npublic override void OnEndOfAlgorithm()\n{\n    if (!_onDataWasCalled)\n        Log(\"FATAL: OnData was never called — check data feed and subscriptions\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify data files exist for all subscribed securities within the date range.","Check the algorithm log for subscription creation errors.","Confirm the data-feed time-forward loop is pumping slices to OnData."],"tags":["quantconnect","lean","ondata","lifecycle","data-feed","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}