{"record":{"id":"5f305bb21c122192","repo":"QuantConnect/Lean","slug":"universe-selection-should-have-been-triggered-righ","errorCode":null,"errorMessage":"Universe selection should have been triggered right away. The first OnData call should have had happened after the universe selection","messagePattern":"Universe selection should have been triggered right away\\. The first OnData call should have had happened after the universe selection","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/CustomUniverseImmediateSelectionRegressionAlgorithm.cs","lineNumber":67,"sourceCode":"            AddUniverse(SecurityType.Equity,\n                \"my-custom-universe\",\n                Resolution.Daily,\n                Market.USA,\n                UniverseSettings,\n                time =>\n                {\n                    _selected = true;\n                    return new[] { \"SPY\", \"GOOG\", \"APPL\" };\n                });\n        }\n\n        public override void OnData(Slice slice)\n        {\n            if (_firstOnData)\n            {\n                if (!_selected)\n                {\n                    throw new RegressionTestException(\"Universe selection should have been triggered right away. \" +\n                        \"The first OnData call should have had happened after the universe selection\");\n                }\n\n                _firstOnData = false;\n            }\n        }\n\n        public override void OnSecuritiesChanged(SecurityChanges changes)\n        {\n            if (!_selected)\n            {\n                throw new RegressionTestException(\"Universe selection should have been triggered right away\");\n            }\n\n            if (!_securitiesChanged)\n            {\n                // Selection should be happening right on algorithm start\n                if (Time != StartDate)","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/CustomUniverseImmediateSelectionRegressionAlgorithm.cs#L49-L85","documentation":"Asserts universe selection ran before the first OnData. _selected is set true inside the selection function; the first OnData checks it and throws if still false. The contract: a custom universe with a selection function must select on algorithm start so securities exist before data flows.","triggerScenarios":"OnData's first invocation occurs while _selected is false, meaning the selection delegate was not invoked during/after Initialize and before the first time slice reached the algorithm.","commonSituations":"Universe selection scheduled lazily (e.g., only on the first universe data point) instead of eagerly; a Lean change that deferred selection; or AddUniverse overload that does not trigger immediate selection at start.","solutions":["Use the AddUniverse overload that accepts a selection function and triggers selection at algorithm start (this test uses the SecurityType+market overload).","Confirm UniverseSettings.Resolution and the universe Resolution are set so selection is not waiting on a slower data trigger.","Verify the selection delegate returns symbols (non-empty) so selection actually completes and sets the flag.","If selection is deferred by design in a newer Lean version, force an initial selection via Schedule or a manual trigger."],"exampleFix":"// before: selection waits for first universe data point, OnData fires first\nAddUniverse(\"my-universe\", time => new[] { \"SPY\" });\n\n// after: use the overload that selects immediately at start\nAddUniverse(SecurityType.Equity, \"my-custom-universe\", Resolution.Daily, Market.USA, UniverseSettings,\n    time => { _selected = true; return new[] { \"SPY\", \"GOOG\", \"APPL\" }; });","handlingStrategy":"validation","validationCode":"// Force eager selection and assert it ran before first OnData\nAddUniverse(SecurityType.Equity, \"my-custom-universe\", Resolution.Daily, Market.USA, UniverseSettings,\n    time => { _selected = true; return new[] { \"SPY\", \"GOOG\", \"APPL\" }; });\n// in OnData first call: if (!_selected) Log(\"Selection not yet run\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the AddUniverse overload that selects at algorithm start.","Return a non-empty symbol list from the selector so selection completes.","Set universe Resolution explicitly to avoid deferred data-triggered selection."],"tags":["regression-test","universe-selection","custom-universe","ordering"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}