Merge pull request #1090 from freqtrade/ujson-loader

backtesting: try to load data with ujson if it exists
This commit is contained in:
Matthias 2018-07-29 08:54:02 +01:00 committed by GitHub
commit 618784d060
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,12 @@
# pragma pylint: disable=missing-docstring # pragma pylint: disable=missing-docstring
import gzip import gzip
import json try:
import ujson as json
except ImportError:
# see mypy/issues/1153
import json # type: ignore
import inspect
import logging import logging
import os import os
from typing import Optional, List, Dict, Tuple, Any from typing import Optional, List, Dict, Tuple, Any
@ -14,6 +19,14 @@ from freqtrade.arguments import TimeRange
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def json_load(data):
"""Try to load data with ujson"""
if inspect.getfullargspec(json.load)[5].get('precise_float'):
return json.load(data, precise_float=True)
else:
return json.load(data)
def trim_tickerlist(tickerlist: List[Dict], timerange: TimeRange) -> List[Dict]: def trim_tickerlist(tickerlist: List[Dict], timerange: TimeRange) -> List[Dict]:
if not tickerlist: if not tickerlist:
return tickerlist return tickerlist
@ -63,11 +76,11 @@ def load_tickerdata_file(
if os.path.isfile(gzipfile): if os.path.isfile(gzipfile):
logger.debug('Loading ticker data from file %s', gzipfile) logger.debug('Loading ticker data from file %s', gzipfile)
with gzip.open(gzipfile) as tickerdata: with gzip.open(gzipfile) as tickerdata:
pairdata = json.load(tickerdata) pairdata = json_load(tickerdata)
elif os.path.isfile(file): elif os.path.isfile(file):
logger.debug('Loading ticker data from file %s', file) logger.debug('Loading ticker data from file %s', file)
with open(file) as tickerdata: with open(file) as tickerdata:
pairdata = json.load(tickerdata) pairdata = json_load(tickerdata)
else: else:
return None return None
@ -163,7 +176,7 @@ def load_cached_data_for_updating(filename: str,
# read the cached file # read the cached file
if os.path.isfile(filename): if os.path.isfile(filename):
with open(filename, "rt") as file: with open(filename, "rt") as file:
data = json.load(file) data = json_load(file)
# remove the last item, because we are not sure if it is correct # remove the last item, because we are not sure if it is correct
# it could be fetched when the candle was incompleted # it could be fetched when the candle was incompleted
if data: if data: