QuantConnect/Lean · error · RegressionTestException

Unexpected data count {_dataCount}. Expected 13

Error message

Unexpected data count {_dataCount}. Expected 13

What it means

Asserted in OnEndOfAlgorithm of a delisting regression that trades AAA.1 and SPY at daily resolution from 2007-05-15 to 2007-05-25. _dataCount accumulates slice.Bars.Count across every OnData call and must equal exactly 13. A mismatch signals that the data feed delivered a different number of trade bars than the fixture expects (e.g., delisting/removed bars, missing data files, or a changed date range).

Source

Thrown at Algorithm.CSharp/DelistingEventsAlgorithm.cs:135

                {
                    _receivedSecurityChangesEvent++;
                }
            }
        }

        public override void OnEndOfAlgorithm()
        {
            if (!_receivedDelistedEvent)
            {
                throw new RegressionTestException("Did not receive expected delisted event");
            }
            if (!_receivedDelistedWarningEvent)
            {
                throw new RegressionTestException("Did not receive expected delisted warning event");
            }
            if (_dataCount != 13)
            {
                throw new RegressionTestException($"Unexpected data count {_dataCount}. Expected 13");
            }
            if (_receivedSecurityChangesEvent != 1)
            {
                throw new RegressionTestException($"Did not receive expected security changes removal! Got {_receivedSecurityChangesEvent}");
            }
        }

        /// <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, Language.Python };

        /// <summary>

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Confirm the AAA.1 and SPY daily factor files exist under the data folder for 2007-05-15 through 2007-05-25.
  2. Run with the exact dates from Initialize (2007-05-15 to 2007-05-25) — do not modify SetStartDate/SetEndDate without recalculating the expected count.
  3. If the count legitimately changed due to a Lean data-handling fix, update the literal 13 to the new verified value and document why.
  4. Temporarily log _dataCount per OnData call to identify which date contributes an unexpected bar.
Defensive patterns

Strategy: validation

Validate before calling

// Track and assert data count with a descriptive message
if (_dataCount != 13)
{
    throw new InvalidOperationException(
        $"Data count {_dataCount} != 13; check AAA.1/SPY daily data for the backtest window");
}

Prevention

When it happens

Trigger: Running the regression when the AAA.1 or SPY daily data for the 2007-05-15..2007-05-25 window is missing, corrupted, or augmented with extra bars. Any change to SetStartDate/SetEndDate or AddSecurity resolution also shifts the accumulated count.

Common situations: Local data store missing the specific delisting-period files for AAA.1, a Lean data-reading change that adds/removes a bar (e.g., handling of the delisting day itself), or editing the date range without updating the expected literal 13.

Related errors


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