QuantConnect/Lean · error · RegressionTestException

Leverage error - expected: {ExpectedLeverage}, actual: {secu

Error message

Leverage error - expected: {ExpectedLeverage}, actual: {security.Leverage}

What it means

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.

Source

Thrown at Algorithm.CSharp/CustomUniverseWithBenchmarkRegressionAlgorithm.cs:150

                        if (_benchmarkPriceDidNotChange > 1)
                        {
                            throw new RegressionTestException($"Benchmark value error - expected a new value, current {currentValue} {slice.Time}" +
                                                "Benchmark value should change when there is a change in hours");
                        }
                    }
                    else
                    {
                        _benchmarkPriceDidNotChange = 0;
                    }
                }
            }
            _previousBenchmarkValue = currentValue;
            _previousTime = slice.Time;

            // assert algorithm security is the correct one - not the internal one
            if (security.Leverage != ExpectedLeverage)
            {
                throw new RegressionTestException($"Leverage error - expected: {ExpectedLeverage}, actual: {security.Leverage}");
            }
        }

        public override void OnEndOfAlgorithm()
        {
            if (!_onDataWasCalled)
            {
                throw new RegressionTestException("OnData was not called");
            }
        }

        /// <summary>
        /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
        /// </summary>
        public bool CanRunLocally { get; } = true;

        /// <summary>
        /// This is used by the regression test system to indicate which languages this algorithm is written in.

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Log security.Leverage and compare with ExpectedLeverage (2) at the throw point.
  2. Trace where security.Leverage is set — check SecurityInitializer, UniverseSettings, and benchmark creation paths.
  3. Verify that SetBenchmark("SPY") does not create a new Security object that overwrites the AddEquity security's properties.
  4. If testing engine changes, ensure the benchmark subscription reuses the existing Security rather than creating a new one with different defaults.

Example fix

// before
if (security.Leverage != ExpectedLeverage)
{
    throw new RegressionTestException($"Leverage error - expected: {ExpectedLeverage}, actual: {security.Leverage}");
}

// after — add SetLeverage in Initialize to be explicit
_spy = AddEquity("SPY", Resolution.Hour).Symbol;
Securities[_spy].SetLeverage(2); // explicitly set, immune to later changes
Defensive patterns

Strategy: validation

Validate before calling

// Validate leverage after all Initialize calls
public override void OnData(Slice slice)
{
    if (Securities[_spy].Leverage != 2)
    {
        Log($"Leverage changed to {Securities[_spy].Leverage}, resetting to 2");
        Securities[_spy].SetLeverage(2);
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13). Data as JSON: /api/errors/2eaec08a762b3648. Report an issue: GitHub.