From 5b826150dff2c2d0f04b4a93ab802a5ce8b50c63 Mon Sep 17 00:00:00 2001 From: Wagner Costa Santos Date: Mon, 5 Sep 2022 17:43:28 -0300 Subject: [PATCH] fix hyperopt - freqai --- freqtrade/freqai/data_drawer.py | 29 +++++++++++++++++++++++----- freqtrade/freqai/freqai_interface.py | 7 +++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/freqtrade/freqai/data_drawer.py b/freqtrade/freqai/data_drawer.py index 9eeabef8f..a1ecb7654 100644 --- a/freqtrade/freqai/data_drawer.py +++ b/freqtrade/freqai/data_drawer.py @@ -93,6 +93,16 @@ class FreqaiDataDrawer: "model_filename": "", "trained_timestamp": 0, "priority": 1, "first": True, "data_path": "", "extras": {}} + def __getstate__(self): + """ + Return state values to be pickled. + It's necessary to allow serialization in hyperopt + """ + return ({ + "pair_dict": self.pair_dict, + "pair_dictionary_path": self.pair_dictionary_path + }) + def load_drawer_from_disk(self): """ Locate and load a previously saved data drawer full of all pair model metadata in @@ -155,14 +165,23 @@ class FreqaiDataDrawer: # create a backup shutil.copy(self.historic_predictions_path, self.historic_predictions_bkp_path) - def save_drawer_to_disk(self): + def save_drawer_to_disk(self, live=False): """ Save data drawer full of all pair model metadata in present model folder. """ - with self.save_lock: + if live: + with self.save_lock: + with open(self.pair_dictionary_path, 'w') as fp: + rapidjson.dump( + self.pair_dict, fp, default=self.np_encoder, + number_mode=rapidjson.NM_NATIVE + ) + else: + # save_lock it's not working with hyperopt with open(self.pair_dictionary_path, 'w') as fp: - rapidjson.dump(self.pair_dict, fp, default=self.np_encoder, - number_mode=rapidjson.NM_NATIVE) + rapidjson.dump( + self.pair_dict, fp, default=self.np_encoder, + number_mode=rapidjson.NM_NATIVE) def save_follower_dict_to_disk(self): """ @@ -437,7 +456,7 @@ class FreqaiDataDrawer: self.model_dictionary[coin] = model self.pair_dict[coin]["model_filename"] = dk.model_filename self.pair_dict[coin]["data_path"] = str(dk.data_path) - self.save_drawer_to_disk() + self.save_drawer_to_disk(dk.live) return diff --git a/freqtrade/freqai/freqai_interface.py b/freqtrade/freqai/freqai_interface.py index a9c21fb65..9a05e8383 100644 --- a/freqtrade/freqai/freqai_interface.py +++ b/freqtrade/freqai/freqai_interface.py @@ -90,6 +90,13 @@ class IFreqaiModel(ABC): self._threads: List[threading.Thread] = [] self._stop_event = threading.Event() + def __getstate__(self): + """ + Return state values to be pickled. + It's necessary to allow serialization in hyperopt + """ + return ({"dd": self.dd}) + def assert_config(self, config: Dict[str, Any]) -> None: if not config.get("freqai", {}):