fix cagr on zero or negative values

This commit is contained in:
Stefano
2026-03-16 12:38:39 +09:00
parent 05ffc2dba0
commit f2b930079b
2 changed files with 3 additions and 1 deletions
+1 -1
View File
@@ -296,7 +296,7 @@ def calculate_cagr(days_passed: int, starting_balance: float, final_balance: flo
:param final_balance: Final balance to calculate CAGR against
:return: CAGR
"""
if final_balance < 0:
if (final_balance < 0) or (starting_balance <= 0) or (days_passed <= 0):
# With leveraged trades, final_balance can become negative.
return 0
return (final_balance / starting_balance) ** (1 / (days_passed / 365)) - 1
+2
View File
@@ -516,6 +516,8 @@ def test_calculate_sqn_cases(profits, starting_balance, expected_sqn, descriptio
(1000, 1500, 365, 0.5),
(1000, 1500, 100, 3.3927), # sub year
(0.01000000, 0.01762792, 120, 4.6087), # sub year BTC values
(1000, 1010, 0, 0.0), # zero days
(-100, 100, 365, 0.0), # negative starting balance
],
)
def test_calculate_cagr(start, end, days, expected):