Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion freqtrade/data/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,11 @@ def calculate_cagr(days_passed: int, starting_balance: float, final_balance: flo
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
try:
return (final_balance / starting_balance) ** (1 / (days_passed / 365)) - 1
except OverflowError:
# Extrapolating a large gain over a very short timeframe can exceed float range.
return 0


def calculate_expectancy(trades: pd.DataFrame) -> tuple[float, float]:
Expand Down
2 changes: 2 additions & 0 deletions tests/data/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ def test_calculate_p_value_zero_mean():
(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
(1.49, 11.2, 1, 0.0), # Overflow - huge gain over a single day
(1.0, 6.99, 1, 1.7146249823477656e308), # just below the overflow threshold
],
)
def test_calculate_cagr(start, end, days, expected):
Expand Down
Loading