2019-12-23 13:56:48 +00:00
|
|
|
import re
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Dict, List, Optional
|
|
|
|
|
|
|
|
from pandas import DataFrame
|
|
|
|
|
|
|
|
from freqtrade import misc
|
|
|
|
from freqtrade.configuration import TimeRange
|
2019-12-25 10:08:49 +00:00
|
|
|
from freqtrade.data.converter import parse_ticker_dataframe
|
2019-12-23 13:56:48 +00:00
|
|
|
|
|
|
|
from .idatahandler import IDataHandler
|
|
|
|
|
|
|
|
|
|
|
|
class JsonDataHandler(IDataHandler):
|
|
|
|
|
|
|
|
_use_zip = False
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def ohlcv_get_pairs(cls, datadir: Path, timeframe: str) -> List[str]:
|
|
|
|
"""
|
|
|
|
Returns a list of all pairs available in this datadir
|
|
|
|
"""
|
2019-12-25 09:21:30 +00:00
|
|
|
|
|
|
|
_tmp = [re.search(r'^(\S+)(?=\-' + timeframe + '.json)', p.name)
|
2019-12-23 13:56:48 +00:00
|
|
|
for p in datadir.glob(f"*{timeframe}.{cls._get_file_extension()}")]
|
2019-12-25 09:21:30 +00:00
|
|
|
# Check if regex found something and only return these results
|
2019-12-25 09:25:30 +00:00
|
|
|
return [match[0].replace('_', '/') for match in _tmp if match]
|
2019-12-23 13:56:48 +00:00
|
|
|
|
2019-12-25 10:08:49 +00:00
|
|
|
def ohlcv_store(self, pair: str, timeframe: str, data: DataFrame) -> None:
|
2019-12-23 13:56:48 +00:00
|
|
|
"""
|
|
|
|
Store data
|
|
|
|
"""
|
2019-12-25 10:08:49 +00:00
|
|
|
filename = JsonDataHandler._pair_data_filename(self._datadir, pair, timeframe)
|
2019-12-25 09:37:31 +00:00
|
|
|
misc.file_dump_json(filename, data, is_zip=self._use_zip)
|
2019-12-23 13:56:48 +00:00
|
|
|
|
2019-12-25 10:08:49 +00:00
|
|
|
def ohlcv_append(self, pair: str, timeframe: str, data: DataFrame) -> None:
|
2019-12-23 13:56:48 +00:00
|
|
|
"""
|
|
|
|
Append data to existing files
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2019-12-25 10:08:49 +00:00
|
|
|
def _ohlcv_load(self, pair: str, timeframe: str,
|
|
|
|
timerange: Optional[TimeRange] = None,
|
|
|
|
fill_up_missing: bool = True,
|
|
|
|
drop_incomplete: bool = True,
|
|
|
|
) -> DataFrame:
|
2019-12-23 13:56:48 +00:00
|
|
|
"""
|
2019-12-25 10:08:49 +00:00
|
|
|
Load data for one pair from disk.
|
|
|
|
Implements the loading and conversation to a Pandas dataframe.
|
2019-12-23 13:56:48 +00:00
|
|
|
:return: Dataframe
|
|
|
|
"""
|
2019-12-25 10:08:49 +00:00
|
|
|
filename = JsonDataHandler._pair_data_filename(self._datadir, pair, timeframe)
|
2019-12-23 13:56:48 +00:00
|
|
|
pairdata = misc.file_load_json(filename)
|
|
|
|
if not pairdata:
|
2019-12-25 10:08:49 +00:00
|
|
|
return DataFrame()
|
2019-12-23 13:56:48 +00:00
|
|
|
|
|
|
|
if timerange:
|
|
|
|
pairdata = IDataHandler.trim_tickerlist(pairdata, timerange)
|
2019-12-25 10:08:49 +00:00
|
|
|
return parse_ticker_dataframe(pairdata, timeframe,
|
|
|
|
pair=self._pair,
|
|
|
|
fill_missing=fill_up_missing,
|
|
|
|
drop_incomplete=drop_incomplete)
|
2019-12-23 13:56:48 +00:00
|
|
|
return pairdata
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def trades_get_pairs(cls, datadir: Path) -> List[str]:
|
|
|
|
"""
|
|
|
|
Returns a list of all pairs available in this datadir
|
|
|
|
"""
|
2019-12-25 09:21:30 +00:00
|
|
|
_tmp = [re.search(r'^(\S+)(?=\-trades.json)', p.name)
|
2019-12-23 13:56:48 +00:00
|
|
|
for p in datadir.glob(f"*trades.{cls._get_file_extension()}")]
|
2019-12-25 09:21:30 +00:00
|
|
|
# Check if regex found something and only return these results to avoid exceptions.
|
2019-12-25 09:25:30 +00:00
|
|
|
return [match[0].replace('_', '/') for match in _tmp if match]
|
2019-12-23 13:56:48 +00:00
|
|
|
|
2019-12-25 10:08:49 +00:00
|
|
|
def trades_store(self, pair: str, data: List[Dict]):
|
2019-12-23 13:56:48 +00:00
|
|
|
"""
|
|
|
|
Store data
|
|
|
|
"""
|
2019-12-25 10:08:49 +00:00
|
|
|
filename = self._pair_trades_filename(self._datadir, pair)
|
2019-12-23 13:56:48 +00:00
|
|
|
misc.file_dump_json(filename, data, is_zip=self._use_zip)
|
|
|
|
|
2019-12-25 10:08:49 +00:00
|
|
|
def trades_append(self, pair: str, data: DataFrame):
|
2019-12-23 13:56:48 +00:00
|
|
|
"""
|
|
|
|
Append data to existing files
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2019-12-25 10:08:49 +00:00
|
|
|
def trades_load(self, pair: str, timerange: Optional[TimeRange] = None) -> List[Dict]:
|
2019-12-23 13:56:48 +00:00
|
|
|
"""
|
|
|
|
Load a pair from file, either .json.gz or .json
|
|
|
|
# TODO: validate timerange ...
|
|
|
|
:return: List of trades
|
|
|
|
"""
|
2019-12-25 10:08:49 +00:00
|
|
|
filename = self._pair_trades_filename(self._datadir, pair)
|
2019-12-23 13:56:48 +00:00
|
|
|
tradesdata = misc.file_load_json(filename)
|
|
|
|
if not tradesdata:
|
|
|
|
return []
|
|
|
|
|
|
|
|
return tradesdata
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _pair_data_filename(cls, datadir: Path, pair: str, timeframe: str) -> Path:
|
|
|
|
pair_s = pair.replace("/", "_")
|
|
|
|
filename = datadir.joinpath(f'{pair_s}-{timeframe}.{cls._get_file_extension()}')
|
|
|
|
return filename
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _get_file_extension(cls):
|
|
|
|
return "json.gz" if cls._use_zip else "json"
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _pair_trades_filename(cls, datadir: Path, pair: str) -> Path:
|
|
|
|
pair_s = pair.replace("/", "_")
|
|
|
|
filename = datadir.joinpath(f'{pair_s}-trades.{cls._get_file_extension()}')
|
|
|
|
return filename
|
|
|
|
|
|
|
|
|
|
|
|
class JsonGzDataHandler(JsonDataHandler):
|
|
|
|
|
|
|
|
_use_zip = True
|