{"record":{"id":"f2e474242ddac48b","repo":"QuantConnect/Lean","slug":"invalid-buying-power-model-found-buyingpowermod-f2e474","errorCode":null,"errorMessage":"Invalid buying power model. Found: {buyingPowerModel.GetType().Name}. Expected: {nameof(FutureMarginModel)}","messagePattern":"Invalid buying power model\\. Found: (.+?)\\. Expected: (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/BasicTemplateFuturesWithExtendedMarketAlgorithm.cs","lineNumber":118,"sourceCode":"                        _contractSymbol = contract.Symbol;\n                        MarketOrder(_contractSymbol, 1);\n                    }\n                }\n            }\n            else\n            {\n                Liquidate();\n            }\n        }\n\n        public override void OnEndOfAlgorithm()\n        {\n            // Get the margin requirements\n            var buyingPowerModel = Securities[_contractSymbol].BuyingPowerModel;\n            var futureMarginModel = buyingPowerModel as FutureMarginModel;\n            if (buyingPowerModel == null)\n            {\n                throw new RegressionTestException($\"Invalid buying power model. Found: {buyingPowerModel.GetType().Name}. Expected: {nameof(FutureMarginModel)}\");\n            }\n            var initialOvernight = futureMarginModel.InitialOvernightMarginRequirement;\n            var maintenanceOvernight = futureMarginModel.MaintenanceOvernightMarginRequirement;\n            var initialIntraday = futureMarginModel.InitialIntradayMarginRequirement;\n            var maintenanceIntraday = futureMarginModel.MaintenanceIntradayMarginRequirement;\n        }\n\n        public override void OnSecuritiesChanged(SecurityChanges changes)\n        {\n            foreach (var addedSecurity in changes.AddedSecurities)\n            {\n                if (addedSecurity.Symbol.SecurityType == SecurityType.Future\n                    && !addedSecurity.Symbol.IsCanonical()\n                    && !addedSecurity.HasData)\n                {\n                    throw new RegressionTestException($\"Future contracts did not work up as expected: {addedSecurity.Symbol}\");\n                }\n            }","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/BasicTemplateFuturesWithExtendedMarketAlgorithm.cs#L100-L136","documentation":"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.","triggerScenarios":"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.","commonSituations":"A SecurityInitializer/custom model overrode FutureMarginModel; _contractSymbol resolved to a canonical symbol; extended-hours add path configured the security differently; engine default changed.","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."],"exampleFix":"// before\nvar futureMarginModel = buyingPowerModel as FutureMarginModel;\nif (buyingPowerModel == null) { throw ...; }\n\n// after: test the cast result\nif (futureMarginModel == null) { throw ...; }","handlingStrategy":"type-guard","validationCode":"var bpm = Securities[_contractSymbol].BuyingPowerModel;\nif (bpm is not FutureMarginModel fmm)\n{\n    Log($\"Expected FutureMarginModel, got {bpm?.GetType().Name ?? \"null\"}.\");\n}","typeGuard":"bool HasFutureMarginModel(Symbol s) => Securities[s].BuyingPowerModel is FutureMarginModel;","tryCatchPattern":null,"preventionTips":["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."],"tags":["futures","buying-power","margin","regression-test","extended-market","security-configuration"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}