From b7145debfb04b03916e3b3a39cc743b4959cd12b Mon Sep 17 00:00:00 2001 From: Anuj Jain Date: Thu, 5 Sep 2024 21:52:09 +0530 Subject: [PATCH] handle trade wide indicators --- docs/advanced-backtesting.md | 4 ++++ freqtrade/data/entryexitanalysis.py | 11 ++++++++++- tests/data/test_entryexitanalysis.py | 1 + 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/advanced-backtesting.md b/docs/advanced-backtesting.md index de1264ae8..5004dfc2a 100644 --- a/docs/advanced-backtesting.md +++ b/docs/advanced-backtesting.md @@ -144,6 +144,10 @@ This detailed view of indicator values enhances the analysis. The `(entry)` and `(exit)` suffixes are added to indicators to distinguish the values at the entry and exit points of the trade. +!!! note "Trade-wide Indicators" + Certain trade-wide indicators do not have the `(entry)` or `(exit)` suffix. These indicators include: + `"open_date"`, `"close_date"`, `"min_rate"`, `"max_rate"`, `"profit_ratio"`, and `"profit_abs"`. + ### Filtering the trade output by date To show only trades between dates within your backtested timerange, supply the usual `timerange` option in `YYYYMMDD-[YYYYMMDD]` format: diff --git a/freqtrade/data/entryexitanalysis.py b/freqtrade/data/entryexitanalysis.py index 964c4f86f..974e39d39 100644 --- a/freqtrade/data/entryexitanalysis.py +++ b/freqtrade/data/entryexitanalysis.py @@ -303,6 +303,15 @@ def print_results( def _merge_dfs(entry_df, exit_df, available_inds): merge_on = ["pair", "open_date"] + trade_wide_indicators = [ + "open_date", + "close_date", + "min_rate", + "max_rate", + "profit_ratio", + "profit_abs", + ] + signal_wide_indicators = list(set(available_inds) - set(trade_wide_indicators)) columns_to_keep = merge_on + ["enter_reason", "exit_reason"] + available_inds if exit_df is None or exit_df.empty: @@ -310,7 +319,7 @@ def _merge_dfs(entry_df, exit_df, available_inds): return pd.merge( entry_df[columns_to_keep], - exit_df[merge_on + available_inds], + exit_df[merge_on + signal_wide_indicators], on=merge_on, suffixes=(" (entry)", " (exit)"), ) diff --git a/tests/data/test_entryexitanalysis.py b/tests/data/test_entryexitanalysis.py index 39456b7a3..374b84fc7 100644 --- a/tests/data/test_entryexitanalysis.py +++ b/tests/data/test_entryexitanalysis.py @@ -168,6 +168,7 @@ def test_backtest_analysis_on_entry_and_rejected_signals_nomock( assert "close (exit)" in captured.out assert "rsi (exit)" in captured.out assert "52.829" in captured.out + assert "profit_abs" in captured.out # test group 1 args = get_args(base_args + ["--analysis-groups", "1"])