From 68f32d76aeb1d0b356a0c228e441495eed5bea8c Mon Sep 17 00:00:00 2001 From: mrpabloyeah Date: Tue, 1 Apr 2025 20:54:03 +0200 Subject: [PATCH 1/2] Fix drawdown calculation when there are no winning trades --- freqtrade/data/metrics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freqtrade/data/metrics.py b/freqtrade/data/metrics.py index d2ae2d64b..e15288802 100644 --- a/freqtrade/data/metrics.py +++ b/freqtrade/data/metrics.py @@ -118,7 +118,7 @@ def _calc_drawdown_series( ) -> pd.DataFrame: max_drawdown_df = pd.DataFrame() max_drawdown_df["cumulative"] = profit_results[value_col].cumsum() - max_drawdown_df["high_value"] = max_drawdown_df["cumulative"].cummax() + max_drawdown_df["high_value"] = np.maximum(0, max_drawdown_df["cumulative"].cummax()) max_drawdown_df["drawdown"] = max_drawdown_df["cumulative"] - max_drawdown_df["high_value"] max_drawdown_df["date"] = profit_results.loc[:, date_col] if starting_balance: From a3f23fd4fb2636402a7f2f023fac6bef8215b2bd Mon Sep 17 00:00:00 2001 From: mrpabloyeah Date: Tue, 1 Apr 2025 22:51:29 +0200 Subject: [PATCH 2/2] Also fix the expected result in the test --- tests/data/test_btanalysis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/data/test_btanalysis.py b/tests/data/test_btanalysis.py index eec3ac4e4..b0128dd25 100644 --- a/tests/data/test_btanalysis.py +++ b/tests/data/test_btanalysis.py @@ -569,7 +569,7 @@ def test_calculate_max_drawdown2(): df1.loc[:, "profit"] = df1["profit"] * -1 # No winning trade ... drawdown = calculate_max_drawdown(df1, date_col="open_date", value_col="profit") - assert drawdown.drawdown_abs == 0.043965 + assert drawdown.drawdown_abs == 0.055545 @pytest.mark.parametrize(