stable/freqtrade/commands/data_commands.py

126 lines
4.9 KiB
Python
Raw Normal View History

2020-01-26 12:17:26 +00:00
import logging
import sys
2020-07-12 07:57:00 +00:00
from collections import defaultdict
2020-01-26 12:17:26 +00:00
from typing import Any, Dict, List
import arrow
from freqtrade.configuration import TimeRange, setup_utils_configuration
from freqtrade.data.converter import (convert_ohlcv_format,
convert_trades_format)
2020-01-26 12:17:26 +00:00
from freqtrade.data.history import (convert_trades_to_ohlcv,
refresh_backtest_ohlcv_data,
refresh_backtest_trades_data)
from freqtrade.exceptions import OperationalException
2020-07-12 07:57:00 +00:00
from freqtrade.exchange import timeframe_to_minutes
2020-01-26 12:17:26 +00:00
from freqtrade.resolvers import ExchangeResolver
from freqtrade.state import RunMode
logger = logging.getLogger(__name__)
def start_download_data(args: Dict[str, Any]) -> None:
"""
Download data (former download_backtest_data.py script)
"""
config = setup_utils_configuration(args, RunMode.UTIL_EXCHANGE)
if 'days' in config and 'timerange' in config:
raise OperationalException("--days and --timerange are mutually exclusive. "
"You can only specify one or the other.")
2020-01-26 12:17:26 +00:00
timerange = TimeRange()
if 'days' in config:
time_since = arrow.utcnow().shift(days=-config['days']).strftime("%Y%m%d")
timerange = TimeRange.parse_timerange(f'{time_since}-')
if 'timerange' in config:
timerange = timerange.parse_timerange(config['timerange'])
2020-01-26 12:17:26 +00:00
if 'pairs' not in config:
raise OperationalException(
"Downloading data requires a list of pairs. "
"Please check the documentation on how to configure this.")
2020-08-26 18:52:09 +00:00
logger.info(f"About to download pairs: {config['pairs']}, "
f"intervals: {config['timeframes']} to {config['datadir']}")
2020-01-26 12:17:26 +00:00
pairs_not_available: List[str] = []
# Init exchange
exchange = ExchangeResolver.load_exchange(config['exchange']['name'], config, validate=False)
# Manual validations of relevant settings
exchange.validate_pairs(config['pairs'])
for timeframe in config['timeframes']:
exchange.validate_timeframes(timeframe)
2020-01-26 12:17:26 +00:00
try:
if config.get('download_trades'):
pairs_not_available = refresh_backtest_trades_data(
2020-08-26 18:52:09 +00:00
exchange, pairs=config['pairs'], datadir=config['datadir'],
timerange=timerange, erase=bool(config.get('erase')),
data_format=config['dataformat_trades'])
2020-01-26 12:17:26 +00:00
# Convert downloaded trade data to different timeframes
convert_trades_to_ohlcv(
2020-08-26 18:52:09 +00:00
pairs=config['pairs'], timeframes=config['timeframes'],
datadir=config['datadir'], timerange=timerange, erase=bool(config.get('erase')),
data_format_ohlcv=config['dataformat_ohlcv'],
data_format_trades=config['dataformat_trades'],
)
2020-01-26 12:17:26 +00:00
else:
pairs_not_available = refresh_backtest_ohlcv_data(
2020-08-26 18:52:09 +00:00
exchange, pairs=config['pairs'], timeframes=config['timeframes'],
datadir=config['datadir'], timerange=timerange, erase=bool(config.get('erase')),
data_format=config['dataformat_ohlcv'])
2020-01-26 12:17:26 +00:00
except KeyboardInterrupt:
sys.exit("SIGINT received, aborting ...")
finally:
if pairs_not_available:
logger.info(f"Pairs [{','.join(pairs_not_available)}] not available "
f"on exchange {exchange.name}.")
def start_convert_data(args: Dict[str, Any], ohlcv: bool = True) -> None:
"""
Convert data from one format to another
"""
config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE)
if ohlcv:
convert_ohlcv_format(config,
convert_from=args['format_from'], convert_to=args['format_to'],
erase=args['erase'])
else:
convert_trades_format(config,
convert_from=args['format_from'], convert_to=args['format_to'],
erase=args['erase'])
2020-07-12 07:57:00 +00:00
def start_list_data(args: Dict[str, Any]) -> None:
"""
List available backtest data
"""
config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE)
2020-07-12 08:23:09 +00:00
from freqtrade.data.history.idatahandler import get_datahandler
2020-07-12 07:57:00 +00:00
from tabulate import tabulate
2020-07-12 08:23:09 +00:00
dhc = get_datahandler(config['datadir'], config['dataformat_ohlcv'])
2020-07-12 07:57:00 +00:00
paircombs = dhc.ohlcv_get_available_data(config['datadir'])
2020-07-14 04:55:34 +00:00
if args['pairs']:
paircombs = [comb for comb in paircombs if comb[0] in args['pairs']]
2020-07-12 07:57:00 +00:00
print(f"Found {len(paircombs)} pair / timeframe combinations.")
groupedpair = defaultdict(list)
for pair, timeframe in sorted(paircombs, key=lambda x: (x[0], timeframe_to_minutes(x[1]))):
groupedpair[pair].append(timeframe)
2020-07-14 14:42:47 +00:00
if groupedpair:
print(tabulate([(pair, ', '.join(timeframes)) for pair, timeframes in groupedpair.items()],
headers=("Pair", "Timeframe"),
tablefmt='psql', stralign='right'))