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
- Confirm _contractSymbol is a concrete future contract (not canonical).
- Ensure no SecurityInitializer assigns a non-margin BuyingPowerModel to futures.
- Verify the engine still defaults futures to FutureMarginModel.
- 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
- Ensure _contractSymbol is a concrete future contract.
- Do not assign a non-margin BuyingPowerModel to futures.
- Test the cast result, not the always-non-null source reference.
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
- Invalid buying power model. Found: {buyingPowerModel.GetType
- Future contracts did not work up as expected: {addedSecurity
- Future contracts did not work up as expected: {addedSecurity
- {Time} unexpected symbol changed event {changedEvent}!
- Expected positive buying power from BNFCR, got {buyingPower.
AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13).
Data as JSON: /api/errors/f2e474242ddac48b.
Report an issue: GitHub.