From b70f18f4c36c13aa3930fa812148fd452a48e18b Mon Sep 17 00:00:00 2001 From: robcaulk Date: Sun, 2 Oct 2022 18:33:39 +0200 Subject: [PATCH 1/4] add close price and date to historic_predictions --- freqtrade/freqai/data_drawer.py | 6 +++++- freqtrade/freqai/freqai_interface.py | 9 ++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/freqtrade/freqai/data_drawer.py b/freqtrade/freqai/data_drawer.py index 471f6875c..0d3bdea29 100644 --- a/freqtrade/freqai/data_drawer.py +++ b/freqtrade/freqai/data_drawer.py @@ -257,7 +257,7 @@ class FreqaiDataDrawer: def append_model_predictions(self, pair: str, predictions: DataFrame, do_preds: NDArray[np.int_], - dk: FreqaiDataKitchen, len_df: int) -> None: + dk: FreqaiDataKitchen, strat_df: DataFrame) -> None: """ Append model predictions to historic predictions dataframe, then set the strategy return dataframe to the tail of the historic predictions. The length of @@ -266,6 +266,7 @@ class FreqaiDataDrawer: historic predictions. """ + len_df = len(strat_df) index = self.historic_predictions[pair].index[-1:] columns = self.historic_predictions[pair].columns @@ -293,6 +294,9 @@ class FreqaiDataDrawer: for return_str in rets: df[return_str].iloc[-1] = rets[return_str] + df['close_price'].iloc[-1] = strat_df['close'].iloc[-1] + df['date_pred'].iloc[-1] = strat_df['date'].iloc[-1] + self.model_return_values[pair] = df.tail(len_df).reset_index(drop=True) def attach_return_values_to_return_dataframe( diff --git a/freqtrade/freqai/freqai_interface.py b/freqtrade/freqai/freqai_interface.py index 78539bae5..5ac7bc32c 100644 --- a/freqtrade/freqai/freqai_interface.py +++ b/freqtrade/freqai/freqai_interface.py @@ -393,7 +393,7 @@ class IFreqaiModel(ABC): # allows FreqUI to show full return values. pred_df, do_preds = self.predict(dataframe, dk) if pair not in self.dd.historic_predictions: - self.set_initial_historic_predictions(pred_df, dk, pair) + self.set_initial_historic_predictions(pred_df, dk, pair, dataframe) self.dd.set_initial_return_values(pair, pred_df) dk.return_dataframe = self.dd.attach_return_values_to_return_dataframe(pair, dataframe) @@ -414,7 +414,7 @@ class IFreqaiModel(ABC): if self.freqai_info.get('fit_live_predictions_candles', 0) and self.live: self.fit_live_predictions(dk, pair) - self.dd.append_model_predictions(pair, pred_df, do_preds, dk, len(dataframe)) + self.dd.append_model_predictions(pair, pred_df, do_preds, dk, dataframe) dk.return_dataframe = self.dd.attach_return_values_to_return_dataframe(pair, dataframe) return @@ -583,7 +583,7 @@ class IFreqaiModel(ABC): self.dd.purge_old_models() def set_initial_historic_predictions( - self, pred_df: DataFrame, dk: FreqaiDataKitchen, pair: str + self, pred_df: DataFrame, dk: FreqaiDataKitchen, pair: str, strat_df: DataFrame ) -> None: """ This function is called only if the datadrawer failed to load an @@ -626,6 +626,9 @@ class IFreqaiModel(ABC): for return_str in dk.data['extra_returns_per_train']: hist_preds_df[return_str] = 0 + hist_preds_df['close_price'] = strat_df['close'] + hist_preds_df['date_pred'] = strat_df['date'] + # # for keras type models, the conv_window needs to be prepended so # # viewing is correct in frequi if self.freqai_info.get('keras', False) or self.ft_params.get('inlier_metric_window', 0): From 6ecd92de4a8cdcccd364aa56098b3b4ba89f93de Mon Sep 17 00:00:00 2001 From: Robert Caulk Date: Mon, 3 Oct 2022 09:55:57 +0200 Subject: [PATCH 2/4] Allow updating without changing identifier --- freqtrade/freqai/data_drawer.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/freqtrade/freqai/data_drawer.py b/freqtrade/freqai/data_drawer.py index 0d3bdea29..603c477a0 100644 --- a/freqtrade/freqai/data_drawer.py +++ b/freqtrade/freqai/data_drawer.py @@ -294,6 +294,12 @@ class FreqaiDataDrawer: for return_str in rets: df[return_str].iloc[-1] = rets[return_str] + # this logic carries users between version without needing to + # change their identifier + if 'close_price' not in df.columns: + df['close_price'] = 0 + df['date_pred'] = 0 + df['close_price'].iloc[-1] = strat_df['close'].iloc[-1] df['date_pred'].iloc[-1] = strat_df['date'].iloc[-1] From 265795824b99fd997e5e21e6be2b3862f01fa461 Mon Sep 17 00:00:00 2001 From: Robert Caulk Date: Mon, 3 Oct 2022 11:58:22 +0200 Subject: [PATCH 3/4] make default type for close_price and date_pred np.nan --- freqtrade/freqai/data_drawer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/freqai/data_drawer.py b/freqtrade/freqai/data_drawer.py index 603c477a0..d6dbe8c6c 100644 --- a/freqtrade/freqai/data_drawer.py +++ b/freqtrade/freqai/data_drawer.py @@ -297,8 +297,8 @@ class FreqaiDataDrawer: # this logic carries users between version without needing to # change their identifier if 'close_price' not in df.columns: - df['close_price'] = 0 - df['date_pred'] = 0 + df['close_price'] = np.nan + df['date_pred'] = np.nan df['close_price'].iloc[-1] = strat_df['close'].iloc[-1] df['date_pred'].iloc[-1] = strat_df['date'].iloc[-1] From 3585742b43db7eddfb3cd05f580a763fa24c6bfd Mon Sep 17 00:00:00 2001 From: Robert Caulk Date: Mon, 3 Oct 2022 17:28:45 +0200 Subject: [PATCH 4/4] remove trailing whitespace --- freqtrade/freqai/data_drawer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freqtrade/freqai/data_drawer.py b/freqtrade/freqai/data_drawer.py index d6dbe8c6c..cde72bfb5 100644 --- a/freqtrade/freqai/data_drawer.py +++ b/freqtrade/freqai/data_drawer.py @@ -294,7 +294,7 @@ class FreqaiDataDrawer: for return_str in rets: df[return_str].iloc[-1] = rets[return_str] - # this logic carries users between version without needing to + # this logic carries users between version without needing to # change their identifier if 'close_price' not in df.columns: df['close_price'] = np.nan