QuantConnect/Lean · error · RegressionTestException

Unexpected data count: {slice.Count}

Error message

Unexpected data count: {slice.Count}

What it means

This RegressionTestException is thrown in OnData of a regression algorithm that adds AAPL via AddEquity and SPY via a custom universe selector, both at Daily resolution. It asserts slice.Count == 2, meaning exactly two securities produced data in that time slice. LEAN throws it to verify that manually-added securities and universe-selected securities start delivering data simultaneously.

Source

Thrown at Algorithm.CSharp/CustomUniverseSelectionRegressionAlgorithm.cs:58

            UniverseSettings.Resolution = Resolution.Daily;
            AddUniverse(SecurityType.Equity,
                "SecondUniverse",
                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>

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Log slice.Keys at the throw point to see which symbols are present and which are missing.
  2. Verify that both AAPL and SPY have complete daily data files for 2013-10-07 through 2013-10-11.
  3. Check that UniverseSettings.Resolution matches the AddEquity resolution so data timing aligns.
  4. If testing engine changes, trace the time-sync / fill-forward logic that merges multiple subscriptions into a single Slice.

Example fix

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

// after — add diagnostics to identify the missing security
if (slice.Count != 2)
{
    var symbols = string.Join(", ", slice.Keys.Select(s => s.Value));
    throw new RegressionTestException($"Unexpected data count: {slice.Count}. Symbols: {symbols}");
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate slice contents before asserting count
public override void OnData(Slice slice)
{
    if (slice.Count != 2)
    {
        Log($"Slice count mismatch at {Time}: {slice.Count}. Keys: {string.Join(", ", slice.Keys.Select(k => k.Value))}");
        return; // or handle gracefully
    }
}

Prevention

When it happens

Trigger: slice.Count differs from 2 because one security did not produce data on a given bar (missing data file, delisting, or data gap), or because an extra subscription leaked into the slice. The most common cause is a data-feed synchronization problem where one security lags or drops out.

Common situations: A data file for AAPL or SPY is missing or corrupted for the test date range, a LEAN version change altered fill-forward or subscription timing so the two securities don't align, or the universe selector intermittently returns empty causing SPY data to disappear.

Related errors


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