{"record":{"id":"2b41bab09aae11a9","repo":"QuantConnect/Lean","slug":"unexpected-bar-error","errorCode":null,"errorMessage":"Unexpected Bar error","messagePattern":"Unexpected Bar error","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/CustomUniverseWithBenchmarkRegressionAlgorithm.cs","lineNumber":90,"sourceCode":"        }\n\n        /// <summary>\n        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.\n        /// </summary>\n        /// <param name=\"slice\">Slice object keyed by symbol containing the stock data</param>\n        public override void OnData(Slice slice)\n        {\n            var security = Securities[_spy];\n            _onDataWasCalled = true;\n\n            var bar = slice.Bars.Values.Single();\n            if (_universeSelected)\n            {\n                if (bar.IsFillForward\n                    || bar.Period != TimeSpan.FromMinutes(1))\n                {\n                    // bar should always be the Minute resolution one here\n                    throw new RegressionTestException(\"Unexpected Bar error\");\n                }\n                if (_previousTime.Date == slice.Time.Date\n                    && (slice.Time - _previousTime) != TimeSpan.FromMinutes(1))\n                {\n                    throw new RegressionTestException(\"For the same date expected data updates every 1 minute\");\n                }\n            }\n            else\n            {\n                if (slice.Time.Minute == 0\n                    && _previousSecurityValue == security.Price)\n                {\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                {","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/CustomUniverseWithBenchmarkRegressionAlgorithm.cs#L72-L108","documentation":"This RegressionTestException is thrown in OnData when the universe is selected (_universeSelected == true). It asserts that the single bar in slice.Bars is NOT a fill-forward bar and has a period of exactly 1 minute. LEAN throws it because when the universe selects SPY at Minute resolution while the AddEquity subscription is at Hour resolution, the universe-driven data should deliver real (non-fill-forward) Minute bars.","triggerScenarios":"bar.IsFillForward is true (the engine filled forward a stale bar instead of delivering real minute data), or bar.Period != 1 minute (the resolution was not correctly upgraded from Hour to Minute when the universe selected the security). Both indicate the universe subscription's resolution override isn't working.","commonSituations":"A LEAN version change to subscription resolution merging logic caused the minute-resolution universe subscription to be overridden by the hour-resolution AddEquity subscription. Also occurs when fill-forward logic changed to be more aggressive, or when universe-selection data-feed configuration was refactored.","solutions":["Log bar.IsFillForward and bar.Period at the throw point to determine which condition failed.","Trace the subscription data-feed configuration to confirm the universe's Minute resolution is not being collapsed to Hour by the existing AddEquity(Hour) subscription.","Check that fill-forward only triggers on actual market gaps, not due to missing data files.","If testing engine changes, verify the SubscriptionDataConfig priority/merge logic preserves the highest-resolution subscription."],"exampleFix":"// before\nif (bar.IsFillForward || bar.Period != TimeSpan.FromMinutes(1))\n{\n    throw new RegressionTestException(\"Unexpected Bar error\");\n}\n\n// after — split conditions for diagnosis\nif (bar.IsFillForward)\n    throw new RegressionTestException($\"Bar was fill-forward, expected real data. Time: {slice.Time}\");\nif (bar.Period != TimeSpan.FromMinutes(1))\n    throw new RegressionTestException($\"Bar period was {bar.Period}, expected 1 minute. Time: {slice.Time}\");","handlingStrategy":"validation","validationCode":"// Validate bar properties before asserting\nvar bar = slice.Bars.Values.Single();\nif (bar.IsFillForward)\n    Log($\"Fill-forward bar at {slice.Time} — check data availability\");\nif (bar.Period != TimeSpan.FromMinutes(1))\n    Log($\"Bar period {bar.Period} — resolution may not be Minute\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When mixing AddEquity(Hour) with a Minute-resolution universe, verify the engine preserves the higher resolution for universe-selected data.","Check data files exist at the expected resolution to avoid fill-forward.","Log bar.IsFillForward during development to detect data gaps."],"tags":["quantconnect","lean","bars","resolution","fill-forward","universe-selection","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}