From 3edc442f4877abb272efaa8df970ae7a134ac410 Mon Sep 17 00:00:00 2001 From: mrpabloyeah Date: Wed, 2 Apr 2025 15:46:05 +0200 Subject: [PATCH 1/2] Fix drawdown calculation when maximum drawdown occurs on the first trade --- freqtrade/data/metrics.py | 10 ++++------ tests/data/test_btanalysis.py | 5 +++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/freqtrade/data/metrics.py b/freqtrade/data/metrics.py index e15288802..f841c1909 100644 --- a/freqtrade/data/metrics.py +++ b/freqtrade/data/metrics.py @@ -201,13 +201,11 @@ def calculate_max_drawdown( if relative else max_drawdown_df["drawdown"].idxmin() ) - if idxmin == 0: - raise ValueError("No losing trade, therefore no drawdown.") - high_date = profit_results.loc[max_drawdown_df.iloc[:idxmin]["high_value"].idxmax(), date_col] + + high_idx = max_drawdown_df.iloc[:idxmin+1]["high_value"].idxmax() + high_date = profit_results.loc[high_idx, date_col] low_date = profit_results.loc[idxmin, date_col] - high_val = max_drawdown_df.loc[ - max_drawdown_df.iloc[:idxmin]["high_value"].idxmax(), "cumulative" - ] + high_val = max_drawdown_df.loc[high_idx, "cumulative"] low_val = max_drawdown_df.loc[idxmin, "cumulative"] max_drawdown_rel = max_drawdown_df.loc[idxmin, "drawdown_relative"] diff --git a/tests/data/test_btanalysis.py b/tests/data/test_btanalysis.py index b0128dd25..7adba9c19 100644 --- a/tests/data/test_btanalysis.py +++ b/tests/data/test_btanalysis.py @@ -562,8 +562,9 @@ def test_calculate_max_drawdown2(): assert pytest.approx(drawdown.relative_account_drawdown) == 0.32129575 df = DataFrame(zip(values[:5], dates[:5], strict=False), columns=["profit", "open_date"]) - with pytest.raises(ValueError, match="No losing trade, therefore no drawdown."): - calculate_max_drawdown(df, date_col="open_date", value_col="profit") + # No losing trade ... + drawdown = calculate_max_drawdown(df, date_col="open_date", value_col="profit") + assert drawdown.drawdown_abs == 0.0 df1 = DataFrame(zip(values[:5], dates[:5], strict=False), columns=["profit", "open_date"]) df1.loc[:, "profit"] = df1["profit"] * -1 From 087fb31f26d43f1b717f1942a92aa768ad38cbd6 Mon Sep 17 00:00:00 2001 From: mrpabloyeah Date: Wed, 2 Apr 2025 16:02:42 +0200 Subject: [PATCH 2/2] Fix for passing CI --- 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 f841c1909..8e234df85 100644 --- a/freqtrade/data/metrics.py +++ b/freqtrade/data/metrics.py @@ -202,7 +202,7 @@ def calculate_max_drawdown( else max_drawdown_df["drawdown"].idxmin() ) - high_idx = max_drawdown_df.iloc[:idxmin+1]["high_value"].idxmax() + high_idx = max_drawdown_df.iloc[: idxmin + 1]["high_value"].idxmax() high_date = profit_results.loc[high_idx, date_col] low_date = profit_results.loc[idxmin, date_col] high_val = max_drawdown_df.loc[high_idx, "cumulative"]