From a20651efd8f99bef142e1219b4c80d3f2f4283e2 Mon Sep 17 00:00:00 2001 From: robcaulk Date: Mon, 30 May 2022 11:37:05 +0200 Subject: [PATCH] Increase performance by only predicting on most recent candle instead of full strat provided dataframe. Collect predictions and store them so that we can feed true predictions back to strategy (so that frequi isnt updating historic predictions based on newly trained models). --- freqtrade/freqai/data_drawer.py | 26 ++++++++++++++++++++++++++ freqtrade/freqai/data_kitchen.py | 2 +- freqtrade/freqai/freqai_interface.py | 12 ++++++++++-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/freqtrade/freqai/data_drawer.py b/freqtrade/freqai/data_drawer.py index 477f45d84..6a5393cb8 100644 --- a/freqtrade/freqai/data_drawer.py +++ b/freqtrade/freqai/data_drawer.py @@ -1,4 +1,5 @@ +import copy import json import logging from pathlib import Path @@ -24,6 +25,7 @@ class FreqaiDataDrawer: self.pair_dict: Dict[str, Any] = {} # dictionary holding all actively inferenced models in memory given a model filename self.model_dictionary: Dict[str, Any] = {} + self.model_return_values: Dict[str, Any] = {} self.pair_data_dict: Dict[str, Any] = {} self.full_path = full_path self.load_drawer_from_disk() @@ -83,3 +85,27 @@ class FreqaiDataDrawer: self.pair_dict[p]['priority'] -= 1 # send pair to end of queue self.pair_dict[pair]['priority'] = len(self.pair_dict) + + def set_initial_return_values(self, pair, dh): + self.model_return_values[pair] = {} + self.model_return_values[pair]['predictions'] = dh.full_predictions + self.model_return_values[pair]['do_preds'] = dh.full_do_predict + self.model_return_values[pair]['target_mean'] = dh.full_target_mean + self.model_return_values[pair]['target_std'] = dh.full_target_std + + def append_model_predictions(self, pair, predictions, do_preds, + target_mean, target_std, dh) -> None: + + pred_store = self.model_return_values[pair]['predictions'] + do_pred_store = self.model_return_values[pair]['do_preds'] + tm_store = self.model_return_values[pair]['target_mean'] + ts_store = self.model_return_values[pair]['target_std'] + pred_store = np.append(pred_store[1:], predictions[-1]) + do_pred_store = np.append(do_pred_store[1:], do_preds[-1]) + tm_store = np.append(tm_store[1:], target_mean) + ts_store = np.append(ts_store[1:], target_std) + + dh.full_predictions = copy.deepcopy(pred_store) + dh.full_do_predict = copy.deepcopy(do_pred_store) + dh.full_target_mean = copy.deepcopy(tm_store) + dh.full_target_std = copy.deepcopy(ts_store) diff --git a/freqtrade/freqai/data_kitchen.py b/freqtrade/freqai/data_kitchen.py index 5ab066bfd..2b8306b5c 100644 --- a/freqtrade/freqai/data_kitchen.py +++ b/freqtrade/freqai/data_kitchen.py @@ -623,7 +623,7 @@ class FreqaiDataKitchen: Append backtest prediction from current backtest period to all previous periods """ - ones = np.ones(len_dataframe) + ones = np.ones(len(predictions)) target_mean, target_std = ones * self.data["target_mean"], ones * self.data["target_std"] self.full_predictions = np.append(self.full_predictions, predictions) diff --git a/freqtrade/freqai/freqai_interface.py b/freqtrade/freqai/freqai_interface.py index 4d02afb69..386fab9fc 100644 --- a/freqtrade/freqai/freqai_interface.py +++ b/freqtrade/freqai/freqai_interface.py @@ -219,8 +219,16 @@ class IFreqaiModel(ABC): self.check_if_feature_list_matches_strategy(dataframe, dh) - preds, do_preds = self.predict(dataframe, dh) - dh.append_predictions(preds, do_preds, len(dataframe)) + if metadata['pair'] not in self.data_drawer.model_return_values: + preds, do_preds = self.predict(dataframe, dh) + dh.append_predictions(preds, do_preds, len(dataframe)) + dh.fill_predictions(len(dataframe)) + self.data_drawer.set_initial_return_values(metadata['pair'], dh) + else: + preds, do_preds = self.predict(dataframe.iloc[-2:], dh) + self.data_drawer.append_model_predictions(metadata['pair'], preds, do_preds, + self.dh.data["target_mean"], + self.dh.data["target_std"], dh) return dh