From 2b89f643b7bd74332be399238012ddedc60acba9 Mon Sep 17 00:00:00 2001 From: Wagner Costa Date: Wed, 28 Dec 2022 19:03:41 -0300 Subject: [PATCH] adjust backtest to new feature engineering functions --- freqtrade/freqai/freqai_interface.py | 32 ++++++++++++++++++---------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/freqtrade/freqai/freqai_interface.py b/freqtrade/freqai/freqai_interface.py index 9025f358a..accd3373f 100644 --- a/freqtrade/freqai/freqai_interface.py +++ b/freqtrade/freqai/freqai_interface.py @@ -149,12 +149,9 @@ class IFreqaiModel(ABC): # the concatenated results for the full backtesting period back to the strategy. elif not self.follow_mode: self.dk = FreqaiDataKitchen(self.config, self.live, metadata["pair"]) - dataframe = self.dk.use_strategy_to_populate_indicators( - strategy, prediction_dataframe=dataframe, pair=metadata["pair"] - ) if not self.config.get("freqai_backtest_live_models", False): logger.info(f"Training {len(self.dk.training_timeranges)} timeranges") - dk = self.start_backtesting(dataframe, metadata, self.dk) + dk = self.start_backtesting(dataframe, metadata, self.dk, strategy) dataframe = dk.remove_features_from_df(dk.return_dataframe) else: logger.info( @@ -255,7 +252,7 @@ class IFreqaiModel(ABC): self.dd.save_metric_tracker_to_disk() def start_backtesting( - self, dataframe: DataFrame, metadata: dict, dk: FreqaiDataKitchen + self, dataframe: DataFrame, metadata: dict, dk: FreqaiDataKitchen, strategy: IStrategy ) -> FreqaiDataKitchen: """ The main broad execution for backtesting. For backtesting, each pair enters and then gets @@ -267,12 +264,14 @@ class IFreqaiModel(ABC): :param dataframe: DataFrame = strategy passed dataframe :param metadata: Dict = pair metadata :param dk: FreqaiDataKitchen = Data management/analysis tool associated to present pair only + :param strategy: Strategy to train on :return: FreqaiDataKitchen = Data management/analysis tool associated to present pair only """ self.pair_it += 1 train_it = 0 + populate_indicators = True # Loop enforcing the sliding window training/backtesting paradigm # tr_train is the training time range e.g. 1 historical month # tr_backtest is the backtesting time range e.g. the week directly @@ -301,14 +300,26 @@ class IFreqaiModel(ABC): dk.set_new_model_names(pair, timestamp_model_id) if dk.check_if_backtest_prediction_is_valid(len_backtest_df): - self.dd.load_metadata(dk) - dk.find_features(dataframe) - self.check_if_feature_list_matches_strategy(dk) + # self.dd.load_metadata(dk) + # dk.find_features(dataframe) + # self.check_if_feature_list_matches_strategy(dk) append_df = dk.get_backtesting_prediction() dk.append_predictions(append_df) else: - dataframe_train = dk.slice_dataframe(tr_train, dataframe) - dataframe_backtest = dk.slice_dataframe(tr_backtest, dataframe) + if populate_indicators: + dataframe = self.dk.use_strategy_to_populate_indicators( + strategy, prediction_dataframe=dataframe, pair=metadata["pair"] + ) + populate_indicators = False + + dataframe_base_train = dataframe.loc[dataframe["date"] < tr_train.stopdt, :] + dataframe_base_train = strategy.set_freqai_targets(dataframe_base_train) + dataframe_base_backtest = dataframe.loc[dataframe["date"] < tr_backtest.stopdt, :] + dataframe_base_backtest = strategy.set_freqai_targets(dataframe_base_backtest) + + dataframe_train = dk.slice_dataframe(tr_train, dataframe_base_train) + dataframe_backtest = dk.slice_dataframe(tr_backtest, dataframe_base_backtest) + if not self.model_exists(dk): dk.find_features(dataframe_train) dk.find_labels(dataframe_train) @@ -913,7 +924,6 @@ class IFreqaiModel(ABC): dk.return_dataframe = dk.return_dataframe.drop(columns=list(columns_to_drop)) dk.return_dataframe = pd.merge( dk.return_dataframe, saved_dataframe, how='left', left_on='date', right_on="date_pred") - # dk.return_dataframe = dk.return_dataframe[saved_dataframe.columns].fillna(0) return dk # Following methods which are overridden by user made prediction models.