QuantConnect/Lean · error · RegressionTestException

Total Net Profit: Expected {expectNetProfit}. Actual {Portfo

Error message

Total Net Profit: Expected {expectNetProfit}. Actual {Portfolio.TotalNetProfit}

What it means

Asserts Portfolio.TotalNetProfit equals _sumOfDividends minus Portfolio.TotalFees. TotalNetProfit = TotalProfit - TotalFees by definition, so if error [90] passed and this fails, TotalFees is inconsistent with the profit subtraction, indicating a fee-accounting or ordering discrepancy.

Source

Thrown at Algorithm.CSharp/DividendRegressionAlgorithm.cs:85

            Debug($"{dividend.Time.ToStringInvariant("o")} >> DIVIDEND >> {dividend.Symbol} - " +
                $"{dividend.Distribution.ToStringInvariant("C")} - {Portfolio.Cash} - " +
                $"{holdings.Price.ToStringInvariant("C")}"
            );
            _sumOfDividends += dividend.Distribution * holdings.Quantity;
        }
        
        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 };

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Log Portfolio.TotalProfit, Portfolio.TotalFees, and Portfolio.TotalNetProfit to verify the arithmetic identity TotalNetProfit == TotalProfit - TotalFees.
  2. Confirm error [90] passes first; if not, fix that first.
  3. Check whether Lean changed TotalProfit to be net-of-fees (which would break this formula).
  4. Verify only one entry trade occurred so fees are deterministic.
Defensive patterns

Strategy: validation

Validate before calling

public override void OnEndOfAlgorithm()
{
    // Verify the identity before asserting
    var computed = Portfolio.TotalProfit - Portfolio.TotalFees;
    if (Portfolio.TotalNetProfit != computed)
    {
        Debug($"Identity broken: Net={Portfolio.TotalNetProfit} Profit-Fees={computed}");
    }
}

Prevention

When it happens

Trigger: TotalFees is non-zero and not subtracted correctly, or TotalProfit already had fees removed in a Lean version change, making the double-subtraction mismatch. Also fires if a fee was charged that the test did not anticipate.

Common situations: A Lean change to fee calculation or to whether TotalProfit is gross or net, an unexpected fee from a second trade, or a data change altering the position size and thus the fee.

Related errors


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