backup historical predictions pickle and load the backup in case of corruption
This commit is contained in:
		| @@ -76,6 +76,8 @@ class FreqaiDataDrawer: | |||||||
|             self.full_path / f"follower_dictionary-{self.follower_name}.json" |             self.full_path / f"follower_dictionary-{self.follower_name}.json" | ||||||
|         ) |         ) | ||||||
|         self.historic_predictions_path = Path(self.full_path / "historic_predictions.pkl") |         self.historic_predictions_path = Path(self.full_path / "historic_predictions.pkl") | ||||||
|  |         self.historic_predictions_bkp_path = Path( | ||||||
|  |             self.full_path / "historic_predictions.backup.pkl") | ||||||
|         self.pair_dictionary_path = Path(self.full_path / "pair_dictionary.json") |         self.pair_dictionary_path = Path(self.full_path / "pair_dictionary.json") | ||||||
|         self.follow_mode = follow_mode |         self.follow_mode = follow_mode | ||||||
|         if follow_mode: |         if follow_mode: | ||||||
| @@ -118,6 +120,7 @@ class FreqaiDataDrawer: | |||||||
|         """ |         """ | ||||||
|         exists = self.historic_predictions_path.is_file() |         exists = self.historic_predictions_path.is_file() | ||||||
|         if exists: |         if exists: | ||||||
|  |             try: | ||||||
|                 with open(self.historic_predictions_path, "rb") as fp: |                 with open(self.historic_predictions_path, "rb") as fp: | ||||||
|                     self.historic_predictions = cloudpickle.load(fp) |                     self.historic_predictions = cloudpickle.load(fp) | ||||||
|                 logger.info( |                 logger.info( | ||||||
| @@ -125,6 +128,13 @@ class FreqaiDataDrawer: | |||||||
|                     "that statistics may be inaccurate if the bot has been offline for " |                     "that statistics may be inaccurate if the bot has been offline for " | ||||||
|                     "an extended period of time." |                     "an extended period of time." | ||||||
|                 ) |                 ) | ||||||
|  |             except EOFError: | ||||||
|  |                 logger.warning( | ||||||
|  |                     'Historical prediction file was corrupted. Trying to load backup file.') | ||||||
|  |                 with open(self.historic_predictions_bkp_path, "rb") as fp: | ||||||
|  |                     self.historic_predictions = cloudpickle.load(fp) | ||||||
|  |                 logger.warning('FreqAI successfully loaded the backup historical predictions file.') | ||||||
|  |  | ||||||
|         elif not self.follow_mode: |         elif not self.follow_mode: | ||||||
|             logger.info("Could not find existing historic_predictions, starting from scratch") |             logger.info("Could not find existing historic_predictions, starting from scratch") | ||||||
|         else: |         else: | ||||||
| @@ -142,6 +152,9 @@ class FreqaiDataDrawer: | |||||||
|         with open(self.historic_predictions_path, "wb") as fp: |         with open(self.historic_predictions_path, "wb") as fp: | ||||||
|             cloudpickle.dump(self.historic_predictions, fp, protocol=cloudpickle.DEFAULT_PROTOCOL) |             cloudpickle.dump(self.historic_predictions, fp, protocol=cloudpickle.DEFAULT_PROTOCOL) | ||||||
|  |  | ||||||
|  |         # 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): | ||||||
|         """ |         """ | ||||||
|         Save data drawer full of all pair model metadata in present model folder. |         Save data drawer full of all pair model metadata in present model folder. | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user