Skip to content

Commit

Permalink
Move dataframe validation to abstract class
Browse files Browse the repository at this point in the history
  • Loading branch information
xmatthias committed Dec 28, 2019
1 parent e861f05 commit 37c5b68
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
20 changes: 16 additions & 4 deletions freqtrade/data/datahandlers/idatahandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import Dict, List, Optional
from datetime import datetime, timezone
from pandas import DataFrame
from freqtrade.data.converter import clean_ohlcv_dataframe, trim_dataframe

from freqtrade.configuration import TimeRange
from freqtrade.exchange import timeframe_to_seconds
Expand Down Expand Up @@ -47,15 +48,26 @@ def ohlcv_load(self, pair, timeframe: str,
timerange_startup.subtract_start(timeframe_to_seconds(timeframe) * startup_candles)

pairdf = self._ohlcv_load(pair, timeframe,
timerange=timerange_startup,
fill_missing=fill_missing,
drop_incomplete=drop_incomplete)
timerange=timerange_startup)
if pairdf.empty:
logger.warning(
f'No history data for pair: "{pair}", timeframe: {timeframe}. '
'Use `freqtrade download-data` to download the data'
)
return pairdf
return pairdf
else:
enddate = pairdf.iloc[-1]['date']

if timerange_startup:
self._validate_pairdata(pair, pairdf, timerange_startup)
pairdf = trim_dataframe(pairdf, timerange_startup)

# incomplete candles should only be dropped if we didn't trim the end beforehand.
return clean_ohlcv_dataframe(pairdf, timeframe,
pair=pair,
fill_missing=fill_missing,
drop_incomplete=(drop_incomplete and
enddate == pairdf.iloc[-1]['date']))

def _validate_pairdata(self, pair, pairdata: DataFrame, timerange: TimeRange):
"""
Expand Down
23 changes: 3 additions & 20 deletions freqtrade/data/datahandlers/jsondatahandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from freqtrade import misc
from freqtrade.configuration import TimeRange
from freqtrade.data.converter import clean_ohlcv_dataframe, trim_dataframe

from .idatahandler import IDataHandler

Expand Down Expand Up @@ -54,18 +53,14 @@ def ohlcv_store(self, pair: str, timeframe: str, data: DataFrame) -> None:

def _ohlcv_load(self, pair: str, timeframe: str,
timerange: Optional[TimeRange] = None,
fill_missing: bool = True,
drop_incomplete: bool = True,
) -> DataFrame:
"""
Internal method used to load data for one pair from disk.
Implements the loading and conversation to a Pandas dataframe.
Timerange trimming and dataframe validation happens outside of this method.
:param pair: Pair to load data
:param timeframe: Ticker timeframe (e.g. "5m")
:param timerange: Limit data to be loaded to this timerange
:param fill_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
:param timerange: Limit data to be loaded to this timerange.
:return: DataFrame with ohlcv data, or empty DataFrame
"""
filename = self._pair_data_filename(self._datadir, pair, timeframe)
Expand All @@ -77,19 +72,7 @@ def _ohlcv_load(self, pair: str, timeframe: str,
unit='ms',
utc=True,
infer_datetime_format=True)

enddate = pairdata.iloc[-1]['date']

if timerange:
self._validate_pairdata(pair, pairdata, timerange)
pairdata = trim_dataframe(pairdata, timerange)

# incomplete candles should only be dropped if we didn't trim the end beforehand.
return clean_ohlcv_dataframe(pairdata, timeframe,
pair=pair,
fill_missing=fill_missing,
drop_incomplete=(drop_incomplete and
enddate == pairdata.iloc[-1]['date']))
return pairdata

def ohlcv_append(self, pair: str, timeframe: str, data: DataFrame) -> None:
"""
Expand Down

0 comments on commit 37c5b68

Please sign in to comment.