{"record":{"id":"94efe35084aaecf3","repo":"QuantConnect/Lean","slug":"unexpected-data-count-slice-count","errorCode":null,"errorMessage":"Unexpected data count: {slice.Count}","messagePattern":"Unexpected data count: (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/CustomUniverseSelectionRegressionAlgorithm.cs","lineNumber":58,"sourceCode":"\n            UniverseSettings.Resolution = Resolution.Daily;\n            AddUniverse(SecurityType.Equity,\n                \"SecondUniverse\",\n                Resolution.Daily,\n                Market.USA,\n                UniverseSettings,\n                time => new[] { \"SPY\" });\n        }\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            if (slice.Count != 2)\n            {\n                throw new RegressionTestException($\"Unexpected data count: {slice.Count}\");\n            }\n            if (ActiveSecurities.Count != 2)\n            {\n                throw new RegressionTestException($\"Unexpected ActiveSecurities count: {ActiveSecurities.Count}\");\n            }\n            if (!Portfolio.Invested)\n            {\n                SetHoldings(Securities.Keys.First(symbol => symbol.Value == \"SPY\"), 1);\n                Debug(\"Purchased Stock\");\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 bool CanRunLocally { get; } = true;\n\n        /// <summary>","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/CustomUniverseSelectionRegressionAlgorithm.cs#L40-L76","documentation":"This RegressionTestException is thrown in OnData of a regression algorithm that adds AAPL via AddEquity and SPY via a custom universe selector, both at Daily resolution. It asserts slice.Count == 2, meaning exactly two securities produced data in that time slice. LEAN throws it to verify that manually-added securities and universe-selected securities start delivering data simultaneously.","triggerScenarios":"slice.Count differs from 2 because one security did not produce data on a given bar (missing data file, delisting, or data gap), or because an extra subscription leaked into the slice. The most common cause is a data-feed synchronization problem where one security lags or drops out.","commonSituations":"A data file for AAPL or SPY is missing or corrupted for the test date range, a LEAN version change altered fill-forward or subscription timing so the two securities don't align, or the universe selector intermittently returns empty causing SPY data to disappear.","solutions":["Log slice.Keys at the throw point to see which symbols are present and which are missing.","Verify that both AAPL and SPY have complete daily data files for 2013-10-07 through 2013-10-11.","Check that UniverseSettings.Resolution matches the AddEquity resolution so data timing aligns.","If testing engine changes, trace the time-sync / fill-forward logic that merges multiple subscriptions into a single Slice."],"exampleFix":"// before\nif (slice.Count != 2)\n{\n    throw new RegressionTestException($\"Unexpected data count: {slice.Count}\");\n}\n\n// after — add diagnostics to identify the missing security\nif (slice.Count != 2)\n{\n    var symbols = string.Join(\", \", slice.Keys.Select(s => s.Value));\n    throw new RegressionTestException($\"Unexpected data count: {slice.Count}. Symbols: {symbols}\");\n}","handlingStrategy":"validation","validationCode":"// Validate slice contents before asserting count\npublic override void OnData(Slice slice)\n{\n    if (slice.Count != 2)\n    {\n        Log($\"Slice count mismatch at {Time}: {slice.Count}. Keys: {string.Join(\", \", slice.Keys.Select(k => k.Value))}\");\n        return; // or handle gracefully\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure all securities share the same resolution so data timing aligns in each slice.","Verify data files are complete for all subscribed securities before running.","Use fill-forward enabled (default) so missing bars don't reduce slice.Count."],"tags":["quantconnect","lean","ondata","slice","regression-test","data-sync"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}