From f1e831a7b8acfcdab524b8325a961fe5491c862c Mon Sep 17 00:00:00 2001 From: robcaulk Date: Sun, 26 Mar 2023 13:43:59 +0200 Subject: [PATCH 1/5] fix bug in backtest target setting --- freqtrade/freqai/data_kitchen.py | 8 ++++---- freqtrade/freqai/freqai_interface.py | 7 +++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/freqtrade/freqai/data_kitchen.py b/freqtrade/freqai/data_kitchen.py index 52d487b08..21b41db2d 100644 --- a/freqtrade/freqai/data_kitchen.py +++ b/freqtrade/freqai/data_kitchen.py @@ -1291,7 +1291,7 @@ class FreqaiDataKitchen: return dataframe - def use_strategy_to_populate_indicators( + def use_strategy_to_populate_indicators( # noqa: C901 self, strategy: IStrategy, corr_dataframes: dict = {}, @@ -1362,12 +1362,12 @@ class FreqaiDataKitchen: dataframe = self.populate_features(dataframe.copy(), corr_pair, strategy, corr_dataframes, base_dataframes, True) - dataframe = strategy.set_freqai_targets(dataframe.copy(), metadata=metadata) + if self.live: + dataframe = strategy.set_freqai_targets(dataframe.copy(), metadata=metadata) + dataframe = self.remove_special_chars_from_feature_names(dataframe) self.get_unique_classes_from_labels(dataframe) - dataframe = self.remove_special_chars_from_feature_names(dataframe) - if self.config.get('reduce_df_footprint', False): dataframe = reduce_dataframe_footprint(dataframe) diff --git a/freqtrade/freqai/freqai_interface.py b/freqtrade/freqai/freqai_interface.py index b657bd811..fe62adea9 100644 --- a/freqtrade/freqai/freqai_interface.py +++ b/freqtrade/freqai/freqai_interface.py @@ -306,7 +306,7 @@ class IFreqaiModel(ABC): if check_features: self.dd.load_metadata(dk) dataframe_dummy_features = self.dk.use_strategy_to_populate_indicators( - strategy, prediction_dataframe=dataframe.tail(1), pair=metadata["pair"] + strategy, prediction_dataframe=dataframe.tail(1), pair=pair ) dk.find_features(dataframe_dummy_features) self.check_if_feature_list_matches_strategy(dk) @@ -316,7 +316,7 @@ class IFreqaiModel(ABC): else: if populate_indicators: dataframe = self.dk.use_strategy_to_populate_indicators( - strategy, prediction_dataframe=dataframe, pair=metadata["pair"] + strategy, prediction_dataframe=dataframe, pair=pair ) populate_indicators = False @@ -332,6 +332,9 @@ class IFreqaiModel(ABC): dataframe_train = dk.slice_dataframe(tr_train, dataframe_base_train) dataframe_backtest = dk.slice_dataframe(tr_backtest, dataframe_base_backtest) + dataframe_train = dk.remove_special_chars_from_feature_names(dataframe_train) + dataframe_backtest = dk.remove_special_chars_from_feature_names(dataframe_backtest) + if not self.model_exists(dk): dk.find_features(dataframe_train) dk.find_labels(dataframe_train) From 55781e7f10fc66aaae4a0039766a8bde94a82355 Mon Sep 17 00:00:00 2001 From: robcaulk Date: Sun, 26 Mar 2023 19:22:52 +0200 Subject: [PATCH 2/5] fix tests --- tests/freqai/conftest.py | 2 ++ tests/freqai/test_freqai_datadrawer.py | 7 ++++++- tests/freqai/test_freqai_datakitchen.py | 1 + tests/freqai/test_freqai_interface.py | 6 ++++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/freqai/conftest.py b/tests/freqai/conftest.py index e140ee80b..75034767d 100644 --- a/tests/freqai/conftest.py +++ b/tests/freqai/conftest.py @@ -119,6 +119,7 @@ def make_unfiltered_dataframe(mocker, freqai_conf): freqai = strategy.freqai freqai.live = True freqai.dk = FreqaiDataKitchen(freqai_conf) + freqai.dk.live = True freqai.dk.pair = "ADA/BTC" data_load_timerange = TimeRange.parse_timerange("20180110-20180130") freqai.dd.load_all_pair_histories(data_load_timerange, freqai.dk) @@ -152,6 +153,7 @@ def make_data_dictionary(mocker, freqai_conf): freqai = strategy.freqai freqai.live = True freqai.dk = FreqaiDataKitchen(freqai_conf) + freqai.dk.live = True freqai.dk.pair = "ADA/BTC" data_load_timerange = TimeRange.parse_timerange("20180110-20180130") freqai.dd.load_all_pair_histories(data_load_timerange, freqai.dk) diff --git a/tests/freqai/test_freqai_datadrawer.py b/tests/freqai/test_freqai_datadrawer.py index da3b8f9c1..8ab2c75da 100644 --- a/tests/freqai/test_freqai_datadrawer.py +++ b/tests/freqai/test_freqai_datadrawer.py @@ -19,6 +19,7 @@ def test_update_historic_data(mocker, freqai_conf): freqai = strategy.freqai freqai.live = True freqai.dk = FreqaiDataKitchen(freqai_conf) + freqai.dk.live = True timerange = TimeRange.parse_timerange("20180110-20180114") freqai.dd.load_all_pair_histories(timerange, freqai.dk) @@ -41,6 +42,7 @@ def test_load_all_pairs_histories(mocker, freqai_conf): freqai = strategy.freqai freqai.live = True freqai.dk = FreqaiDataKitchen(freqai_conf) + freqai.dk.live = True timerange = TimeRange.parse_timerange("20180110-20180114") freqai.dd.load_all_pair_histories(timerange, freqai.dk) @@ -60,6 +62,7 @@ def test_get_base_and_corr_dataframes(mocker, freqai_conf): freqai = strategy.freqai freqai.live = True freqai.dk = FreqaiDataKitchen(freqai_conf) + freqai.dk.live = True timerange = TimeRange.parse_timerange("20180110-20180114") freqai.dd.load_all_pair_histories(timerange, freqai.dk) sub_timerange = TimeRange.parse_timerange("20180111-20180114") @@ -87,6 +90,7 @@ def test_use_strategy_to_populate_indicators(mocker, freqai_conf): freqai = strategy.freqai freqai.live = True freqai.dk = FreqaiDataKitchen(freqai_conf) + freqai.dk.live = True timerange = TimeRange.parse_timerange("20180110-20180114") freqai.dd.load_all_pair_histories(timerange, freqai.dk) sub_timerange = TimeRange.parse_timerange("20180111-20180114") @@ -103,8 +107,9 @@ def test_get_timerange_from_live_historic_predictions(mocker, freqai_conf): exchange = get_patched_exchange(mocker, freqai_conf) strategy.dp = DataProvider(freqai_conf, exchange) freqai = strategy.freqai - freqai.live = True + freqai.live = False freqai.dk = FreqaiDataKitchen(freqai_conf) + freqai.dk.live = False timerange = TimeRange.parse_timerange("20180126-20180130") freqai.dd.load_all_pair_histories(timerange, freqai.dk) sub_timerange = TimeRange.parse_timerange("20180128-20180130") diff --git a/tests/freqai/test_freqai_datakitchen.py b/tests/freqai/test_freqai_datakitchen.py index 95665a775..3f0fc697d 100644 --- a/tests/freqai/test_freqai_datakitchen.py +++ b/tests/freqai/test_freqai_datakitchen.py @@ -180,6 +180,7 @@ def test_get_full_model_path(mocker, freqai_conf, model): freqai = strategy.freqai freqai.live = True freqai.dk = FreqaiDataKitchen(freqai_conf) + freqai.dk.live = True timerange = TimeRange.parse_timerange("20180110-20180130") freqai.dd.load_all_pair_histories(timerange, freqai.dk) diff --git a/tests/freqai/test_freqai_interface.py b/tests/freqai/test_freqai_interface.py index 3b370aea4..e149ca517 100644 --- a/tests/freqai/test_freqai_interface.py +++ b/tests/freqai/test_freqai_interface.py @@ -87,6 +87,7 @@ def test_extract_data_and_train_model_Standard(mocker, freqai_conf, model, pca, freqai.live = True freqai.can_short = can_short freqai.dk = FreqaiDataKitchen(freqai_conf) + freqai.dk.live = True freqai.dk.set_paths('ADA/BTC', 10000) timerange = TimeRange.parse_timerange("20180110-20180130") freqai.dd.load_all_pair_histories(timerange, freqai.dk) @@ -135,6 +136,7 @@ def test_extract_data_and_train_model_MultiTargets(mocker, freqai_conf, model, s freqai = strategy.freqai freqai.live = True freqai.dk = FreqaiDataKitchen(freqai_conf) + freqai.dk.live = True timerange = TimeRange.parse_timerange("20180110-20180130") freqai.dd.load_all_pair_histories(timerange, freqai.dk) @@ -178,6 +180,7 @@ def test_extract_data_and_train_model_Classifiers(mocker, freqai_conf, model): freqai = strategy.freqai freqai.live = True freqai.dk = FreqaiDataKitchen(freqai_conf) + freqai.dk.live = True timerange = TimeRange.parse_timerange("20180110-20180130") freqai.dd.load_all_pair_histories(timerange, freqai.dk) @@ -238,6 +241,7 @@ def test_start_backtesting(mocker, freqai_conf, model, num_files, strat, caplog) freqai = strategy.freqai freqai.live = False freqai.dk = FreqaiDataKitchen(freqai_conf) + freqai.dk.live = True timerange = TimeRange.parse_timerange("20180110-20180130") freqai.dd.load_all_pair_histories(timerange, freqai.dk) sub_timerange = TimeRange.parse_timerange("20180110-20180130") @@ -394,6 +398,7 @@ def test_principal_component_analysis(mocker, freqai_conf): freqai = strategy.freqai freqai.live = True freqai.dk = FreqaiDataKitchen(freqai_conf) + freqai.dk.live = True timerange = TimeRange.parse_timerange("20180110-20180130") freqai.dd.load_all_pair_histories(timerange, freqai.dk) @@ -425,6 +430,7 @@ def test_plot_feature_importance(mocker, freqai_conf): freqai = strategy.freqai freqai.live = True freqai.dk = FreqaiDataKitchen(freqai_conf) + freqai.dk.live = True timerange = TimeRange.parse_timerange("20180110-20180130") freqai.dd.load_all_pair_histories(timerange, freqai.dk) From 3cabcabcbd5230de3a9c8cb45759d9e540ea4c4f Mon Sep 17 00:00:00 2001 From: robcaulk Date: Mon, 27 Mar 2023 15:23:01 +0200 Subject: [PATCH 3/5] ensure labels are properly defined in backtesting --- freqtrade/freqai/freqai_interface.py | 1 + tests/freqai/test_freqai_interface.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/freqtrade/freqai/freqai_interface.py b/freqtrade/freqai/freqai_interface.py index fe62adea9..7444bf404 100644 --- a/freqtrade/freqai/freqai_interface.py +++ b/freqtrade/freqai/freqai_interface.py @@ -334,6 +334,7 @@ class IFreqaiModel(ABC): dataframe_train = dk.remove_special_chars_from_feature_names(dataframe_train) dataframe_backtest = dk.remove_special_chars_from_feature_names(dataframe_backtest) + dk.get_unique_classes_from_labels(dataframe_train) if not self.model_exists(dk): dk.find_features(dataframe_train) diff --git a/tests/freqai/test_freqai_interface.py b/tests/freqai/test_freqai_interface.py index e149ca517..5f8071446 100644 --- a/tests/freqai/test_freqai_interface.py +++ b/tests/freqai/test_freqai_interface.py @@ -241,7 +241,6 @@ def test_start_backtesting(mocker, freqai_conf, model, num_files, strat, caplog) freqai = strategy.freqai freqai.live = False freqai.dk = FreqaiDataKitchen(freqai_conf) - freqai.dk.live = True timerange = TimeRange.parse_timerange("20180110-20180130") freqai.dd.load_all_pair_histories(timerange, freqai.dk) sub_timerange = TimeRange.parse_timerange("20180110-20180130") @@ -375,6 +374,9 @@ def test_backtesting_fit_live_predictions(mocker, freqai_conf, caplog): sub_timerange = TimeRange.parse_timerange("20180129-20180130") corr_df, base_df = freqai.dd.get_base_and_corr_dataframes(sub_timerange, "LTC/BTC", freqai.dk) df = freqai.dk.use_strategy_to_populate_indicators(strategy, corr_df, base_df, "LTC/BTC") + df = strategy.set_freqai_targets(df.copy(), metadata={"pair": "LTC/BTC"}) + df = freqai.dk.remove_special_chars_from_feature_names(df) + freqai.dk.get_unique_classes_from_labels(df) freqai.dk.pair = "ADA/BTC" freqai.dk.full_df = df.fillna(0) freqai.dk.full_df From 355fde3bca6f668da1fef36e187f85d09cf0b3f7 Mon Sep 17 00:00:00 2001 From: robcaulk Date: Wed, 29 Mar 2023 22:01:54 +0200 Subject: [PATCH 4/5] revert setting dk to live in test_plot_feature_importances --- tests/freqai/test_freqai_interface.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/freqai/test_freqai_interface.py b/tests/freqai/test_freqai_interface.py index 5f8071446..1122d9e9c 100644 --- a/tests/freqai/test_freqai_interface.py +++ b/tests/freqai/test_freqai_interface.py @@ -432,7 +432,6 @@ def test_plot_feature_importance(mocker, freqai_conf): freqai = strategy.freqai freqai.live = True freqai.dk = FreqaiDataKitchen(freqai_conf) - freqai.dk.live = True timerange = TimeRange.parse_timerange("20180110-20180130") freqai.dd.load_all_pair_histories(timerange, freqai.dk) From c2c97d9f78801ede95191e799d480a3b943a14cd Mon Sep 17 00:00:00 2001 From: robcaulk Date: Sat, 8 Apr 2023 13:20:29 +0200 Subject: [PATCH 5/5] make a fake pair_dict instead of MagicMocking it --- tests/freqai/test_freqai_interface.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/freqai/test_freqai_interface.py b/tests/freqai/test_freqai_interface.py index 1122d9e9c..5c2d39e53 100644 --- a/tests/freqai/test_freqai_interface.py +++ b/tests/freqai/test_freqai_interface.py @@ -432,10 +432,12 @@ def test_plot_feature_importance(mocker, freqai_conf): freqai = strategy.freqai freqai.live = True freqai.dk = FreqaiDataKitchen(freqai_conf) + freqai.dk.live = True timerange = TimeRange.parse_timerange("20180110-20180130") freqai.dd.load_all_pair_histories(timerange, freqai.dk) - freqai.dd.pair_dict = MagicMock() + freqai.dd.pair_dict = {"ADA/BTC": {"model_filename": "fake_name", + "trained_timestamp": 1, "data_path": "", "extras": {}}} data_load_timerange = TimeRange.parse_timerange("20180110-20180130") new_timerange = TimeRange.parse_timerange("20180120-20180130")