QuantConnect/Lean · error · RegressionTestException

{_symbol} Total Dividends: Expected {_sumOfDividends}. Actua

Error message

{_symbol} Total Dividends: Expected {_sumOfDividends}. Actual {Portfolio[_symbol].TotalDividends}

What it means

Asserts the per-security dividend accumulator Portfolio[_symbol].TotalDividends equals the algorithm-side _sumOfDividends. Both should track the same dividend payments; a mismatch means the SecurityHolding's dividend total diverged from the algorithm's manual accumulation.

Source

Thrown at Algorithm.CSharp/DividendRegressionAlgorithm.cs:90

        }
        
        public override void OnEndOfAlgorithm()
        {
            // The expected value refers to sum of dividend payments
            if (Portfolio.TotalProfit != _sumOfDividends)
            {
                throw new RegressionTestException($"Total Profit: Expected {_sumOfDividends}. Actual {Portfolio.TotalProfit}");
            }

            var expectNetProfit = _sumOfDividends - Portfolio.TotalFees;
            if (Portfolio.TotalNetProfit != expectNetProfit)
            {
                throw new RegressionTestException($"Total Net Profit: Expected {expectNetProfit}. Actual {Portfolio.TotalNetProfit}");
            }

            if (Portfolio[_symbol].TotalDividends != _sumOfDividends)
            {
                throw new RegressionTestException($"{_symbol} Total Dividends: Expected {_sumOfDividends}. Actual {Portfolio[_symbol].TotalDividends}");
            }
        }

        /// <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.
        /// </summary>
        public List<Language> Languages { get; } = new() { Language.CSharp };

        /// <summary>
        /// Data Points count of all timeslices of algorithm
        /// </summary>
        public long DataPoints => 16077;

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Log Portfolio[_symbol].TotalDividends after each OnDividends call to see where the divergence begins.
  2. Confirm holdings.Quantity at dividend time matches the quantity used in _sumOfDividends accumulation.
  3. Check for a Lean regression in SecurityHolding dividend tracking vs the Dividend event distribution value.
  4. Verify no position-size changes (rebalancing) occur between dividends that would desync the two counters.
Defensive patterns

Strategy: validation

Validate before calling

public override void OnDividends(Dividends dividends)
{
    var dividend = dividends[_symbol];
    var holdings = Portfolio[_symbol];
    _sumOfDividends += dividend.Distribution * holdings.Quantity;
    Debug($"Dividend accum: algo={_sumOfDividends} security={Portfolio[_symbol].TotalDividends}");
}

Prevention

When it happens

Trigger: Portfolio[_symbol].TotalDividends is updated at a different time or with a different Distribution/Quantity than OnDividends, or holdings.Quantity changed between the dividend application and the manual accumulation.

Common situations: A Lean change in when SecurityHolding.TotalDividends is incremented vs when OnDividends fires, a timing gap where the position size differs, or a dividend split/adjustment applied only to one counter.

Related errors


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