diff --git a/freqtrade/configuration/timerange.py b/freqtrade/configuration/timerange.py index 527402af7..5731631c5 100644 --- a/freqtrade/configuration/timerange.py +++ b/freqtrade/configuration/timerange.py @@ -1,11 +1,14 @@ """ This module contains the argument manager class """ +import logging import re from typing import Optional import arrow +logger = logging.getLogger(__name__) + class TimeRange: """ @@ -28,9 +31,33 @@ class TimeRange: and self.startts == other.startts and self.stopts == other.stopts) def subtract_start(self, seconds) -> None: + """ + Subtracts from startts if startts is set. + :param seconds: Seconds to subtract from starttime + :return: None (Modifies the object in place) + """ if self.startts: self.startts = self.startts - seconds + def adjust_start_if_necessary(self, ticker_interval_secs: int, startup_candles: int, + min_date: arrow.Arrow) -> None: + """ + Adjust startts by candles. + Applies only if no startup-candles have been available. + :param ticker_interval_secs: Ticker interval in seconds e.g. `timeframe_to_seconds('5m')` + :param startup_candles: Number of candles to move start-date forward + :param min_date: Minimum data date loaded. Key kriterium to decide if start-time + has to be moved + :return: None (Modifies the object in place) + """ + if (not self.starttype or (startup_candles + and min_date.timestamp == self.startts)): + # If no startts was defined, or test-data starts at the defined test-date + logger.warning("Moving start-date by %s candles to account for startup time.", + startup_candles) + self.startts = (min_date.timestamp + ticker_interval_secs * startup_candles) + self.starttype = 'date' + @staticmethod def parse_timerange(text: Optional[str]): """ diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 6b82dd601..1d6b328a8 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -446,15 +446,9 @@ class Backtesting: 'Loading backtest data from %s up to %s (%s days)..', min_date.isoformat(), max_date.isoformat(), (max_date - min_date).days ) - if (not timerange.starttype or (self.required_startup - and min_date.timestamp == timerange.startts)): - # If no startts was defined, or test-data starts at the defined test-date - logger.warning("Moving start-date by %s candles to account for startup time.", - self.required_startup) - timerange.startts = (min_date.timestamp - + timeframe_to_seconds(self.ticker_interval) - * self.required_startup) - timerange.starttype = 'date' + # Adjust startts forward if not enough data is available + timerange.adjust_start_if_necessary(timeframe_to_seconds(self.ticker_interval), + self.required_startup, min_date) for strat in self.strategylist: logger.info("Running backtesting for Strategy %s", strat.get_strategy_name())