Allow initializing handler-class just once

This commit is contained in:
Matthias 2019-12-25 15:55:28 +01:00
parent 416517b0c9
commit 5fca17d7e1

View File

@ -8,7 +8,7 @@ Includes:
import logging import logging
import operator import operator
from datetime import datetime, timezone from datetime import datetime
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple from typing import Any, Dict, List, Optional, Tuple
@ -18,6 +18,7 @@ from pandas import DataFrame
from freqtrade import OperationalException, misc from freqtrade import OperationalException, misc
from freqtrade.configuration import TimeRange from freqtrade.configuration import TimeRange
from freqtrade.data.converter import trades_to_ohlcv from freqtrade.data.converter import trades_to_ohlcv
from freqtrade.data.datahandlers.idatahandler import IDataHandler
from freqtrade.exchange import Exchange, timeframe_to_minutes from freqtrade.exchange import Exchange, timeframe_to_minutes
from .datahandlers import get_datahandlerclass from .datahandlers import get_datahandlerclass
@ -105,7 +106,8 @@ def load_pair_history(pair: str,
fill_up_missing: bool = True, fill_up_missing: bool = True,
drop_incomplete: bool = True, drop_incomplete: bool = True,
startup_candles: int = 0, startup_candles: int = 0,
data_format: str = 'json' data_format: str = 'json',
data_handler: IDataHandler,
) -> DataFrame: ) -> DataFrame:
""" """
Load cached ticker history for the given pair. Load cached ticker history for the given pair.
@ -117,18 +119,21 @@ def load_pair_history(pair: str,
:param fill_up_missing: Fill missing values with "No action"-candles :param fill_up_missing: Fill missing values with "No action"-candles
:param drop_incomplete: Drop last candle assuming it may be incomplete. :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 startup_candles: Additional candles to load at the start of the period
:param data_format: Format of the data :param data_format: Format of the data. Ignored if data_handler is set.
:param data_handler: Initialized data-handler to use.
Will be initialized from data_format if not set
:return: DataFrame with ohlcv data, or empty DataFrame :return: DataFrame with ohlcv data, or empty DataFrame
""" """
HandlerClass = get_datahandlerclass(data_format) if not data_handler:
loader = HandlerClass(datadir) HandlerClass = get_datahandlerclass(data_format)
return loader.ohlcv_load(pair=pair, data_handler = HandlerClass(datadir)
timeframe=timeframe, return data_handler.ohlcv_load(pair=pair,
timerange=timerange, timeframe=timeframe,
fill_missing=fill_up_missing, timerange=timerange,
drop_incomplete=drop_incomplete, fill_missing=fill_up_missing,
startup_candles=startup_candles, drop_incomplete=drop_incomplete,
) startup_candles=startup_candles,
)
def load_data(datadir: Path, def load_data(datadir: Path,
@ -137,7 +142,8 @@ def load_data(datadir: Path,
timerange: Optional[TimeRange] = None, timerange: Optional[TimeRange] = None,
fill_up_missing: bool = True, fill_up_missing: bool = True,
startup_candles: int = 0, startup_candles: int = 0,
fail_without_data: bool = False fail_without_data: bool = False,
data_format: str = 'json',
) -> Dict[str, DataFrame]: ) -> Dict[str, DataFrame]:
""" """
Load ticker history data for a list of pairs. Load ticker history data for a list of pairs.
@ -149,17 +155,23 @@ def load_data(datadir: Path,
:param fill_up_missing: Fill missing values with "No action"-candles :param fill_up_missing: Fill missing values with "No action"-candles
:param startup_candles: Additional candles to load at the start of the period :param startup_candles: Additional candles to load at the start of the period
:param fail_without_data: Raise OperationalException if no data is found. :param fail_without_data: Raise OperationalException if no data is found.
:param data_handler: Initialized data-handler to use.
:return: dict(<pair>:<Dataframe>) :return: dict(<pair>:<Dataframe>)
""" """
result: Dict[str, DataFrame] = {} result: Dict[str, DataFrame] = {}
if startup_candles > 0 and timerange: if startup_candles > 0 and timerange:
logger.info(f'Using indicator startup period: {startup_candles} ...') logger.info(f'Using indicator startup period: {startup_candles} ...')
HandlerClass = get_datahandlerclass(data_format)
data_handler = HandlerClass(datadir)
for pair in pairs: for pair in pairs:
hist = load_pair_history(pair=pair, timeframe=timeframe, hist = load_pair_history(pair=pair, timeframe=timeframe,
datadir=datadir, timerange=timerange, datadir=datadir, timerange=timerange,
fill_up_missing=fill_up_missing, fill_up_missing=fill_up_missing,
startup_candles=startup_candles) startup_candles=startup_candles,
data_handler=data_handler
)
if not hist.empty: if not hist.empty:
result[pair] = hist result[pair] = hist