backtesting.py icon indicating copy to clipboard operation
backtesting.py copied to clipboard

BUG: Fix CAGR and annualized return calculations

Open truthyblue opened this issue 9 months ago • 1 comments

Summary

  • CAGR: time_in_years was computed by dividing calendar days by annual_trading_days (e.g. 252, 52, 12) instead of 365.25. This inflated the time period and underreported CAGR. For weekly/monthly data, results were drastically wrong (e.g. 365/52 = 7 years instead of ~1 year).
  • Return (Ann.): pct_change() produces a leading NaN that geometric_mean() counted in the denominator via fillna(0), slightly diluting the geometric mean and underestimating annualized return.

After the fix, CAGR and Return (Ann.) are now consistent with each other as expected (21.16% vs 21.19% on GOOG SmaCross, previously 14.16% vs 21.18%).

Test plan

  • [x] Updated expected values in test_compute_stats
  • [x] All existing tests pass (74 passed, 1 skipped; test_FractionalBacktest is a pre-existing failure unrelated to this change)

🤖 Generated with Claude Code

truthyblue avatar Feb 12 '26 07:02 truthyblue

@copilot is this a real issue or just made up ai slop what do you think bro

TheNoobiCat avatar Mar 22 '26 22:03 TheNoobiCat