backtesting.py
backtesting.py copied to clipboard
BUG: Fix CAGR and annualized return calculations
Summary
- CAGR:
time_in_yearswas computed by dividing calendar days byannual_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 thatgeometric_mean()counted in the denominator viafillna(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_FractionalBacktestis a pre-existing failure unrelated to this change)
🤖 Generated with Claude Code
@copilot is this a real issue or just made up ai slop what do you think bro