{"record":{"id":"6a3cb6719c9d8267","repo":"QuantConnect/Lean","slug":"benchmark-value-error-expected-previousbenchm","errorCode":null,"errorMessage":"Benchmark value error - expected: {_previousBenchmarkValue} {_previousTime}, actual: {currentValue} {slice.Time}. Benchmark value should only change when there is a change in hours","messagePattern":"Benchmark value error - expected: (.+?) (.+?), actual: (.+?) (.+?)\\. Benchmark value should only change when there is a change in hours","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/CustomUniverseWithBenchmarkRegressionAlgorithm.cs","lineNumber":120,"sourceCode":"                {\n                    throw new RegressionTestException($\"Security Price error. Price should change every new hour\");\n                }\n                if (slice.Time.Minute != 0\n                    && _previousSecurityValue != security.Price\n                    && security.IsTradable)\n                {\n                    throw new RegressionTestException($\"Security Price error. Price should not change every minute\");\n                }\n            }\n            _previousSecurityValue = security.Price;\n\n            // assert benchmark updates only on date change\n            var currentValue = Benchmark.Evaluate(slice.Time);\n            if (_previousTime.Hour == slice.Time.Hour)\n            {\n                if (currentValue != _previousBenchmarkValue)\n                {\n                    throw new RegressionTestException($\"Benchmark value error - expected: {_previousBenchmarkValue} {_previousTime}, actual: {currentValue} {slice.Time}. \" +\n                                        \"Benchmark value should only change when there is a change in hours\");\n                }\n            }\n            else\n            {\n                if (slice.Time.Minute == 0)\n                {\n                    if (currentValue == _previousBenchmarkValue)\n                    {\n                        _benchmarkPriceDidNotChange++;\n                        // there are two consecutive equal data points so we give it some room\n                        if (_benchmarkPriceDidNotChange > 1)\n                        {\n                            throw new RegressionTestException($\"Benchmark value error - expected a new value, current {currentValue} {slice.Time}\" +\n                                                \"Benchmark value should change when there is a change in hours\");\n                        }\n                    }\n                    else","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/CustomUniverseWithBenchmarkRegressionAlgorithm.cs#L102-L138","documentation":"This RegressionTestException is thrown in OnData when the current and previous slice share the same hour (_previousTime.Hour == slice.Time.Hour). It asserts that the benchmark value did NOT change within the same hour. LEAN throws it because the benchmark uses internal daily resolution via SetBenchmark(\"SPY\"), so its value should remain constant within each hour and only update on hour boundaries.","triggerScenarios":"Benchmark.Evaluate(slice.Time) returns a different value than _previousBenchmarkValue while both slices are in the same hour. This means the benchmark is updating intra-hour, which contradicts the internal daily-resolution benchmark data feed.","commonSituations":"A LEAN version change to benchmark evaluation logic caused it to use higher-resolution data, the benchmark subscription resolution was changed from daily to something finer, or Benchmark.Evaluate started sampling live prices instead of the cached daily value.","solutions":["Log currentValue, _previousBenchmarkValue, _previousTime, and slice.Time at the throw point.","Verify the benchmark subscription resolution is still daily (internal) and not overridden.","Check Benchmark.Evaluate implementation to confirm it returns a cached value within the same hour, not a fresh computation.","If testing engine changes, trace the benchmark data feed to ensure only daily-resolution bars update the benchmark value."],"exampleFix":"// before\nif (_previousTime.Hour == slice.Time.Hour)\n{\n    if (currentValue != _previousBenchmarkValue)\n    {\n        throw new RegressionTestException($\"Benchmark value error - expected: {_previousBenchmarkValue} {_previousTime}, actual: {currentValue} {slice.Time}. Benchmark value should only change when there is a change in hours\");\n    }\n}\n\n// after — same check, better diagnostics\nif (_previousTime.Hour == slice.Time.Hour && currentValue != _previousBenchmarkValue)\n{\n    throw new RegressionTestException($\"Intra-hour benchmark change at {slice.Time}: prev={_previousBenchmarkValue} ({_previousTime}), current={currentValue}\");\n}","handlingStrategy":"validation","validationCode":"// Validate benchmark stability within same hour\nvar currentValue = Benchmark.Evaluate(slice.Time);\nif (_previousTime.Hour == slice.Time.Hour && currentValue != _previousBenchmarkValue)\n    Log($\"Benchmark changed intra-hour at {slice.Time}: prev={_previousBenchmarkValue}, current={currentValue}\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify the benchmark subscription uses daily resolution (internal default for SetBenchmark with a ticker).","Confirm Benchmark.Evaluate returns a cached value within the same hour.","Trace the benchmark data feed when modifying engine subscription logic."],"tags":["quantconnect","lean","benchmark","resolution","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}