QuantConnect/Lean · error · RegressionTestException

Unexpected ActiveSecurities count: {ActiveSecurities.Count}

Error message

Unexpected ActiveSecurities count: {ActiveSecurities.Count}

What it means

This RegressionTestException is thrown in OnData immediately after the slice.Count check. It asserts ActiveSecurities.Count == 2, verifying that the algorithm's active security collection contains exactly the two expected securities (AAPL from AddEquity, SPY from universe selection). Unlike slice.Count which reflects data in one time-step, ActiveSecurities reflects the algorithm's subscription state.

Source

Thrown at Algorithm.CSharp/CustomUniverseSelectionRegressionAlgorithm.cs:62

                Resolution.Daily,
                Market.USA,
                UniverseSettings,
                time => new[] { "SPY" });
        }

        /// <summary>
        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// </summary>
        /// <param name="slice">Slice object keyed by symbol containing the stock data</param>
        public override void OnData(Slice slice)
        {
            if (slice.Count != 2)
            {
                throw new RegressionTestException($"Unexpected data count: {slice.Count}");
            }
            if (ActiveSecurities.Count != 2)
            {
                throw new RegressionTestException($"Unexpected ActiveSecurities count: {ActiveSecurities.Count}");
            }
            if (!Portfolio.Invested)
            {
                SetHoldings(Securities.Keys.First(symbol => symbol.Value == "SPY"), 1);
                Debug("Purchased Stock");
            }
        }

        /// <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 };

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Log ActiveSecurities.Keys to see exactly which securities are active and identify unexpected entries.
  2. Check if SetBenchmark or any other Initialize call creates additional securities that shouldn't be in ActiveSecurities.
  3. Verify universe-selection removal timing hasn't changed — securities should remain active for the full test window.
  4. If testing engine changes, trace SecurityManager additions and removals to ensure only the two intended securities are active.

Example fix

// before
if (ActiveSecurities.Count != 2)
{
    throw new RegressionTestException($"Unexpected ActiveSecurities count: {ActiveSecurities.Count}");
}

// after — diagnostic
if (ActiveSecurities.Count != 2)
{
    var keys = string.Join(", ", ActiveSecurities.Keys.Select(s => s.Value));
    throw new RegressionTestException($"Unexpected ActiveSecurities count: {ActiveSecurities.Count}. Keys: {keys}");
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate ActiveSecurities before asserting
if (ActiveSecurities.Count != 2)
{
    Log($"ActiveSecurities: {string.Join(", ", ActiveSecurities.Keys.Select(k => k.Value))}");
    // identify unexpected entries and investigate their source
}

Prevention

When it happens

Trigger: ActiveSecurities.Count != 2 when a security was added but not subscribed (stuck in pending state), when a universe removal dropped a security before OnData, or when an extra security was created unintentionally (e.g. benchmark or internal subscription leaking into ActiveSecurities).

Common situations: SetBenchmark or other internal subscriptions inadvertently create active securities, universe-selection removals fire too early, or a LEAN version change altered when securities transition from added to active. Also occurs when the algorithm's SecuritiesManager state diverges from the expected two-security setup.

Related errors


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