From 00ff0c9b917a28f55d676976736cd4e0e25a7bc9 Mon Sep 17 00:00:00 2001 From: robcaulk Date: Fri, 6 May 2022 12:54:49 +0200 Subject: [PATCH] ensure user defined timerange truncates final backtest so that we arent mismatching data lengths upon return to strategy. Rename DataHandler class to FreqaiDataKitchen --- .../{data_handler.py => data_kitchen.py} | 24 +++++++++++++++---- freqtrade/freqai/freqai_interface.py | 4 ++-- 2 files changed, 22 insertions(+), 6 deletions(-) rename freqtrade/freqai/{data_handler.py => data_kitchen.py} (96%) diff --git a/freqtrade/freqai/data_handler.py b/freqtrade/freqai/data_kitchen.py similarity index 96% rename from freqtrade/freqai/data_handler.py rename to freqtrade/freqai/data_kitchen.py index e58575970..1bea97697 100644 --- a/freqtrade/freqai/data_handler.py +++ b/freqtrade/freqai/data_kitchen.py @@ -22,7 +22,7 @@ SECONDS_IN_DAY = 86400 logger = logging.getLogger(__name__) -class DataHandler: +class FreqaiDataKitchen: """ Class designed to handle all the data for the IFreqaiModel class model. Functionalities include holding, saving, loading, and analyzing the data. @@ -291,32 +291,48 @@ class DataHandler: bt_period = bt_split * SECONDS_IN_DAY full_timerange = TimeRange.parse_timerange(tr) + config_timerange = TimeRange.parse_timerange(self.config["timerange"]) timerange_train = copy.deepcopy(full_timerange) timerange_backtest = copy.deepcopy(full_timerange) tr_training_list = [] tr_backtesting_list = [] first = True + # within_config_timerange = True while True: if not first: timerange_train.startts = timerange_train.startts + bt_period timerange_train.stopts = timerange_train.startts + train_period - # if a full training period doesnt fit, we stop - if timerange_train.stopts > full_timerange.stopts: - break + # make sure we finish with a full backtest + # if timerange_train.stopts > config_timerange.stopts - bt_period: + # within_config_timerange = False + # timerange_train.stopts = config_timerange.stopts - bt_period + # # if a full training period doesnt fit, we stop + # if timerange_train.stopts > full_timerange.stopts: + # break first = False start = datetime.datetime.utcfromtimestamp(timerange_train.startts) stop = datetime.datetime.utcfromtimestamp(timerange_train.stopts) tr_training_list.append(start.strftime("%Y%m%d") + "-" + stop.strftime("%Y%m%d")) # associated backtest period + if timerange_backtest.stopts > config_timerange.stopts: + timerange_backtest.stopts = config_timerange.stopts + timerange_backtest.startts = timerange_train.stopts timerange_backtest.stopts = timerange_backtest.startts + bt_period + start = datetime.datetime.utcfromtimestamp(timerange_backtest.startts) stop = datetime.datetime.utcfromtimestamp(timerange_backtest.stopts) tr_backtesting_list.append(start.strftime("%Y%m%d") + "-" + stop.strftime("%Y%m%d")) + # ensure we are predicting on exactly same amount of data as requested by user defined + # --timerange + if timerange_backtest.stopts == config_timerange.stopts: + break + + print(tr_training_list, tr_backtesting_list) return tr_training_list, tr_backtesting_list def slice_dataframe(self, tr: str, df: DataFrame) -> DataFrame: diff --git a/freqtrade/freqai/freqai_interface.py b/freqtrade/freqai/freqai_interface.py index 62779a4e1..63e94383c 100644 --- a/freqtrade/freqai/freqai_interface.py +++ b/freqtrade/freqai/freqai_interface.py @@ -8,7 +8,7 @@ import numpy as np import pandas as pd from pandas import DataFrame -from freqtrade.freqai.data_handler import DataHandler +from freqtrade.freqai.data_kitchen import FreqaiDataKitchen pd.options.mode.chained_assignment = None @@ -55,7 +55,7 @@ class IFreqaiModel(ABC): :metadata: pair metadataa coming from strategy. """ self.pair = metadata["pair"] - self.dh = DataHandler(self.config, dataframe) + self.dh = FreqaiDataKitchen(self.config, dataframe) logger.info("going to train %s timeranges", len(self.dh.training_timeranges))