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

  1. Compare BarCounter value against the actual trading calendar for the SetStartDate/SetEndDate window and update ExpectedBarCount if holidays changed.
  2. Verify daily SPX data files exist for every expected trading day in the regression data folder.
  3. Check MarketHoursDatabase entries for Market.USA and SecurityType.Index to confirm no schedule changes.
  4. 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

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


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