{"record":{"id":"363372c5cf220c2b","repo":"QuantConnect/Lean","slug":"did-not-receive-expected-security-changes-removal","errorCode":null,"errorMessage":"Did not receive expected security changes removal! Got {_receivedSecurityChangesEvent}","messagePattern":"Did not receive expected security changes removal! Got (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/DelistingEventsAlgorithm.cs","lineNumber":139,"sourceCode":"        }\n\n        public override void OnEndOfAlgorithm()\n        {\n            if (!_receivedDelistedEvent)\n            {\n                throw new RegressionTestException(\"Did not receive expected delisted event\");\n            }\n            if (!_receivedDelistedWarningEvent)\n            {\n                throw new RegressionTestException(\"Did not receive expected delisted warning event\");\n            }\n            if (_dataCount != 13)\n            {\n                throw new RegressionTestException($\"Unexpected data count {_dataCount}. Expected 13\");\n            }\n            if (_receivedSecurityChangesEvent != 1)\n            {\n                throw new RegressionTestException($\"Did not receive expected security changes removal! Got {_receivedSecurityChangesEvent}\");\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>\n        /// This is used by the regression test system to indicate which languages this algorithm is written in.\n        /// </summary>\n        public List<Language> Languages { get; } = new() { Language.CSharp, Language.Python };\n\n        /// <summary>\n        /// Data Points count of all timeslices of algorithm\n        /// </summary>\n        public long DataPoints => 87;\n","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/DelistingEventsAlgorithm.cs#L121-L157","documentation":"OnSecuritiesChanged increments _receivedSecurityChangesEvent only when AAA.1 appears in changes.RemovedSecurities. OnEndOfAlgorithm asserts it equals exactly 1. A value of 0 means the delisted security was never removed from the universe; a value >1 means it was removed more than once, indicating a universe-selection bug.","triggerScenarios":"AAA.1 is delisted but the universe manager does not emit a removal, or emits duplicate removal SecurityChanges events. Also fires if the symbol comparison removedSecurity.Symbol.Value == \"AAA.1\" never matches due to a symbol-mapping or ticker change.","commonSituations":"Lean version change in how delisted securities are removed from the universe, symbol mapping (mapped vs raw symbol value), or a data issue where the delisting event is not processed so the security is never dropped.","solutions":["Log every change in OnSecuritiesChanged (added + removed symbols) to see whether AAA.1 is present and how many times.","Verify the delisting event for AAA.1 is in the data and is processed before algorithm end.","If duplicate removals are a Lean regression, fix the universe removal deduplication logic rather than relaxing the assert.","Confirm the symbol value comparison uses the same form (Value vs mapped Value) as the data provider."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"public override void OnSecuritiesChanged(SecurityChanges changes)\n{\n    foreach (var removed in changes.RemovedSecurities)\n    {\n        if (removed.Symbol.Value == \"AAA.1\")\n        {\n            if (_receivedSecurityChangesEvent >= 1)\n                Debug($\"WARNING: duplicate removal of AAA.1\");\n            _receivedSecurityChangesEvent++;\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Log all SecurityChanges (added/removed) to detect duplicate or missing removals.","Compare symbols by SID when mapping is involved.","Verify delisting events are in the data so removals actually fire."],"tags":["quantconnect","delisting","universe-selection","security-changes","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}