pr review - refactoring backtesting freqai
This commit is contained in:
parent
df51da22ee
commit
7bed0450d2
@ -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
|
||||
|
@ -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.
|
||||
|
@ -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,
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user