QuantConnect/Lean · error · RegressionTestException

Universe selection should have been triggered right away

Error message

Universe selection should have been triggered right away

What it means

Asserts the selection function ran before the first OnSecuritiesChanged callback. If OnSecuritiesChanged fires while _selected is false, the securities-changed event was raised without the selection delegate having executed, violating the 'select first, then notify' ordering.

Source

Thrown at Algorithm.CSharp/CustomUniverseImmediateSelectionRegressionAlgorithm.cs:79

        public override void OnData(Slice slice)
        {
            if (_firstOnData)
            {
                if (!_selected)
                {
                    throw new RegressionTestException("Universe selection should have been triggered right away. " +
                        "The first OnData call should have had happened after the universe selection");
                }

                _firstOnData = false;
            }
        }

        public override void OnSecuritiesChanged(SecurityChanges changes)
        {
            if (!_selected)
            {
                throw new RegressionTestException("Universe selection should have been triggered right away");
            }

            if (!_securitiesChanged)
            {
                // Selection should be happening right on algorithm start
                if (Time != StartDate)
                {
                    throw new RegressionTestException("Universe selection should have been triggered right away");
                }

                if (changes.AddedSecurities.Count != ExpectedSymbols.Count)
                {
                    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)))
                {

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Ensure the selection function executes synchronously during Initialize/first algorithm time so _selected is true before any OnSecuritiesChanged.
  2. Avoid seeding securities into the universe that would raise changed events prior to selection.
  3. Use the immediate-selection AddUniverse overload that runs the selector at start.
  4. Re-order lifecycle hooks so selection precedes security-change notification.

Example fix

// before: seeding raises OnSecuritiesChanged before selection runs
AddUniverse(seededSymbols, selector);

// after: no pre-seed; selector runs first at start
AddUniverse(SecurityType.Equity, "my-custom-universe", Resolution.Daily, Market.USA, UniverseSettings,
    time => { _selected = true; return new[] { "SPY", "GOOG", "APPL" }; });
Defensive patterns

Strategy: validation

Validate before calling

// Track selection state and assert ordering in callbacks
if (!_selected)
    Log($"OnSecuritiesChanged fired before selection at {Time}");

Prevention

When it happens

Trigger: OnSecuritiesChanged is invoked (e.g., from initial security seeding) before the universe's selection delegate sets _selected=true, so the callback sees the flag still false.

Common situations: Initial seeding of the universe's configured securities triggers OnSecuritiesChanged ahead of the first selection pass; or selection is scheduled after the first changed event in the lifecycle.

Related errors


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