{"record":{"id":"ad59976e13b5e77e","repo":"QuantConnect/Lean","slug":"total-net-profit-expected-expectnetprofit-actu","errorCode":null,"errorMessage":"Total Net Profit: Expected {expectNetProfit}. Actual {Portfolio.TotalNetProfit}","messagePattern":"Total Net Profit: Expected (.+?)\\. Actual (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/DividendRegressionAlgorithm.cs","lineNumber":85,"sourceCode":"            Debug($\"{dividend.Time.ToStringInvariant(\"o\")} >> DIVIDEND >> {dividend.Symbol} - \" +\n                $\"{dividend.Distribution.ToStringInvariant(\"C\")} - {Portfolio.Cash} - \" +\n                $\"{holdings.Price.ToStringInvariant(\"C\")}\"\n            );\n            _sumOfDividends += dividend.Distribution * holdings.Quantity;\n        }\n        \n        public override void OnEndOfAlgorithm()\n        {\n            // The expected value refers to sum of dividend payments\n            if (Portfolio.TotalProfit != _sumOfDividends)\n            {\n                throw new RegressionTestException($\"Total Profit: Expected {_sumOfDividends}. Actual {Portfolio.TotalProfit}\");\n            }\n\n            var expectNetProfit = _sumOfDividends - Portfolio.TotalFees;\n            if (Portfolio.TotalNetProfit != expectNetProfit)\n            {\n                throw new RegressionTestException($\"Total Net Profit: Expected {expectNetProfit}. Actual {Portfolio.TotalNetProfit}\");\n            }\n\n            if (Portfolio[_symbol].TotalDividends != _sumOfDividends)\n            {\n                throw new RegressionTestException($\"{_symbol} Total Dividends: Expected {_sumOfDividends}. Actual {Portfolio[_symbol].TotalDividends}\");\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.\n        /// </summary>\n        public List<Language> Languages { get; } = new() { Language.CSharp };\n","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/DividendRegressionAlgorithm.cs#L67-L103","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Log Portfolio.TotalProfit, Portfolio.TotalFees, and Portfolio.TotalNetProfit to verify the arithmetic identity TotalNetProfit == TotalProfit - TotalFees.","Confirm error [90] passes first; if not, fix that first.","Check whether Lean changed TotalProfit to be net-of-fees (which would break this formula).","Verify only one entry trade occurred so fees are deterministic."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"public override void OnEndOfAlgorithm()\n{\n    // Verify the identity before asserting\n    var computed = Portfolio.TotalProfit - Portfolio.TotalFees;\n    if (Portfolio.TotalNetProfit != computed)\n    {\n        Debug($\"Identity broken: Net={Portfolio.TotalNetProfit} Profit-Fees={computed}\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify the TotalProfit/TotalFees/TotalNetProfit identity holds in your Lean version.","Ensure error [90] passes before diagnosing this one.","Log all three values to detect fee-accounting regressions."],"tags":["quantconnect","dividends","portfolio","fees","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}