QuantConnect/Lean · error · RegressionTestException

Future contracts did not work up as expected: {addedSecurity

Error message

Future contracts did not work up as expected: {addedSecurity.Symbol}

What it means

OnSecuritiesChanged asserts that every non-canonical (real) future contract added by the universe has data (addedSecurity.HasData). If a concrete future contract is added but HasData is false, the universe selected a contract with no data feed — it would be untradeable and indicates a filter or data problem.

Source

Thrown at Algorithm.CSharp/BasicTemplateFuturesAlgorithm.cs:134

            if (buyingPowerModel == null)
            {
                throw new RegressionTestException($"Invalid buying power model. Found: {buyingPowerModel.GetType().Name}. Expected: {nameof(FutureMarginModel)}");
            }
            var initialOvernight = futureMarginModel.InitialOvernightMarginRequirement;
            var maintenanceOvernight = futureMarginModel.MaintenanceOvernightMarginRequirement;
            var initialIntraday = futureMarginModel.InitialIntradayMarginRequirement;
            var maintenanceIntraday = futureMarginModel.MaintenanceIntradayMarginRequirement;
        }

        public override void OnSecuritiesChanged(SecurityChanges changes)
        {
            foreach (var addedSecurity in changes.AddedSecurities)
            {
                if (addedSecurity.Symbol.SecurityType == SecurityType.Future
                    && !addedSecurity.Symbol.IsCanonical()
                    && !addedSecurity.HasData)
                {
                    throw new RegressionTestException($"Future contracts did not work up as expected: {addedSecurity.Symbol}");
                }
            }
        }

        /// <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 bool CanRunLocally { get; } = true;

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

        /// <summary>
        /// Data Points count of all timeslices of algorithm
        /// </summary>
        public long DataPoints => 40308;

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Tighten the future contract SetFilter to exclude expiries/contracts without data.
  2. Ensure the required futures data for the selected contracts is downloaded/available.
  3. Filter the chain on volume/liquidity or HasData before selecting a contract.
  4. Verify the data feed/subscription configuration so added contracts receive bars.

Example fix

// before
where futuresContract.Expiry > Time.Date.AddDays(90)

// after: also require the contract has data
where futuresContract.Expiry > Time.Date.AddDays(90) && futuresContract.HasData
Defensive patterns

Strategy: validation

Validate before calling

foreach (var added in changes.AddedSecurities)
{
    if (added.Symbol.SecurityType == SecurityType.Future
        && !added.Symbol.IsCanonical() && !added.HasData)
    {
        Log($"{added.Symbol} added without data; tightening filter.");
    }
}

Type guard

bool IsTradeableFutureWith(Security s) => s.Symbol.SecurityType == SecurityType.Future && !s.Symbol.IsCanonical() && s.HasData;

Prevention

When it happens

Trigger: OnSecuritiesChanged receives an added security that is SecurityType.Future, not canonical (!IsCanonical()), but addedSecurity.HasData is false — a real contract was selected without any data subscription backing it.

Common situations: The future contract filter selected an expiry with no data in the local/remote dataset; data for that contract is missing or not downloaded; the SetFilter range included illiquid/empty contracts; a data feed configuration issue left the security without bars.

Related errors


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