QuantConnect/Lean · error · RegressionTestException
Unexpected history count: {history.Count}
Error message
Unexpected history count: {history.Count} What it means
Thrown when a History(symbol, 10) call for either SpxOption or Spx returns fewer or more than 10 daily bars. This verifies the Lean history service correctly retrieves and assembles the requested number of daily-resolution data points from the data store.
Source
Thrown at Algorithm.CSharp/BasicTemplateIndexDailyAlgorithm.cs:83
AssertIndicators();
if (Resolution != Resolution.Daily)
{
return;
}
var openInterest = Securities[SpxOption].Cache.GetAll<OpenInterest>();
if (openInterest.Single().EndTime != new DateTime(2021, 1, 15, 15, 15, 0))
{
throw new ArgumentException($"Unexpected open interest time: {openInterest.Single().EndTime}");
}
foreach (var symbol in new[] { SpxOption, Spx })
{
var history = History(symbol, 10).ToList();
if (history.Count != 10)
{
throw new RegressionTestException($"Unexpected history count: {history.Count}");
}
if (history.Any(x => x.Time.TimeOfDay != new TimeSpan(8, 30, 0)))
{
throw new RegressionTestException($"Unexpected history data start time");
}
if (history.Any(x => x.EndTime.TimeOfDay != new TimeSpan(15, 15, 0)))
{
throw new RegressionTestException($"Unexpected history data end time");
}
}
}
/// <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 override bool CanRunLocally { get; } = true;
/// <summary>View on GitHub (pinned to d2c3659f87)
Solutions
- Ensure the backtest date range covers at least 10 trading days before the end date so History(10) can fill.
- Verify SPX and SpxOption daily data files exist and contain entries for all expected trading days.
- Debug-log history.Select(x => x.Time).ToArray() to see exactly which dates are returned.
- Check the HistoryProvider implementation for changes to lookback or bar-count resolution.
Example fix
// before
var history = History(symbol, 10).ToList();
if (history.Count != 10)
{
throw new RegressionTestException($"Unexpected history count: {history.Count}");
}
// after — log missing dates for diagnosis
var history = History(symbol, 10).ToList();
if (history.Count != 10)
{
var dates = string.Join(", ", history.Select(x => x.Time.ToString("yyyy-MM-dd")));
throw new RegressionTestException($"Unexpected history count: {history.Count}. Dates: [{dates}]");
} Defensive patterns
Strategy: validation
Validate before calling
// Validate history count with diagnostic logging
var history = History(symbol, 10).ToList();
if (history.Count != 10)
{
var dates = history.Select(x => x.Time.ToString("yyyy-MM-dd"));
Log($"History for {symbol}: count={history.Count}, dates=[{string.Join(", ", dates)}]");
} Prevention
- Ensure backtest window spans enough trading days for the history lookback.
- Verify data files exist for every day in the lookback period.
- Log history dates when counts mismatch to identify missing days.
- Test history requests in isolation before adding assertions.
When it happens
Trigger: Data files for SPX or SPX option are missing days within the 10-bar lookback window, the history request period boundary is off-by-one, the algorithm ran for fewer than 10 days, or the data reader skips corrupt or sparse entries.
Common situations: Regression data set updates that removed specific days, history-provider changes that alter bar aggregation, sparse-data filtering that drops incomplete bars, or backtest window too short to supply 10 history bars.
Related errors
- Bar Count {BarCounter} is not expected count of {ExpectedBar
- Unexpected open interest time: {openInterest.Single().EndTim
- Unexpected history data start time
- Unexpected history data end time
- _customWarmUp indicator was expected to be ready
AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13).
Data as JSON: /api/errors/68b52c6aaba0a12b.
Report an issue: GitHub.