QuantConnect/Lean · error · RegressionTestException

OnData was not called

Error message

OnData was not called

What it means

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.

Source

Thrown at Algorithm.CSharp/CustomUniverseWithBenchmarkRegressionAlgorithm.cs:158

                        _benchmarkPriceDidNotChange = 0;
                    }
                }
            }
            _previousBenchmarkValue = currentValue;
            _previousTime = slice.Time;

            // assert algorithm security is the correct one - not the internal one
            if (security.Leverage != ExpectedLeverage)
            {
                throw new RegressionTestException($"Leverage error - expected: {ExpectedLeverage}, actual: {security.Leverage}");
            }
        }

        public override void OnEndOfAlgorithm()
        {
            if (!_onDataWasCalled)
            {
                throw new RegressionTestException("OnData was not called");
            }
        }

        /// <summary>
        /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
        /// </summary>
        public bool CanRunLocally { get; } = true;

        /// <summary>
        /// This is used by the regression test system to indicate which languages this algorithm is written in.
        /// </summary>
        public List<Language> Languages { get; } = new() { Language.CSharp };

        /// <summary>
        /// Data Points count of all timeslices of algorithm
        /// </summary>
        public long DataPoints => 3500;

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Check that data files exist for all subscribed securities within the SetStartDate/SetEndDate range.
  2. Verify the data-feed subscription creation succeeded — look for subscription errors in logs.
  3. Trace the algorithm time-forward loop to confirm slices are being pumped to OnData.
  4. If testing engine changes, check that the IAlgorithm.PublishToCallbacks or equivalent data-delivery path is intact.

Example fix

// before
public override void OnEndOfAlgorithm()
{
    if (!_onDataWasCalled)
    {
        throw new RegressionTestException("OnData was not called");
    }
}

// after — check is fine; fix the root cause (missing data):
// Verify in Initialize that subscriptions are created:
AddEquity("SPY", Resolution.Hour); // ensure this succeeds and data exists for 2013-10-04 to 2013-10-11
Defensive patterns

Strategy: validation

Validate before calling

// In OnData, set the flag at the very top
public override void OnData(Slice slice)
{
    _onDataWasCalled = true;
    // rest of logic
}
// In OnEndOfAlgorithm, log if not called
public override void OnEndOfAlgorithm()
{
    if (!_onDataWasCalled)
        Log("FATAL: OnData was never called — check data feed and subscriptions");
}

Prevention

When it happens

Trigger: _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.

Common situations: 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.

Related errors


AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13). Data as JSON: /api/errors/0deab67c16539d67. Report an issue: GitHub.