{"record":{"id":"68b52c6aaba0a12b","repo":"QuantConnect/Lean","slug":"unexpected-history-count-history-count","errorCode":null,"errorMessage":"Unexpected history count: {history.Count}","messagePattern":"Unexpected history count: (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/BasicTemplateIndexDailyAlgorithm.cs","lineNumber":83,"sourceCode":"            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)\n                {\n                    throw new RegressionTestException($\"Unexpected history count: {history.Count}\");\n                }\n                if (history.Any(x => x.Time.TimeOfDay != new TimeSpan(8, 30, 0)))\n                {\n                    throw new RegressionTestException($\"Unexpected history data start time\");\n                }\n                if (history.Any(x => x.EndTime.TimeOfDay != new TimeSpan(15, 15, 0)))\n                {\n                    throw new RegressionTestException($\"Unexpected history data end time\");\n                }\n            }\n        }\n\n        /// <summary>\n        /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.\n        /// </summary>\n        public override bool CanRunLocally { get; } = true;\n\n        /// <summary>","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/BasicTemplateIndexDailyAlgorithm.cs#L65-L101","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar history = History(symbol, 10).ToList();\nif (history.Count != 10)\n{\n    throw new RegressionTestException($\"Unexpected history count: {history.Count}\");\n}\n\n// after — log missing dates for diagnosis\nvar history = History(symbol, 10).ToList();\nif (history.Count != 10)\n{\n    var dates = string.Join(\", \", history.Select(x => x.Time.ToString(\"yyyy-MM-dd\")));\n    throw new RegressionTestException($\"Unexpected history count: {history.Count}. Dates: [{dates}]\");\n}","handlingStrategy":"validation","validationCode":"// Validate history count with diagnostic logging\nvar history = History(symbol, 10).ToList();\nif (history.Count != 10)\n{\n    var dates = history.Select(x => x.Time.ToString(\"yyyy-MM-dd\"));\n    Log($\"History for {symbol}: count={history.Count}, dates=[{string.Join(\", \", dates)}]\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["quantconnect","history","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"}