From 7bed0450d2c6ae90dd00d98a51b18701be6c4874 Mon Sep 17 00:00:00 2001 From: Wagner Costa Santos Date: Wed, 31 Aug 2022 15:36:29 -0300 Subject: [PATCH] pr review - refactoring backtesting freqai --- freqtrade/freqai/data_kitchen.py | 19 ++++++++++++++----- freqtrade/freqai/freqai_interface.py | 12 ++++++------ tests/freqai/test_freqai_interface.py | 8 ++++---- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/freqtrade/freqai/data_kitchen.py b/freqtrade/freqai/data_kitchen.py index 80b795b8e..8dc94e9ec 100644 --- a/freqtrade/freqai/data_kitchen.py +++ b/freqtrade/freqai/data_kitchen.py @@ -1,7 +1,6 @@ import copy import datetime import logging -import os import shutil from pathlib import Path from typing import Any, Dict, List, Tuple @@ -1108,15 +1107,25 @@ class FreqaiDataKitchen: :param file_name: h5 file name :param root_folder: folder to save h5 file """ - os.makedirs(root_folder, exist_ok=True) - append_df.to_hdf(file_name, key='append_df', mode='w') + backtesting_root = Path( + self.full_path + / root_folder + ) + if not backtesting_root.is_dir(): + backtesting_root.mkdir(parents=True, exist_ok=True) - def get_backtesting_prediction(self, prediction_file_name: str) -> DataFrame: + full_file_path = Path(self.full_path / root_folder / file_name) + append_df.to_hdf(full_file_path, key='append_df', mode='w') + + def get_backtesting_prediction( + self, root_prediction: str, prediction_file_name: str + ) -> DataFrame: """ Retrive from disk the prediction dataframe :param prediction_file_name: prediction file full path :return: :Dataframe: Backtesting prediction from current backtesting period """ - append_df = pd.read_hdf(prediction_file_name) + prediction_path = Path(self.full_path / root_prediction / prediction_file_name) + append_df = pd.read_hdf(prediction_path) return append_df diff --git a/freqtrade/freqai/freqai_interface.py b/freqtrade/freqai/freqai_interface.py index d396113e8..ad64588a7 100644 --- a/freqtrade/freqai/freqai_interface.py +++ b/freqtrade/freqai/freqai_interface.py @@ -234,12 +234,12 @@ class IFreqaiModel(ABC): if self.backtest_prediction_exists( metadata["pair"], dk, trained_timestamp=trained_timestamp_int ): - prediction_filename, _ = self.get_backtesting_prediction_file_name( + prediction_filename, root_prediction = self.get_backtesting_prediction_file_name( metadata["pair"], dk, trained_timestamp=int(trained_timestamp.stopts)) - append_df = dk.get_backtesting_prediction(prediction_filename) + append_df = dk.get_backtesting_prediction(root_prediction, prediction_filename) dk.append_predictions(append_df) else: if not self.model_exists( @@ -680,10 +680,10 @@ class IFreqaiModel(ABC): :boolean: whether the prediction file exists or not. """ if not self.live: - prediction_file_name, _ = self.get_backtesting_prediction_file_name( + prediction_file_name, root_prediction = self.get_backtesting_prediction_file_name( pair, dk, trained_timestamp ) - path_to_predictionfile = Path(prediction_file_name) + path_to_predictionfile = Path(dk.full_path / root_prediction / prediction_file_name) file_exists = path_to_predictionfile.is_file() if file_exists and not scanning: @@ -711,8 +711,8 @@ class IFreqaiModel(ABC): """ coin, _ = pair.split("/") prediction_base_filename = f"{coin.lower()}_{trained_timestamp}" - root_prediction = f'{dk.full_path}/backtesting_predictions' - prediction_file_name = f"{root_prediction}/{prediction_base_filename}_predictions.h5" + root_prediction = 'backtesting_predictions' + prediction_file_name = f"{prediction_base_filename}_predictions.h5" return prediction_file_name, root_prediction # Following methods which are overridden by user made prediction models. diff --git a/tests/freqai/test_freqai_interface.py b/tests/freqai/test_freqai_interface.py index 792ffc467..09f5d27ff 100644 --- a/tests/freqai/test_freqai_interface.py +++ b/tests/freqai/test_freqai_interface.py @@ -192,7 +192,7 @@ def test_start_backtesting(mocker, freqai_conf): freqai.start_backtesting(df, metadata, freqai.dk) model_folders = [x for x in freqai.dd.full_path.iterdir() if x.is_dir()] - assert len(model_folders) == 5 + assert len(model_folders) == 6 shutil.rmtree(Path(freqai.dk.full_path)) @@ -217,7 +217,7 @@ def test_start_backtesting_subdaily_backtest_period(mocker, freqai_conf): metadata = {"pair": "LTC/BTC"} freqai.start_backtesting(df, metadata, freqai.dk) model_folders = [x for x in freqai.dd.full_path.iterdir() if x.is_dir()] - assert len(model_folders) == 8 + assert len(model_folders) == 9 shutil.rmtree(Path(freqai.dk.full_path)) @@ -242,7 +242,7 @@ def test_start_backtesting_from_existing_folder(mocker, freqai_conf, caplog): freqai.start_backtesting(df, metadata, freqai.dk) model_folders = [x for x in freqai.dd.full_path.iterdir() if x.is_dir()] - assert len(model_folders) == 5 + assert len(model_folders) == 6 # without deleting the exiting folder structure, re-run @@ -263,7 +263,7 @@ def test_start_backtesting_from_existing_folder(mocker, freqai_conf, caplog): freqai.start_backtesting(df, metadata, freqai.dk) assert log_has_re( - "Found model at ", + "Found backtesting prediction ", caplog, )