QuantConnect/Lean · error · RegressionTestException

Expected a single subscription for the underlying index secu

Error message

Expected a single subscription for the underlying index security

What it means

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).

Source

Thrown at Algorithm.CSharp/DuplicatedIndexOptionSubscriptionRegressionAlgorithm.cs:62

            var spxOption = QuantConnect.Symbol.CreateOption(
                spx,
                Market.USA,
                OptionStyle.European,
                OptionRight.Call,
                3200m,
                new DateTime(2021, 1, 15));

            AddIndexOptionContract(spxOption, Resolution.Minute);

            if (SubscriptionManager.Subscriptions.Count() < 2)
            {
                throw new RegressionTestException("Expected subscriptions for the added index option contract");
            }

            if (SubscriptionManager.Subscriptions.Count(x => x.Symbol == spx) != 1)
            {
                throw new RegressionTestException("Expected a single subscription for the underlying index security");
            }

            // Quit early, we already tested what we wanted
            Quit();
        }

        /// <summary>
        /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
        /// </summary>
        public virtual bool CanRunLocally { get; } = true;

        /// <summary>
        /// This is used by the regression test system to indicate which languages this algorithm is written in.
        /// </summary>
        public virtual List<Language> Languages { get; } = new() { Language.CSharp };

        /// <summary>
        /// Data Points count of all timeslices of algorithm

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Inspect all subscriptions matching spx and compare their SIDs/configs to find the duplicate.
  2. In AddIndexOptionContract, ensure the underlying index security is reused (resolved by SID) rather than re-added.
  3. Add a guard in the SubscriptionManager that rejects adding an identical existing subscription.

Example fix

// before
if (SubscriptionManager.Subscriptions.Count(x => x.Symbol == spx) != 1)
{
    throw new RegressionTestException("Expected a single subscription for the underlying index security");
}

// after (surface the duplicates for diagnosis)
var spxSubs = SubscriptionManager.Subscriptions.Where(x => x.Symbol == spx).ToList();
if (spxSubs.Count != 1)
{
    throw new RegressionTestException(
        $"Expected a single subscription for the underlying index security, got {spxSubs.Count}: {string.Join(", ", spxSubs.Select(s => s.Configuration))}");
}
Defensive patterns

Strategy: validation

Validate before calling

var spxSubs = SubscriptionManager.Subscriptions.Where(s => s.Symbol == spx).Select(s => s.Configuration).ToList();
if (spxSubs.Count != 1)
{
    foreach (var c in spxSubs) Log($"Duplicate underlying subscription: {c}");
}

Prevention

When it happens

Trigger: AddIndexOptionContract internally calls AddIndex for the underlying again, creating a duplicate spx subscription. This is exactly the regression the test guards against.

Common situations: 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.

Related errors


AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13). Data as JSON: /api/errors/54df94b56b61ba69. Report an issue: GitHub.