{"record":{"id":"cc9239b89ea84d19","repo":"QuantConnect/Lean","slug":"history-request-returned-different-data-than-expec","errorCode":null,"errorMessage":"History request returned different data than expected","messagePattern":"History request returned different data than expected","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/DescendingCustomDataObjectStoreRegressionAlgorithm.cs","lineNumber":103,"sourceCode":"                    throw new RegressionTestException(\"One or more custom data fields (Open, High, Low, Close, Price) are zero.\");\n                }\n\n                _receivedData.Add(sortCustomData);\n            }\n        }\n\n        public override void OnEndOfAlgorithm()\n        {\n            if (_receivedData.Count == 0)\n            {\n                throw new RegressionTestException(\"Custom data was not fetched\");\n            }\n\n            var history = History<SortCustomData>(_customSymbol, StartDate, EndDate, Resolution.Hour).ToList();\n\n            if (history.Count != _receivedData.Count)\n            {\n                throw new RegressionTestException(\"History request returned different data than expected\");\n            }\n\n            // Iterate through the history collection, checking if the EndTime is in ascending order.\n            for (int i = 0; i < history.Count - 1; i++)\n            {\n                if (history[i].EndTime > history[i + 1].EndTime)\n                {\n                    throw new RegressionTestException($\"Order failure: {history[i].EndTime} > {history[i + 1].EndTime} at index {i}.\");\n                }\n            }\n        }\n\n        /// <summary>\n        /// Final status of the algorithm\n        /// </summary>\n        public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed;\n\n        /// <summary>","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/DescendingCustomDataObjectStoreRegressionAlgorithm.cs#L85-L121","documentation":"OnEndOfAlgorithm requests History<SortCustomData>(_customSymbol, StartDate, EndDate, Resolution.Hour) and asserts history.Count equals _receivedData.Count (the number of points delivered live in OnData). A mismatch means the history API returns a different dataset than the live subscription for the same symbol and range.","triggerScenarios":"The history request uses Resolution.Hour while the live subscription used Daily (Resolution.Daily in AddData), causing a different aggregation/count. Also fires if history filters out data that OnData included, or if the Sort flag behaves differently between live and history paths.","commonSituations":"Resolution mismatch between AddData (Daily) and the History call (Hour), a Lean history-vs-subscription inconsistency for custom data with Sort=true, or timezone/boundary differences excluding edge points.","solutions":["Align the History call resolution with the subscription: use Resolution.Daily to match AddData<SortCustomData>(..., Resolution.Daily).","Log both counts and a few sample timestamps from each to identify which points are excluded.","If the discrepancy is a Lean bug in history for sorted custom data, fix the history reader to apply the same Sort logic as the subscription.","Verify StartDate/EndDate boundaries are inclusive on both paths."],"exampleFix":"// before\nvar history = History<SortCustomData>(_customSymbol, StartDate, EndDate, Resolution.Hour).ToList();\n\n// after — match the subscription resolution\nvar history = History<SortCustomData>(_customSymbol, StartDate, EndDate, Resolution.Daily).ToList();","handlingStrategy":"validation","validationCode":"// Use the same resolution as the subscription\nvar history = History<SortCustomData>(_customSymbol, StartDate, EndDate, Resolution.Daily).ToList();\nif (history.Count != _receivedData.Count)\n{\n    Debug($\"History={history.Count} vs Received={_receivedData.Count}\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Match the History call resolution to the AddData subscription resolution.","Log both counts and sample timestamps to localize discrepancies.","Verify inclusive date boundaries on both subscription and history paths."],"tags":["quantconnect","custom-data","history","resolution","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}