{"record":{"id":"1cb12cce4b7f81c2","repo":"QuantConnect/Lean","slug":"order-failure-history-i-endtime-history-i","errorCode":null,"errorMessage":"Order failure: {history[i].EndTime} > {history[i + 1].EndTime} at index {i}.","messagePattern":"Order failure: (.+?) > (.+?) at index (.+?)\\.","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/DescendingCustomDataObjectStoreRegressionAlgorithm.cs","lineNumber":111,"sourceCode":"        {\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>\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 bool CanRunLocally => 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 List<Language> Languages => new() { Language.CSharp, Language.Python };","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/DescendingCustomDataObjectStoreRegressionAlgorithm.cs#L93-L129","documentation":"After fetching history, the algorithm iterates consecutive pairs and asserts history[i].EndTime <= history[i+1].EndTime (ascending). A failure means the custom-data Sort=true flag did not re-order descending source data into ascending order in the history result.","triggerScenarios":"SortCustomData.GetSource sets Sort = true, but the history path does not honor it, so points arrive in the original descending order. Also fires if EndTime is computed incorrectly (e.g., defaults to Time instead of Time + period).","commonSituations":"A Lean regression where the history pipeline skips the Sort step that the subscription pipeline applies, or a custom data type that does not set EndTime correctly for bar-style data.","solutions":["Confirm GetSource sets Sort = true on the SubscriptionDataSource in all paths (subscription and history share it, but verify history invokes GetSource).","Check whether EndTime is overridden in SortCustomData; BaseData.EndTime defaults to Time, which may need overriding for the ordering to be meaningful.","If the Sort flag is not respected by history, fix the Lean history data-reading path to sort.","Log the EndTime sequence to confirm where the inversion occurs."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"for (int i = 0; i < history.Count - 1; i++)\n{\n    if (history[i].EndTime > history[i + 1].EndTime)\n    {\n        Debug($\"Inversion at {i}: {history[i].EndTime} > {history[i+1].EndTime}\");\n        // optionally re-sort as a safety net\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Confirm Sort = true is set on the SubscriptionDataSource in GetSource.","Override EndTime in custom data if the default (Time) is not the correct bar boundary.","Log the EndTime sequence to catch where sorting fails."],"tags":["quantconnect","custom-data","history","sorting","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}