QuantConnect/Lean · error · RegressionTestException

Expected events didn't happen

Error message

Expected events didn't happen

What it means

This RegressionTestException is thrown in OnEndOfAlgorithm as a final sanity gate. It checks three boolean flags: _firstOnData (must be false, meaning OnData fired at least once), _selected (universe selector function was invoked), and _securitiesChanged (OnSecuritiesChanged fired). LEAN uses it to guarantee the full event lifecycle of the algorithm executed.

Source

Thrown at Algorithm.CSharp/CustomUniverseImmediateSelectionRegressionAlgorithm.cs:109

                {
                    throw new RegressionTestException($"Expected {ExpectedSymbols.Count} stocks to be added to the algorithm, " +
                        $"but found {changes.AddedSecurities.Count}");
                }

                if (!ExpectedSymbols.All(x => changes.AddedSecurities.Any(security => security.Symbol == x)))
                {
                    throw new RegressionTestException("Expected symbols were not added to the algorithm");
                }

                _securitiesChanged = true;
            }
        }

        public override void OnEndOfAlgorithm()
        {
            if (_firstOnData || !_selected || !_securitiesChanged)
            {
                throw new RegressionTestException("Expected events didn't happen");
            }
        }

        /// <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 => 52;

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Check each flag individually to isolate which event failed: log _firstOnData, _selected, _securitiesChanged.
  2. If _selected is false, trace the universe-selection scheduling to confirm the selector is invoked at algorithm start.
  3. If _firstOnData is true (OnData never ran), verify data files exist for the date range and the subscription is properly created.
  4. If _securitiesChanged is false, ensure the engine emits SecurityChanges even when the added set is non-empty on the first selection.

Example fix

// before — single combined assertion gives no clue which event failed
if (_firstOnData || !_selected || !_securitiesChanged)
{
    throw new RegressionTestException("Expected events didn't happen");
}

// after — split for faster diagnosis
if (_firstOnData) throw new RegressionTestException("OnData was never called");
if (!_selected) throw new RegressionTestException("Universe selector was never invoked");
if (!_securitiesChanged) throw new RegressionTestException("OnSecuritiesChanged was never called");
Defensive patterns

Strategy: validation

Validate before calling

// In OnEndOfAlgorithm, check each flag independently for clearer diagnosis
public override void OnEndOfAlgorithm()
{
    if (_firstOnData) Log("WARNING: OnData was never called");
    if (!_selected) Log("WARNING: Universe selector was never invoked");
    if (!_securitiesChanged) Log("WARNING: OnSecuritiesChanged was never called");
}

Prevention

When it happens

Trigger: One or more of the lifecycle events never fired: OnData was never called (no data fed to algorithm), the universe selector function was never invoked (_selected stays false), or OnSecuritiesChanged was never called. Any single missing event trips the combined condition.

Common situations: Data file missing or unreadable so no slices reach OnData, universe-selection disabled or broken in engine changes so the selector is never called, or SecurityChanges events suppressed by an engine refactor. Also happens when algorithm start/end dates have no market data.

Related errors


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