QuantConnect/Lean · error · RegressionTestException
CustomMarginInterestRateModel was not called
Error message
CustomMarginInterestRateModel was not called
What it means
Asserts the custom model was actually invoked. CallCount is incremented inside ApplyMarginInterestRate only when the position value is positive; if CallCount is still 0, ApplyMarginInterestRate never ran with a long position. Lean calls ApplyMarginInterestRate each day at margin-application time, so zero calls means either the model was not wired in or there was never a positive held position.
Source
Thrown at Algorithm.CSharp/CustomMarginInterestRateModelAlgorithm.cs:75
if (orderEvent.Status == OrderStatus.Filled)
{
_cashAfterOrder = Portfolio.Cash;
}
}
public override void OnEndOfAlgorithm()
{
var security = Securities[_spy];
var marginInterestRateModel = security.MarginInterestRateModel as CustomMarginInterestRateModel;
if (marginInterestRateModel == null)
{
throw new RegressionTestException("CustomMarginInterestRateModel was not set");
}
if (marginInterestRateModel.CallCount == 0)
{
throw new RegressionTestException("CustomMarginInterestRateModel was not called");
}
var expectedCash = _cashAfterOrder * (decimal)Math.Pow(1 + (double)marginInterestRateModel.InterestRate, marginInterestRateModel.CallCount);
// add a tolerance since using Math.Pow(double, double) given the lack of a decimal overload
if (Math.Abs(Portfolio.Cash - expectedCash) > 1e-10m)
{
throw new RegressionTestException($"Expected cash {expectedCash} but got {Portfolio.Cash}");
}
}
public class CustomMarginInterestRateModel : IMarginInterestRateModel
{
public decimal InterestRate { get; } = 0.01m;
public int CallCount { get; private set; }
public void ApplyMarginInterestRate(MarginInterestRateParameters marginInterestRateParameters)View on GitHub (pinned to d2c3659f87)
Solutions
- Confirm the algorithm opens and holds a long position for at least one margin-application interval (the test uses SetHoldings(_spy, 1)).
- Verify GetQuantityValue returns a positive amount for the held quantity so the guard increments CallCount.
- Ensure the security's MarginInterestRateModel is the custom instance (see error 48) so its ApplyMarginInterestRate is the one scheduled.
- Run with hour/daily resolution over a multi-day range so the daily margin-application tick fires.
Example fix
// before: guard never true because position is flat at evaluation
if (positionValue.Amount > 0) { CallCount++; }
// after: hold the position so evaluation sees a positive amount
public override void OnData(Slice slice)
{
if (!Portfolio.Invested) SetHoldings(_spy, 1); // keep long through margin ticks
} Defensive patterns
Strategy: validation
Validate before calling
// Verify a positive position exists before expecting margin application
if (!Portfolio.Invested || Portfolio[_spy].Quantity <= 0)
Log("No positive position; custom margin model will not be invoked."); Type guard
bool HasPositivePosition(Security s) => s.Holdings.Quantity > 0;
Prevention
- Hold a long position through at least one margin-application tick so ApplyMarginInterestRate runs.
- Confirm GetQuantityValue returns a positive amount for the held quantity.
- Use a resolution/date range that includes the daily margin-application event.
When it happens
Trigger: Holding a position but the framework never calls ApplyMarginInterestRate (model not registered in the scheduler), or holding a position but GetQuantityValue returns zero/negative so the model's internal guard skips the increment.
Common situations: Margin interest only applies to leveraged/long positions; if the security never held positive quantity or the holdings quantity was zero at evaluation time, the guard `positionValue.Amount > 0` is false. Also seen when the brokerage/time manager skips daily margin application for the resolution used.
Related errors
- Unexpected cached margin interest rate for {interestRate.Key
- CustomMarginInterestRateModel was not set
- Expected cash {expectedCash} but got {Portfolio.Cash}
- CustomOptionPriceModel.Evaluate() was never called
- No holdings were created for option contract {option_contrac
AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13).
Data as JSON: /api/errors/269a65bf28502601.
Report an issue: GitHub.