Refactor start-adjust logic to timerange

This commit is contained in:
Matthias 2019-10-23 19:30:01 +02:00
parent 5c2682e2c9
commit bd4a23beeb
2 changed files with 30 additions and 9 deletions

View File

@ -1,11 +1,14 @@
""" """
This module contains the argument manager class This module contains the argument manager class
""" """
import logging
import re import re
from typing import Optional from typing import Optional
import arrow import arrow
logger = logging.getLogger(__name__)
class TimeRange: class TimeRange:
""" """
@ -28,9 +31,33 @@ class TimeRange:
and self.startts == other.startts and self.stopts == other.stopts) and self.startts == other.startts and self.stopts == other.stopts)
def subtract_start(self, seconds) -> None: def subtract_start(self, seconds) -> None:
"""
Subtracts <seconds> from startts if startts is set.
:param seconds: Seconds to subtract from starttime
:return: None (Modifies the object in place)
"""
if self.startts: if self.startts:
self.startts = self.startts - seconds 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 <startup_candles> 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 @staticmethod
def parse_timerange(text: Optional[str]): def parse_timerange(text: Optional[str]):
""" """

View File

@ -446,15 +446,9 @@ class Backtesting:
'Loading backtest data from %s up to %s (%s days)..', 'Loading backtest data from %s up to %s (%s days)..',
min_date.isoformat(), max_date.isoformat(), (max_date - min_date).days min_date.isoformat(), max_date.isoformat(), (max_date - min_date).days
) )
if (not timerange.starttype or (self.required_startup # Adjust startts forward if not enough data is available
and min_date.timestamp == timerange.startts)): timerange.adjust_start_if_necessary(timeframe_to_seconds(self.ticker_interval),
# If no startts was defined, or test-data starts at the defined test-date self.required_startup, min_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'
for strat in self.strategylist: for strat in self.strategylist:
logger.info("Running backtesting for Strategy %s", strat.get_strategy_name()) logger.info("Running backtesting for Strategy %s", strat.get_strategy_name())