diff --git a/freqtrade/arguments.py b/freqtrade/arguments.py index afcb05511..00869f974 100644 --- a/freqtrade/arguments.py +++ b/freqtrade/arguments.py @@ -222,6 +222,9 @@ class Arguments(object): syntax = [(r'^-(\d{8})$', (None, 'date')), (r'^(\d{8})-$', ('date', None)), (r'^(\d{8})-(\d{8})$', ('date', 'date')), + (r'^-(\d{10})$', (None, 'timestamp')), + (r'^(\d{10})-$', ('timestamp', None)), + (r'^(\d{10})-(\d{10})$', ('timestamp', 'timestamp')), (r'^(-\d+)$', (None, 'line')), (r'^(\d+)-$', ('line', None)), (r'^(\d+)-(\d+)$', ('index', 'index'))] @@ -237,6 +240,8 @@ class Arguments(object): start = rvals[index] if stype[0] == 'date': start = arrow.get(start, 'YYYYMMDD').timestamp + elif stype[0] == 'timestamp': + start = arrow.get(start).timestamp else: start = int(start) index += 1 @@ -244,6 +249,8 @@ class Arguments(object): stop = rvals[index] if stype[1] == 'date': stop = arrow.get(stop, 'YYYYMMDD').timestamp + elif stype[1] == 'timestamp': + stop = arrow.get(stop).timestamp else: stop = int(stop) return stype, start, stop diff --git a/freqtrade/optimize/__init__.py b/freqtrade/optimize/__init__.py index 68ba5622e..c16cd3c3f 100644 --- a/freqtrade/optimize/__init__.py +++ b/freqtrade/optimize/__init__.py @@ -15,6 +15,63 @@ from user_data.hyperopt_conf import hyperopt_optimize_conf logger = logging.getLogger(__name__) +def sanitize_start_stop_date(tickerlist: List[Dict], timerange, pair): + """ + This function is called when either timerange start or stop are type date + - + Prevent index out or range error in trim_tickerlist, which occurs when trying + to process a stop or start range outside that in the cached data + returns (start, stop, nothing_to_trim) + nothing_to_trim > 0 is a flag to later not send the pair data to trim_tickerlist + - + Function Logic: + - if cache records begin after timerange start - reset start to first record + - if cache records end before timerange stop - reset stop to last record + - if start or stop are fully after or before cache records - return, nothing_to_trim. + - if only 1 of stop or start are set in timerange use the first or last + record appropriately from the cache as the unset value + """ + + # Do nothing if timerange does not contain a type date. + stype, start, stop = timerange + nothing_to_trim = 0 + if stype[0] != 'date' or stype[1] != 'date': + sanitized_dates = [start, stop, nothing_to_trim] + return sanitized_dates + + # If no arg for stop or start then set to the first or last record in the cache + stop = stop if stype[1] == 'date' else int(((tickerlist[(len(tickerlist) - 1)][0]) / 1000)) + start = start if stype[0] == 'date' else int(((tickerlist[0][0]) / 1000)) + + # If requested range start is after cache records end - no data to be trimmed + # If requested start range is before start of cache, move it to first nearest record + if stype[0] == 'date': + if (tickerlist[0][0]) > (stop * 1000): + logger.warn('No data for %s timerange in cache, update cache ', pair) + nothing_to_trim = nothing_to_trim + 1 + elif (tickerlist[0][0]) > (start * 1000): + start = (tickerlist[0][0] / 1000) + logger.warn('Requested start timerange for %s not in cache, update cache ', pair) + + # If requested range stop is before cache records begin - no data to be trimmed + # If requested stop range is after end of cache, move it to last nearest record + if stype[1] == 'date': + if (tickerlist[(len(tickerlist) - 1)][0]) < (start * 1000): + logger.warn('No data for %s timerange in cache, update cache ', pair) + nothing_to_trim = nothing_to_trim + 2 + elif (tickerlist[(len(tickerlist) - 1)][0]) < (stop * 1000): + stop = (tickerlist[(len(tickerlist) - 1)][0] / 1000) + logger.warn('Requested stop timerange for %s not in cache, update cache ', pair) + + # Impossible range, nothing to trim. + if start > stop: + nothing_to_trim = nothing_to_trim + 4 + logger.warn('Check timerange for %s', pair) + + sanitized_dates = [start, stop, nothing_to_trim] + return sanitized_dates + + def trim_tickerlist(tickerlist: List[Dict], timerange: Tuple[Tuple, int, int]) -> List[Dict]: if not tickerlist: return tickerlist @@ -23,7 +80,6 @@ def trim_tickerlist(tickerlist: List[Dict], timerange: Tuple[Tuple, int, int]) - start_index = 0 stop_index = len(tickerlist) - if stype[0] == 'line': stop_index = start if stype[0] == 'index': @@ -75,6 +131,20 @@ def load_tickerdata_file( else: return None + """ + Call to function to catch if a start or stop date from timerange is outside + range of records in the cached ticker list. + This prevents "index out of range" error. + """ + if timerange: + stype, start, stop = timerange + if stype[0] == 'date' or stype[1] == 'date': + sanitized_dates = sanitize_start_stop_date(pairdata, timerange, pair) + timerange = (('date', 'date'), int(sanitized_dates[0]), int(sanitized_dates[1])) + # If no overlap of timerange to cache data return pairdata, do not call trim_tickerlist + if sanitized_dates[2] > 0: + return pairdata + if timerange: pairdata = trim_tickerlist(pairdata, timerange) return pairdata