{"record":{"id":"54df94b56b61ba69","repo":"QuantConnect/Lean","slug":"expected-a-single-subscription-for-the-underlying","errorCode":null,"errorMessage":"Expected a single subscription for the underlying index security","messagePattern":"Expected a single subscription for the underlying index security","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/DuplicatedIndexOptionSubscriptionRegressionAlgorithm.cs","lineNumber":62,"sourceCode":"\n            var spxOption = QuantConnect.Symbol.CreateOption(\n                spx,\n                Market.USA,\n                OptionStyle.European,\n                OptionRight.Call,\n                3200m,\n                new DateTime(2021, 1, 15));\n\n            AddIndexOptionContract(spxOption, Resolution.Minute);\n\n            if (SubscriptionManager.Subscriptions.Count() < 2)\n            {\n                throw new RegressionTestException(\"Expected subscriptions for the added index option contract\");\n            }\n\n            if (SubscriptionManager.Subscriptions.Count(x => x.Symbol == spx) != 1)\n            {\n                throw new RegressionTestException(\"Expected a single subscription for the underlying index security\");\n            }\n\n            // Quit early, we already tested what we wanted\n            Quit();\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 virtual 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 virtual List<Language> Languages { get; } = new() { Language.CSharp };\n\n        /// <summary>\n        /// Data Points count of all timeslices of algorithm","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/DuplicatedIndexOptionSubscriptionRegressionAlgorithm.cs#L44-L80","documentation":"Asserts exactly one subscription exists whose Symbol == spx (the underlying index). This is the core anti-duplication invariant of the test: adding an index option contract must NOT create a second subscription for the underlying index. A count != 1 means the underlying got duplicated (the bug the test exists to catch).","triggerScenarios":"AddIndexOptionContract internally calls AddIndex for the underlying again, creating a duplicate spx subscription. This is exactly the regression the test guards against.","commonSituations":"A change to how index-option contracts resolve their underlying: if it re-adds the underlying instead of reusing the existing subscription, duplication occurs. Market/canonical resolution logic producing a different SID for the same logical index.","solutions":["Inspect all subscriptions matching spx and compare their SIDs/configs to find the duplicate.","In AddIndexOptionContract, ensure the underlying index security is reused (resolved by SID) rather than re-added.","Add a guard in the SubscriptionManager that rejects adding an identical existing subscription."],"exampleFix":"// before\nif (SubscriptionManager.Subscriptions.Count(x => x.Symbol == spx) != 1)\n{\n    throw new RegressionTestException(\"Expected a single subscription for the underlying index security\");\n}\n\n// after (surface the duplicates for diagnosis)\nvar spxSubs = SubscriptionManager.Subscriptions.Where(x => x.Symbol == spx).ToList();\nif (spxSubs.Count != 1)\n{\n    throw new RegressionTestException(\n        $\"Expected a single subscription for the underlying index security, got {spxSubs.Count}: {string.Join(\", \", spxSubs.Select(s => s.Configuration))}\");\n}","handlingStrategy":"validation","validationCode":"var spxSubs = SubscriptionManager.Subscriptions.Where(s => s.Symbol == spx).Select(s => s.Configuration).ToList();\nif (spxSubs.Count != 1)\n{\n    foreach (var c in spxSubs) Log($\"Duplicate underlying subscription: {c}\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Reuse the existing underlying security by SID in AddIndexOptionContract instead of re-adding it.","Add a SubscriptionManager guard that rejects identical existing subscriptions.","Include duplicate configs in the assertion message for fast triage."],"tags":["quantconnect","lean","regression-test","subscriptions","index-options","deduplication","regression-guard"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}