QuantConnect/Lean · error · RegressionTestException

Universe selection should have been triggered right away. Th

Error message

Universe selection should have been triggered right away. The first OnData call should have had happened after the universe selection

What it means

Asserts universe selection ran before the first OnData. _selected is set true inside the selection function; the first OnData checks it and throws if still false. The contract: a custom universe with a selection function must select on algorithm start so securities exist before data flows.

Source

Thrown at Algorithm.CSharp/CustomUniverseImmediateSelectionRegressionAlgorithm.cs:67

            AddUniverse(SecurityType.Equity,
                "my-custom-universe",
                Resolution.Daily,
                Market.USA,
                UniverseSettings,
                time =>
                {
                    _selected = true;
                    return new[] { "SPY", "GOOG", "APPL" };
                });
        }

        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)

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Use the AddUniverse overload that accepts a selection function and triggers selection at algorithm start (this test uses the SecurityType+market overload).
  2. Confirm UniverseSettings.Resolution and the universe Resolution are set so selection is not waiting on a slower data trigger.
  3. Verify the selection delegate returns symbols (non-empty) so selection actually completes and sets the flag.
  4. If selection is deferred by design in a newer Lean version, force an initial selection via Schedule or a manual trigger.

Example fix

// before: selection waits for first universe data point, OnData fires first
AddUniverse("my-universe", time => new[] { "SPY" });

// after: use the overload that selects immediately 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

// Force eager selection and assert it ran before first OnData
AddUniverse(SecurityType.Equity, "my-custom-universe", Resolution.Daily, Market.USA, UniverseSettings,
    time => { _selected = true; return new[] { "SPY", "GOOG", "APPL" }; });
// in OnData first call: if (!_selected) Log("Selection not yet run");

Prevention

When it happens

Trigger: OnData's first invocation occurs while _selected is false, meaning the selection delegate was not invoked during/after Initialize and before the first time slice reached the algorithm.

Common situations: Universe selection scheduled lazily (e.g., only on the first universe data point) instead of eagerly; a Lean change that deferred selection; or AddUniverse overload that does not trigger immediate selection at start.

Related errors


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