From ec1b676ab44c091d6c0ffcf87df029e3ae86e8af Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 16 Feb 2024 20:04:49 +0100 Subject: [PATCH] Fix edge-case when calculating cagr edge-case with leveraged trades - yielding a negative final balance. closes #9820 --- freqtrade/data/metrics.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/freqtrade/data/metrics.py b/freqtrade/data/metrics.py index 7b45342bb..b37e0bb19 100644 --- a/freqtrade/data/metrics.py +++ b/freqtrade/data/metrics.py @@ -191,6 +191,9 @@ 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: + # With leveraged trades, final_balance can become negative. + return 0 return (final_balance / starting_balance) ** (1 / (days_passed / 365)) - 1