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
OnEndOfAlgorithm asserts the traded future's BuyingPowerModel is a FutureMarginModel (margin-based). The code casts to FutureMarginModel then checks the original for null — if the model is null or a different type, futures margin requirements cannot be read. This guards that futures securities are configured with the correct margin model.
Source
Thrown at Algorithm.CSharp/BasicTemplateFuturesAlgorithm.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 points to a concrete (non-canonical) future contract added via AddFutureContract or chain selection.
- Ensure no custom SecurityInitializer sets a non-margin BuyingPowerModel on futures.
- Check the engine version still assigns FutureMarginModel as the default for SecurityType.Future.
- If you intentionally use a custom model, update the assertion to match your model type.
Example fix
// before
var futureMarginModel = buyingPowerModel as FutureMarginModel;
if (buyingPowerModel == null) { throw ...; }
// after: check the cast result, not the (always non-null) source
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, not canonical.
- Do not override futures BuyingPowerModel with a non-margin type.
- Test the cast result (futureMarginModel), not the always-non-null source.
When it happens
Trigger: Securities[_contractSymbol].BuyingPowerModel is null or not a FutureMarginModel when OnEndOfAlgorithm runs — e.g. the security was added without futures margin configuration, or a custom buying power model replaced the default.
Common situations: A SecurityInitializer or custom BuyingPowerModel override replaced FutureMarginModel; the symbol was added as a non-future security type; engine default buying power model for futures changed; _contractSymbol resolved to a canonical/continuous symbol instead of a real contract.
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
- Expected positive buying power from BNFCR, got {buyingPower.
- ETHUSDC buying power ({ethBuyingPower.Value}) must be less t
AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13).
Data as JSON: /api/errors/8f8445afa56b0d88.
Report an issue: GitHub.