2019-12-23 13:56:48 +00:00
|
|
|
"""
|
|
|
|
Abstract datahandler interface.
|
|
|
|
It's subclasses handle and storing data from disk.
|
|
|
|
|
|
|
|
"""
|
2019-12-25 10:09:29 +00:00
|
|
|
import logging
|
2019-12-25 10:09:59 +00:00
|
|
|
from abc import ABC, abstractclassmethod, abstractmethod
|
|
|
|
from copy import deepcopy
|
2019-12-23 13:56:48 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Dict, List, Optional
|
2019-12-25 10:09:59 +00:00
|
|
|
|
|
|
|
import arrow
|
2019-12-23 13:56:48 +00:00
|
|
|
from pandas import DataFrame
|
|
|
|
|
|
|
|
from freqtrade.configuration import TimeRange
|
2019-12-25 10:09:29 +00:00
|
|
|
from freqtrade.exchange import timeframe_to_seconds
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2019-12-23 13:56:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class IDataHandler(ABC):
|
|
|
|
|
2019-12-25 10:09:29 +00:00
|
|
|
def __init__(self, datadir: Path) -> None:
|
2019-12-23 13:56:48 +00:00
|
|
|
self._datadir = datadir
|
|
|
|
|
2019-12-25 10:09:29 +00:00
|
|
|
# TODO: create abstract interface
|
|
|
|
|
|
|
|
def ohlcv_load(self, pair, timeframe: str,
|
|
|
|
timerange: Optional[TimeRange] = None,
|
2019-12-25 10:09:59 +00:00
|
|
|
fill_missing: bool = True,
|
2019-12-25 10:09:29 +00:00
|
|
|
drop_incomplete: bool = True,
|
|
|
|
startup_candles: int = 0,
|
|
|
|
) -> DataFrame:
|
|
|
|
"""
|
|
|
|
Load cached ticker history for the given pair.
|
|
|
|
|
|
|
|
:param pair: Pair to load data for
|
|
|
|
:param timeframe: Ticker timeframe (e.g. "5m")
|
|
|
|
:param timerange: Limit data to be loaded to this timerange
|
|
|
|
:param fill_up_missing: Fill missing values with "No action"-candles
|
|
|
|
:param drop_incomplete: Drop last candle assuming it may be incomplete.
|
|
|
|
:param startup_candles: Additional candles to load at the start of the period
|
|
|
|
:return: DataFrame with ohlcv data, or empty DataFrame
|
|
|
|
"""
|
|
|
|
# Fix startup period
|
|
|
|
timerange_startup = deepcopy(timerange)
|
|
|
|
if startup_candles > 0 and timerange_startup:
|
|
|
|
timerange_startup.subtract_start(timeframe_to_seconds(timeframe) * startup_candles)
|
|
|
|
|
|
|
|
pairdf = self._ohlcv_load(pair, timeframe,
|
|
|
|
timerange=timerange_startup,
|
2019-12-25 10:09:59 +00:00
|
|
|
fill_missing=fill_missing,
|
2019-12-25 10:09:29 +00:00
|
|
|
drop_incomplete=drop_incomplete)
|
2019-12-25 10:09:59 +00:00
|
|
|
if pairdf.empty:
|
2019-12-25 10:09:29 +00:00
|
|
|
logger.warning(
|
|
|
|
f'No history data for pair: "{pair}", timeframe: {timeframe}. '
|
|
|
|
'Use `freqtrade download-data` to download the data'
|
|
|
|
)
|
|
|
|
return pairdf
|
|
|
|
else:
|
|
|
|
if timerange_startup:
|
|
|
|
self._validate_pairdata(pair, pairdf, timerange_startup)
|
|
|
|
return pairdf
|
|
|
|
|
2019-12-25 10:09:59 +00:00
|
|
|
def _validate_pairdata(self, pair, pairdata: DataFrame, timerange: TimeRange):
|
2019-12-25 10:09:29 +00:00
|
|
|
"""
|
|
|
|
Validates pairdata for missing data at start end end and logs warnings.
|
|
|
|
:param pairdata: Dataframe to validate
|
|
|
|
:param timerange: Timerange specified for start and end dates
|
|
|
|
"""
|
|
|
|
|
|
|
|
if timerange.starttype == 'date' and pairdata[0][0] > timerange.startts * 1000:
|
|
|
|
logger.warning('Missing data at start for pair %s, data starts at %s',
|
|
|
|
pair, arrow.get(pairdata[0][0] // 1000).strftime('%Y-%m-%d %H:%M:%S'))
|
|
|
|
if timerange.stoptype == 'date' and pairdata[-1][0] < timerange.stopts * 1000:
|
|
|
|
logger.warning('Missing data at end for pair %s, data ends at %s',
|
|
|
|
pair, arrow.get(pairdata[-1][0] // 1000).strftime('%Y-%m-%d %H:%M:%S'))
|
2019-12-23 13:56:48 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def trim_tickerlist(tickerlist: List[Dict], timerange: TimeRange) -> List[Dict]:
|
|
|
|
"""
|
|
|
|
TODO: investigate if this is needed ... we can probably cover this in a dataframe
|
|
|
|
Trim tickerlist based on given timerange
|
|
|
|
"""
|
|
|
|
if not tickerlist:
|
|
|
|
return tickerlist
|
|
|
|
|
|
|
|
start_index = 0
|
|
|
|
stop_index = len(tickerlist)
|
|
|
|
|
|
|
|
if timerange.starttype == 'date':
|
|
|
|
while (start_index < len(tickerlist) and
|
|
|
|
tickerlist[start_index][0] < timerange.startts * 1000):
|
|
|
|
start_index += 1
|
|
|
|
|
|
|
|
if timerange.stoptype == 'date':
|
|
|
|
while (stop_index > 0 and
|
|
|
|
tickerlist[stop_index-1][0] > timerange.stopts * 1000):
|
|
|
|
stop_index -= 1
|
|
|
|
|
|
|
|
if start_index > stop_index:
|
|
|
|
raise ValueError(f'The timerange [{timerange.startts},{timerange.stopts}] is incorrect')
|
|
|
|
|
|
|
|
return tickerlist[start_index:stop_index]
|