QuantConnect/Lean · error · RegressionTestException

Invalid buying power model. Found: {buyingPowerModel.GetType

Error message

Invalid buying power model. Found: {buyingPowerModel.GetType().Name}. Expected: {nameof(FutureMarginModel)}

What it means

Same as the BasicTemplateFuturesAlgorithm check: OnEndOfAlgorithm asserts the traded future's BuyingPowerModel is a FutureMarginModel so margin requirements can be read. The code casts then checks the original reference — if the model is null or wrong-typed, futures margin is unavailable, indicating misconfiguration.

Source

Thrown at Algorithm.CSharp/BasicTemplateFuturesWithExtendedMarketAlgorithm.cs:118

                        _contractSymbol = contract.Symbol;
                        MarketOrder(_contractSymbol, 1);
                    }
                }
            }
            else
            {
                Liquidate();
            }
        }

        public override void OnEndOfAlgorithm()
        {
            // Get the margin requirements
            var buyingPowerModel = Securities[_contractSymbol].BuyingPowerModel;
            var futureMarginModel = buyingPowerModel as FutureMarginModel;
            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}");
                }
            }

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Confirm _contractSymbol is a concrete future contract (not canonical).
  2. Ensure no SecurityInitializer assigns a non-margin BuyingPowerModel to futures.
  3. Verify the engine still defaults futures to FutureMarginModel.
  4. If using a custom margin model, update the assertion to that type.

Example fix

// before
var futureMarginModel = buyingPowerModel as FutureMarginModel;
if (buyingPowerModel == null) { throw ...; }

// after: test the cast result
if (futureMarginModel == null) { throw ...; }
Defensive patterns

Strategy: type-guard

Validate before calling

var bpm = Securities[_contractSymbol].BuyingPowerModel;
if (bpm is not FutureMarginModel fmm)
{
    Log($"Expected FutureMarginModel, got {bpm?.GetType().Name ?? "null"}.");
}

Type guard

bool HasFutureMarginModel(Symbol s) => Securities[s].BuyingPowerModel is FutureMarginModel;

Prevention

When it happens

Trigger: Securities[_contractSymbol].BuyingPowerModel is null or not a FutureMarginModel in the extended-market algorithm at OnEndOfAlgorithm — a non-margin model was assigned, or _contractSymbol is not a real future contract.

Common situations: A SecurityInitializer/custom model overrode FutureMarginModel; _contractSymbol resolved to a canonical symbol; extended-hours add path configured the security differently; engine default changed.

Related errors


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