{"record":{"id":"28d1d69b8965e8f6","repo":"QuantConnect/Lean","slug":"custommargininterestratemodel-was-not-set","errorCode":null,"errorMessage":"CustomMarginInterestRateModel was not set","messagePattern":"CustomMarginInterestRateModel was not set","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/CustomMarginInterestRateModelAlgorithm.cs","lineNumber":70,"sourceCode":"            }\n        }\n\n        public override void OnOrderEvent(OrderEvent orderEvent)\n        {\n            if (orderEvent.Status == OrderStatus.Filled)\n            {\n                _cashAfterOrder = Portfolio.Cash;\n            }\n        }\n\n        public override void OnEndOfAlgorithm()\n        {\n            var security = Securities[_spy];\n            var marginInterestRateModel = security.MarginInterestRateModel as CustomMarginInterestRateModel;\n\n            if (marginInterestRateModel == null)\n            {\n                throw new RegressionTestException(\"CustomMarginInterestRateModel was not set\");\n            }\n\n            if (marginInterestRateModel.CallCount == 0)\n            {\n                throw new RegressionTestException(\"CustomMarginInterestRateModel was not called\");\n            }\n\n            var expectedCash = _cashAfterOrder * (decimal)Math.Pow(1 + (double)marginInterestRateModel.InterestRate, marginInterestRateModel.CallCount);\n\n            // add a tolerance since using Math.Pow(double, double) given the lack of a decimal overload\n            if (Math.Abs(Portfolio.Cash - expectedCash) > 1e-10m)\n            {\n                throw new RegressionTestException($\"Expected cash {expectedCash} but got {Portfolio.Cash}\");\n            }\n        }\n\n        public class CustomMarginInterestRateModel : IMarginInterestRateModel\n        {","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/CustomMarginInterestRateModelAlgorithm.cs#L52-L88","documentation":"Asserts that security.SetMarginInterestRateModel(...) persisted the model. OnEndOfAlgorithm casts Securities[_spy].MarginInterestRateModel to CustomMarginInterestRateModel; a null result means the property was never set or was overwritten by a default after Initialize.","triggerScenarios":"Calling security.SetMarginInterestRateModel(new CustomMarginInterestRateModel()) in Initialize, then later (or via a framework default) the MarginInterestRateModel is replaced by the brokerage default before OnEndOfAlgorithm.","commonSituations":"A Lean upgrade that resets MarginInterestRateModel when brokerage model is applied after Initialize, calling SetBrokerageModel after setting the custom model, or setting the model on the wrong security reference (e.g., a transient Security from AddEquity not stored).","solutions":["Set the custom model AFTER SetBrokerageModel so the brokerage default does not overwrite it.","Ensure you store the Security returned by AddEquity and call SetMarginInterestRateModel on that exact instance.","Re-assert the model type in OnEndOfAlgorithm (as the test does) to catch silent overwrites early.","Check whether the brokerage model's implementation reassigns MarginInterestRateModel and disable/override that path."],"exampleFix":"// before: brokerage model applied last clobbers the custom model\nsecurity.SetMarginInterestRateModel(new CustomMarginInterestRateModel());\nSetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage);\n\n// after: set custom model after brokerage defaults are applied\nSetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage);\nsecurity.SetMarginInterestRateModel(new CustomMarginInterestRateModel());","handlingStrategy":"validation","validationCode":"// Assert the model is set right after configuration\nsecurity.SetMarginInterestRateModel(new CustomMarginInterestRateModel());\nif (!(security.MarginInterestRateModel is CustomMarginInterestRateModel))\n    throw new InvalidOperationException(\"Custom margin interest rate model was overwritten\");","typeGuard":"bool HasCustomModel(Security s) => s.MarginInterestRateModel is CustomMarginInterestRateModel;","tryCatchPattern":null,"preventionTips":["Call SetMarginInterestRateModel AFTER SetBrokerageModel so defaults do not overwrite it.","Keep the Security reference returned by AddEquity and configure that instance.","Re-assert the model type in OnEndOfAlgorithm to catch silent overwrites."],"tags":["regression-test","margin-interest-rate","brokerage-model","set-model"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}