{"record":{"id":"2eaec08a762b3648","repo":"QuantConnect/Lean","slug":"leverage-error-expected-expectedleverage-act","errorCode":null,"errorMessage":"Leverage error - expected: {ExpectedLeverage}, actual: {security.Leverage}","messagePattern":"Leverage error - expected: (.+?), actual: (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/CustomUniverseWithBenchmarkRegressionAlgorithm.cs","lineNumber":150,"sourceCode":"                        if (_benchmarkPriceDidNotChange > 1)\n                        {\n                            throw new RegressionTestException($\"Benchmark value error - expected a new value, current {currentValue} {slice.Time}\" +\n                                                \"Benchmark value should change when there is a change in hours\");\n                        }\n                    }\n                    else\n                    {\n                        _benchmarkPriceDidNotChange = 0;\n                    }\n                }\n            }\n            _previousBenchmarkValue = currentValue;\n            _previousTime = slice.Time;\n\n            // assert algorithm security is the correct one - not the internal one\n            if (security.Leverage != ExpectedLeverage)\n            {\n                throw new RegressionTestException($\"Leverage error - expected: {ExpectedLeverage}, actual: {security.Leverage}\");\n            }\n        }\n\n        public override void OnEndOfAlgorithm()\n        {\n            if (!_onDataWasCalled)\n            {\n                throw new RegressionTestException(\"OnData was not called\");\n            }\n        }\n\n        /// <summary>\n        /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.\n        /// </summary>\n        public bool CanRunLocally { get; } = true;\n\n        /// <summary>\n        /// This is used by the regression test system to indicate which languages this algorithm is written in.","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/CustomUniverseWithBenchmarkRegressionAlgorithm.cs#L132-L168","documentation":"This RegressionTestException is thrown at the end of OnData on every data tick. It asserts that the algorithm's SPY security has a Leverage property equal to ExpectedLeverage (2). LEAN throws it to verify that the security the algorithm uses (not the internal benchmark security) retains the correct leverage — the default equity leverage (2x) must not be altered by benchmark or universe subscription creation.","triggerScenarios":"security.Leverage != 2, meaning the SPY security's leverage was changed from the default equity value. This can happen if the benchmark subscription or universe subscription inadvertently reset or overwrote the security's leverage, or if default leverage for equities changed.","commonSituations":"A LEAN version change altered default equity leverage, the benchmark security creation modified the shared Security object's leverage, or UniverseSettings or security initializer logic changed the leverage. Also occurs when the internal benchmark security and the algorithm security are not properly separated.","solutions":["Log security.Leverage and compare with ExpectedLeverage (2) at the throw point.","Trace where security.Leverage is set — check SecurityInitializer, UniverseSettings, and benchmark creation paths.","Verify that SetBenchmark(\"SPY\") does not create a new Security object that overwrites the AddEquity security's properties.","If testing engine changes, ensure the benchmark subscription reuses the existing Security rather than creating a new one with different defaults."],"exampleFix":"// before\nif (security.Leverage != ExpectedLeverage)\n{\n    throw new RegressionTestException($\"Leverage error - expected: {ExpectedLeverage}, actual: {security.Leverage}\");\n}\n\n// after — add SetLeverage in Initialize to be explicit\n_spy = AddEquity(\"SPY\", Resolution.Hour).Symbol;\nSecurities[_spy].SetLeverage(2); // explicitly set, immune to later changes","handlingStrategy":"validation","validationCode":"// Validate leverage after all Initialize calls\npublic override void OnData(Slice slice)\n{\n    if (Securities[_spy].Leverage != 2)\n    {\n        Log($\"Leverage changed to {Securities[_spy].Leverage}, resetting to 2\");\n        Securities[_spy].SetLeverage(2);\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Explicitly call SetLeverage after all security-creating calls (AddEquity, SetBenchmark) to guard against overwrites.","Verify that benchmark subscription creation does not modify the shared Security's leverage.","Check SecurityInitializer default leverage settings when modifying engine defaults."],"tags":["quantconnect","lean","leverage","security","benchmark","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}