QuantConnect/Lean · error · ArgumentException
Bar Count {BarCounter} is not expected count of {ExpectedBar
Error message
Bar Count {BarCounter} is not expected count of {ExpectedBarCount} What it means
Thrown in OnEndOfAlgorithm when BarCounter (incremented each time a slice contains SPX data) does not equal ExpectedBarCount (10 = two 5-day trading weeks). This assertion verifies that the daily-resolution data pipeline delivers exactly the expected number of market-day slices for the configured backtest window.
Source
Thrown at Algorithm.CSharp/BasicTemplateIndexDailyAlgorithm.cs:63
MarketOrder(SpxOption, 1);
}
else
{
Liquidate();
}
// Count how many slices we receive with SPX data in it to assert later
if (slice.ContainsKey(Spx))
{
BarCounter++;
}
}
public override void OnEndOfAlgorithm()
{
if (BarCounter != ExpectedBarCount)
{
throw new ArgumentException($"Bar Count {BarCounter} is not expected count of {ExpectedBarCount}");
}
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)View on GitHub (pinned to d2c3659f87)
Solutions
- Compare BarCounter value against the actual trading calendar for the SetStartDate/SetEndDate window and update ExpectedBarCount if holidays changed.
- Verify daily SPX data files exist for every expected trading day in the regression data folder.
- Check MarketHoursDatabase entries for Market.USA and SecurityType.Index to confirm no schedule changes.
- Log each slice.Date in OnData to identify which specific days are missing or extra.
Example fix
// before
protected virtual int ExpectedBarCount => 2 * 5; // 10
// date range changed but count not updated
// after — recount based on actual trading days in new range
protected virtual int ExpectedBarCount => TradingCalendar.GetTradingDays(
new DateTime(2021, 1, 1), new DateTime(2021, 1, 15), Market.USA).Count(); Defensive patterns
Strategy: validation
Validate before calling
// Log bar count throughout the algorithm for early detection
public override void OnEndOfAlgorithm()
{
if (BarCounter != ExpectedBarCount)
{
Log($"Bar count mismatch: got {BarCounter}, expected {ExpectedBarCount}");
// Use a soft assert in non-regression runs
Debug($"Trading days may have changed — verify ExpectedBarCount");
}
} Prevention
- Compute ExpectedBarCount dynamically from the trading calendar rather than hardcoding.
- Log each slice date to maintain a trace of received bars.
- Update ExpectedBarCount whenever the date range or market calendar changes.
- Use QuantConnect's trading-calendar API to count expected days.
When it happens
Trigger: The date range includes or excludes holidays/weekends differently than expected, daily data files are missing for one or more trading days, the algorithm start/end dates were modified without updating ExpectedBarCount, or a market-hours/calendar change altered the number of valid trading days.
Common situations: Holiday data corrections in the regression dataset, changes to US market-hours definitions in MarketHoursDatabase, date range edits that don't update ExpectedBarCount, or data normalization changes that drop or duplicate daily bars.
Related errors
- Unexpected history count: {history.Count}
- Unexpected open interest time: {openInterest.Single().EndTim
- Indicators are not ready!
- Index is not tradable.
- Unexpected history data start time
AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13).
Data as JSON: /api/errors/f8f63a52e17947a8.
Report an issue: GitHub.