2019-06-12 09:33:20 +00:00
|
|
|
import logging
|
2019-07-21 12:13:38 +00:00
|
|
|
import sys
|
2019-08-16 12:42:44 +00:00
|
|
|
from pathlib import Path
|
2019-08-25 13:01:27 +00:00
|
|
|
from typing import Any, Dict, List
|
2019-06-12 09:33:20 +00:00
|
|
|
|
2019-08-16 12:42:44 +00:00
|
|
|
import arrow
|
2019-10-13 10:12:20 +00:00
|
|
|
from tabulate import tabulate
|
2019-08-16 12:42:44 +00:00
|
|
|
|
2019-09-21 10:53:15 +00:00
|
|
|
from freqtrade import OperationalException
|
2019-08-16 12:42:44 +00:00
|
|
|
from freqtrade.configuration import Configuration, TimeRange
|
2019-07-29 04:15:49 +00:00
|
|
|
from freqtrade.configuration.directory_operations import create_userdata_dir
|
2019-08-25 13:01:27 +00:00
|
|
|
from freqtrade.data.history import refresh_backtest_ohlcv_data
|
2019-09-30 21:33:33 +00:00
|
|
|
from freqtrade.exchange import available_exchanges, ccxt_exchanges
|
2019-10-13 10:12:20 +00:00
|
|
|
from freqtrade.misc import plural
|
2019-08-16 12:42:44 +00:00
|
|
|
from freqtrade.resolvers import ExchangeResolver
|
2019-06-12 09:33:20 +00:00
|
|
|
from freqtrade.state import RunMode
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-09-12 18:16:39 +00:00
|
|
|
def setup_utils_configuration(args: Dict[str, Any], method: RunMode) -> Dict[str, Any]:
|
2019-06-12 09:33:20 +00:00
|
|
|
"""
|
2019-06-16 18:37:43 +00:00
|
|
|
Prepare the configuration for utils subcommands
|
2019-06-12 09:33:20 +00:00
|
|
|
:param args: Cli args from Arguments()
|
|
|
|
:return: Configuration
|
|
|
|
"""
|
|
|
|
configuration = Configuration(args, method)
|
2019-08-16 12:42:44 +00:00
|
|
|
config = configuration.get_config()
|
2019-06-12 09:33:20 +00:00
|
|
|
|
2019-06-16 18:37:43 +00:00
|
|
|
config['exchange']['dry_run'] = True
|
2019-06-12 09:33:20 +00:00
|
|
|
# Ensure we do not use Exchange credentials
|
|
|
|
config['exchange']['key'] = ''
|
|
|
|
config['exchange']['secret'] = ''
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2019-09-12 18:16:39 +00:00
|
|
|
def start_list_exchanges(args: Dict[str, Any]) -> None:
|
2019-06-12 09:33:20 +00:00
|
|
|
"""
|
2019-06-14 18:54:38 +00:00
|
|
|
Print available exchanges
|
2019-06-12 09:33:20 +00:00
|
|
|
:param args: Cli args from Arguments()
|
|
|
|
:return: None
|
|
|
|
"""
|
2019-09-30 21:33:33 +00:00
|
|
|
exchanges = ccxt_exchanges() if args['list_exchanges_all'] else available_exchanges()
|
2019-09-12 18:16:39 +00:00
|
|
|
if args['print_one_column']:
|
2019-09-30 21:33:33 +00:00
|
|
|
print('\n'.join(exchanges))
|
2019-06-12 09:33:20 +00:00
|
|
|
else:
|
2019-09-30 21:33:33 +00:00
|
|
|
if args['list_exchanges_all']:
|
|
|
|
print(f"All exchanges supported by the ccxt library: {', '.join(exchanges)}")
|
|
|
|
else:
|
|
|
|
print(f"Exchanges available for Freqtrade: {', '.join(exchanges)}")
|
2019-07-21 12:13:38 +00:00
|
|
|
|
|
|
|
|
2019-09-12 18:16:39 +00:00
|
|
|
def start_create_userdir(args: Dict[str, Any]) -> None:
|
2019-07-21 12:13:38 +00:00
|
|
|
"""
|
|
|
|
Create "user_data" directory to contain user data strategies, hyperopts, ...)
|
|
|
|
:param args: Cli args from Arguments()
|
|
|
|
:return: None
|
|
|
|
"""
|
2019-09-12 18:16:39 +00:00
|
|
|
if "user_data_dir" in args and args["user_data_dir"]:
|
|
|
|
create_userdata_dir(args["user_data_dir"], create_dir=True)
|
2019-07-21 12:13:38 +00:00
|
|
|
else:
|
|
|
|
logger.warning("`create-userdir` requires --userdir to be set.")
|
|
|
|
sys.exit(1)
|
2019-08-21 17:35:27 +00:00
|
|
|
|
|
|
|
|
2019-09-12 18:16:39 +00:00
|
|
|
def start_download_data(args: Dict[str, Any]) -> None:
|
2019-08-16 12:42:44 +00:00
|
|
|
"""
|
2019-08-16 13:27:33 +00:00
|
|
|
Download data (former download_backtest_data.py script)
|
2019-08-16 12:42:44 +00:00
|
|
|
"""
|
|
|
|
config = setup_utils_configuration(args, RunMode.OTHER)
|
|
|
|
|
|
|
|
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}-')
|
|
|
|
|
2019-09-21 10:53:15 +00:00
|
|
|
if 'pairs' not in config:
|
|
|
|
raise OperationalException(
|
2019-09-24 04:35:41 +00:00
|
|
|
"Downloading data requires a list of pairs. "
|
2019-09-21 10:53:15 +00:00
|
|
|
"Please check the documentation on how to configure this.")
|
|
|
|
|
2019-08-16 12:42:44 +00:00
|
|
|
dl_path = Path(config['datadir'])
|
|
|
|
logger.info(f'About to download pairs: {config["pairs"]}, '
|
|
|
|
f'intervals: {config["timeframes"]} to {dl_path}')
|
|
|
|
|
2019-08-25 13:01:27 +00:00
|
|
|
pairs_not_available: List[str] = []
|
2019-08-16 12:42:44 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# Init exchange
|
|
|
|
exchange = ExchangeResolver(config['exchange']['name'], config).exchange
|
|
|
|
|
2019-08-25 13:01:27 +00:00
|
|
|
pairs_not_available = refresh_backtest_ohlcv_data(
|
|
|
|
exchange, pairs=config["pairs"], timeframes=config["timeframes"],
|
|
|
|
dl_path=Path(config['datadir']), timerange=timerange, erase=config.get("erase"))
|
2019-08-16 12:42:44 +00:00
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit("SIGINT received, aborting ...")
|
|
|
|
|
|
|
|
finally:
|
|
|
|
if pairs_not_available:
|
2019-08-25 13:01:27 +00:00
|
|
|
logger.info(f"Pairs [{','.join(pairs_not_available)}] not available "
|
|
|
|
f"on exchange {config['exchange']['name']}.")
|
2019-09-29 08:54:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def start_list_timeframes(args: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
Print ticker intervals (timeframes) available on Exchange
|
|
|
|
"""
|
|
|
|
config = setup_utils_configuration(args, RunMode.OTHER)
|
2019-09-29 20:08:11 +00:00
|
|
|
# Do not use ticker_interval set in the config
|
|
|
|
config['ticker_interval'] = None
|
2019-09-29 08:54:20 +00:00
|
|
|
|
|
|
|
# Init exchange
|
|
|
|
exchange = ExchangeResolver(config['exchange']['name'], config).exchange
|
|
|
|
|
2019-09-29 20:08:11 +00:00
|
|
|
if args['print_one_column']:
|
|
|
|
print('\n'.join(exchange.timeframes))
|
2019-09-29 08:54:20 +00:00
|
|
|
else:
|
2019-09-29 20:08:11 +00:00
|
|
|
print(f"Timeframes available for the exchange `{config['exchange']['name']}`: "
|
|
|
|
f"{', '.join(exchange.timeframes)}")
|
2019-10-13 10:12:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def start_list_pairs(args: Dict[str, Any], pairs_only: bool = False) -> None:
|
|
|
|
"""
|
|
|
|
Print pairs on the exchange
|
|
|
|
:param args: Cli args from Arguments()
|
|
|
|
:param pairs_only: if True print only pairs, otherwise print all instruments (markets)
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
config = setup_utils_configuration(args, RunMode.OTHER)
|
|
|
|
|
|
|
|
# Init exchange
|
|
|
|
exchange = ExchangeResolver(config['exchange']['name'], config).exchange
|
|
|
|
|
|
|
|
active_only = args.get('active_only', False)
|
|
|
|
base_currency = args.get('base_currency', '')
|
|
|
|
quote_currency = args.get('quote_currency', '')
|
|
|
|
|
|
|
|
pairs = exchange.get_markets(base_currency=base_currency,
|
|
|
|
quote_currency=quote_currency,
|
|
|
|
pairs_only=pairs_only,
|
|
|
|
active_only=active_only)
|
|
|
|
|
|
|
|
if args.get('print_list', False):
|
|
|
|
# print data as a list
|
|
|
|
print(f"Exchange {exchange.name} has {len(pairs)} " +
|
|
|
|
(plural(len(pairs), "pair" if pairs_only else "market")) +
|
|
|
|
(f" with {base_currency} as base currency" if base_currency else "") +
|
|
|
|
(" and" if base_currency and quote_currency else "") +
|
|
|
|
(f" with {quote_currency} as quote currency" if quote_currency else "") +
|
|
|
|
(f": {sorted(pairs.keys())}" if len(pairs) else "") + ".")
|
|
|
|
else:
|
|
|
|
# print data as a table
|
|
|
|
tabular_data = []
|
|
|
|
for _, v in pairs.items():
|
|
|
|
tabular_data.append([v['id'], v['symbol'], v['base'], v['quote'],
|
|
|
|
"Yes" if v['active'] else "No"])
|
|
|
|
|
|
|
|
headers = ['Id', 'Symbol', 'Base', 'Quote', 'Active']
|
|
|
|
print(tabulate(tabular_data, headers=headers, tablefmt='pipe'))
|