{"record":{"id":"c7930e5617caf74a","repo":"QuantConnect/Lean","slug":"unexpected-history-data-end-time","errorCode":null,"errorMessage":"Unexpected history data end time","messagePattern":"Unexpected history data end time","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/BasicTemplateIndexDailyAlgorithm.cs","lineNumber":91,"sourceCode":"            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>\n        /// This is used by the regression test system to indicate which languages this algorithm is written in.\n        /// </summary>\n        public override List<Language> Languages { get; } = new() { Language.CSharp, Language.Python };\n\n        /// <summary>\n        /// Data Points count of all timeslices of algorithm\n        /// </summary>\n        public override long DataPoints => 122;","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/BasicTemplateIndexDailyAlgorithm.cs#L73-L109","documentation":"Thrown when any of the 10 history bars for SpxOption or Spx has an EndTime.TimeOfDay that is not 15:15:00. Daily SPX bars should end at 15:15 US Eastern (market close). A mismatch signals the bar EndTime (close timestamp) was computed incorrectly by the data pipeline.","triggerScenarios":"DailyPreciseEndTime setting is not applied or was changed, the bar EndTime falls back to midnight-plus-one-day instead of exchange close, MarketHoursDatabase changed the SPX close time, or the data file stores the wrong close timestamp.","commonSituations":"Changes to Settings.DailyPreciseEndTime behavior in the engine, data normalization that defaults EndTime to the next midnight, timezone confusion between data storage and algorithm time, or modifications to how Lean derives EndTime from Time plus bar span.","solutions":["Confirm Settings.DailyPreciseEndTime = true is set in Initialize and that the engine honors it.","Check MarketHoursDatabase for the SPX market-close time to ensure it is 15:15.","Inspect raw data file EndTime fields and compare with what the history provider returns.","Log bad.EndTime with its .Kind to distinguish UTC from local time issues."],"exampleFix":"// before\nif (history.Any(x => x.EndTime.TimeOfDay != new TimeSpan(15, 15, 0)))\n{\n    throw new RegressionTestException($\"Unexpected history data end time\");\n}\n\n// after — identify the specific bar\nvar bad = history.FirstOrDefault(x => x.EndTime.TimeOfDay != new TimeSpan(15, 15, 0));\nif (bad != null)\n{\n    throw new RegressionTestException($\"Unexpected history end time: {bad.EndTime} (Kind={bad.EndTime.Kind})\");\n}","handlingStrategy":"validation","validationCode":"// Validate history bar end times with diagnostics\nvar history = History(symbol, 10).ToList();\nvar expectedEnd = new TimeSpan(15, 15, 0);\nvar badBar = history.FirstOrDefault(x => x.EndTime.TimeOfDay != expectedEnd);\nif (badBar != null)\n{\n    Log($\"Bad end time: {badBar.EndTime} (kind={badBar.EndTime.Kind}) for {symbol}\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure Settings.DailyPreciseEndTime = true when testing daily bar close times.","Verify MarketHoursDatabase market-close time matches expectations.","Log EndTime.Kind to distinguish UTC from exchange-local.","Check data pipeline bar-construction logic after engine updates."],"tags":["quantconnect","history","timestamp","market-hours","daily-precise-end-time","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}