From ba305e93edb49defc42f9a8b01b957e33984722f Mon Sep 17 00:00:00 2001 From: Patel Kaushal <36811899+koradiyakaushal@users.noreply.github.com> Date: Thu, 21 Apr 2022 18:35:41 +0530 Subject: [PATCH 1/3] Ref: timeseries friendly merge_ordered in merge_informative_pair function --- freqtrade/strategy/strategy_helper.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/freqtrade/strategy/strategy_helper.py b/freqtrade/strategy/strategy_helper.py index a36cb3dbb..fef5fb812 100644 --- a/freqtrade/strategy/strategy_helper.py +++ b/freqtrade/strategy/strategy_helper.py @@ -56,12 +56,18 @@ def merge_informative_pair(dataframe: pd.DataFrame, informative: pd.DataFrame, # Combine the 2 dataframes # all indicators on the informative sample MUST be calculated before this point - dataframe = pd.merge(dataframe, informative, left_on='date', - right_on=date_merge, how='left') + if ffill: + # https://pandas.pydata.org/docs/user_guide/merging.html#timeseries-friendly-merging + # merge_ordered - ffill method is 2.5x faster than seperate ffill() + dataframe = pd.merge_ordered(dataframe, informative, fill_method="ffill", left_on='date', + right_on=date_merge, how='left') + else: + dataframe = pd.merge(dataframe, informative, left_on='date', + right_on=date_merge, how='left') dataframe = dataframe.drop(date_merge, axis=1) - if ffill: - dataframe = dataframe.ffill() + # if ffill: + # dataframe = dataframe.ffill() return dataframe From 84f5a4d5bc0e116ba1b7a5a476c9d4e717561cf3 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 23 Apr 2022 10:51:24 +0200 Subject: [PATCH 2/3] Fix indentation --- freqtrade/strategy/strategy_helper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/strategy/strategy_helper.py b/freqtrade/strategy/strategy_helper.py index fef5fb812..43728dc1f 100644 --- a/freqtrade/strategy/strategy_helper.py +++ b/freqtrade/strategy/strategy_helper.py @@ -60,10 +60,10 @@ def merge_informative_pair(dataframe: pd.DataFrame, informative: pd.DataFrame, # https://pandas.pydata.org/docs/user_guide/merging.html#timeseries-friendly-merging # merge_ordered - ffill method is 2.5x faster than seperate ffill() dataframe = pd.merge_ordered(dataframe, informative, fill_method="ffill", left_on='date', - right_on=date_merge, how='left') + right_on=date_merge, how='left') else: dataframe = pd.merge(dataframe, informative, left_on='date', - right_on=date_merge, how='left') + right_on=date_merge, how='left') dataframe = dataframe.drop(date_merge, axis=1) # if ffill: From a2af7b4fd82ef5b1697aa427967a70cfff53f876 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 23 Apr 2022 11:25:20 +0200 Subject: [PATCH 3/3] Test non-ffill approach --- tests/strategy/test_strategy_helpers.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/strategy/test_strategy_helpers.py b/tests/strategy/test_strategy_helpers.py index 65fb9f6dc..244fd3919 100644 --- a/tests/strategy/test_strategy_helpers.py +++ b/tests/strategy/test_strategy_helpers.py @@ -68,6 +68,21 @@ def test_merge_informative_pair(): assert result.iloc[7]['date_1h'] == result.iloc[4]['date'] assert result.iloc[8]['date_1h'] == result.iloc[4]['date'] + informative = generate_test_data('1h', 40) + result = merge_informative_pair(data, informative, '15m', '1h', ffill=False) + # First 3 rows are empty + assert result.iloc[0]['date_1h'] is pd.NaT + assert result.iloc[1]['date_1h'] is pd.NaT + assert result.iloc[2]['date_1h'] is pd.NaT + # Next 4 rows contain the starting date (0:00) + assert result.iloc[3]['date_1h'] == result.iloc[0]['date'] + assert result.iloc[4]['date_1h'] is pd.NaT + assert result.iloc[5]['date_1h'] is pd.NaT + assert result.iloc[6]['date_1h'] is pd.NaT + # Next 4 rows contain the next Hourly date original date row 4 + assert result.iloc[7]['date_1h'] == result.iloc[4]['date'] + assert result.iloc[8]['date_1h'] is pd.NaT + def test_merge_informative_pair_same(): data = generate_test_data('15m', 40)