{"record":{"id":"7da00c9e703b0ea3","repo":"QuantConnect/Lean","slug":"universe-selection-should-have-been-triggered-righ-7da00c","errorCode":null,"errorMessage":"Universe selection should have been triggered right away","messagePattern":"Universe selection should have been triggered right away","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/CustomUniverseImmediateSelectionRegressionAlgorithm.cs","lineNumber":79,"sourceCode":"        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)\n                {\n                    throw new RegressionTestException(\"Universe selection should have been triggered right away\");\n                }\n\n                if (changes.AddedSecurities.Count != ExpectedSymbols.Count)\n                {\n                    throw new RegressionTestException($\"Expected {ExpectedSymbols.Count} stocks to be added to the algorithm, \" +\n                        $\"but found {changes.AddedSecurities.Count}\");\n                }\n\n                if (!ExpectedSymbols.All(x => changes.AddedSecurities.Any(security => security.Symbol == x)))\n                {","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/CustomUniverseImmediateSelectionRegressionAlgorithm.cs#L61-L97","documentation":"Asserts the selection function ran before the first OnSecuritiesChanged callback. If OnSecuritiesChanged fires while _selected is false, the securities-changed event was raised without the selection delegate having executed, violating the 'select first, then notify' ordering.","triggerScenarios":"OnSecuritiesChanged is invoked (e.g., from initial security seeding) before the universe's selection delegate sets _selected=true, so the callback sees the flag still false.","commonSituations":"Initial seeding of the universe's configured securities triggers OnSecuritiesChanged ahead of the first selection pass; or selection is scheduled after the first changed event in the lifecycle.","solutions":["Ensure the selection function executes synchronously during Initialize/first algorithm time so _selected is true before any OnSecuritiesChanged.","Avoid seeding securities into the universe that would raise changed events prior to selection.","Use the immediate-selection AddUniverse overload that runs the selector at start.","Re-order lifecycle hooks so selection precedes security-change notification."],"exampleFix":"// before: seeding raises OnSecuritiesChanged before selection runs\nAddUniverse(seededSymbols, selector);\n\n// after: no pre-seed; selector runs first 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":"// Track selection state and assert ordering in callbacks\nif (!_selected)\n    Log($\"OnSecuritiesChanged fired before selection at {Time}\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run the selector synchronously at start so _selected is true before changed events.","Avoid seeding universe securities that raise changed events pre-selection.","Pin the AddUniverse overload that guarantees select-then-notify ordering."],"tags":["regression-test","universe-selection","securities-changed","ordering"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}