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
- Ensure the selection function executes synchronously during Initialize/first algorithm time so _selected is true before any OnSecuritiesChanged.
- Avoid seeding securities into the universe that would raise changed events prior to selection.
- Use the immediate-selection AddUniverse overload that runs the selector at start.
- 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
- Run the selector synchronously at start so _selected is true before changed events.
- Avoid seeding universe securities that raise changed events pre-selection.
- Pin the AddUniverse overload that guarantees select-then-notify ordering.
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
- Universe selection should have been triggered right away. Th
- Future contracts did not work up as expected: {addedSecurity
- Future contracts did not work up as expected: {addedSecurity
- Expected {ExpectedSymbols.Count} stocks to be added to the a
- Expected symbols were not added to the algorithm
AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13).
Data as JSON: /api/errors/7da00c9e703b0ea3.
Report an issue: GitHub.