{"record":{"id":"f8f63a52e17947a8","repo":"QuantConnect/Lean","slug":"bar-count-barcounter-is-not-expected-count-of-e","errorCode":null,"errorMessage":"Bar Count {BarCounter} is not expected count of {ExpectedBarCount}","messagePattern":"Bar Count (.+?) is not expected count of (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/BasicTemplateIndexDailyAlgorithm.cs","lineNumber":63,"sourceCode":"                MarketOrder(SpxOption, 1);\n            }\n            else\n            {\n                Liquidate();\n            }\n\n            // Count how many slices we receive with SPX data in it to assert later\n            if (slice.ContainsKey(Spx))\n            {\n                BarCounter++;\n            }\n        }\n\n        public override void OnEndOfAlgorithm()\n        {\n            if (BarCounter != ExpectedBarCount)\n            {\n                throw new ArgumentException($\"Bar Count {BarCounter} is not expected count of {ExpectedBarCount}\");\n            }\n            AssertIndicators();\n\n            if (Resolution != Resolution.Daily)\n            {\n                return;\n            }\n\n            var openInterest = Securities[SpxOption].Cache.GetAll<OpenInterest>();\n            if (openInterest.Single().EndTime != new DateTime(2021, 1, 15, 15, 15, 0))\n            {\n                throw new ArgumentException($\"Unexpected open interest time: {openInterest.Single().EndTime}\");\n            }\n\n            foreach (var symbol in new[] { SpxOption, Spx })\n            {\n                var history = History(symbol, 10).ToList();\n                if (history.Count != 10)","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/BasicTemplateIndexDailyAlgorithm.cs#L45-L81","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nprotected virtual int ExpectedBarCount => 2 * 5; // 10\n// date range changed but count not updated\n\n// after — recount based on actual trading days in new range\nprotected virtual int ExpectedBarCount => TradingCalendar.GetTradingDays(\n    new DateTime(2021, 1, 1), new DateTime(2021, 1, 15), Market.USA).Count();","handlingStrategy":"validation","validationCode":"// Log bar count throughout the algorithm for early detection\npublic override void OnEndOfAlgorithm()\n{\n    if (BarCounter != ExpectedBarCount)\n    {\n        Log($\"Bar count mismatch: got {BarCounter}, expected {ExpectedBarCount}\");\n        // Use a soft assert in non-regression runs\n        Debug($\"Trading days may have changed — verify ExpectedBarCount\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["quantconnect","daily-data","bar-count","data-pipeline","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}