2020-01-26 12:08:58 +00:00
|
|
|
import csv
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
from pathlib import Path
|
2020-02-14 18:24:30 +00:00
|
|
|
from typing import Any, Dict, List
|
2020-01-26 12:08:58 +00:00
|
|
|
|
|
|
|
import rapidjson
|
2020-09-28 17:39:41 +00:00
|
|
|
from colorama import Fore, Style
|
|
|
|
from colorama import init as colorama_init
|
2020-01-26 12:08:58 +00:00
|
|
|
from tabulate import tabulate
|
|
|
|
|
2020-01-26 12:55:48 +00:00
|
|
|
from freqtrade.configuration import setup_utils_configuration
|
2021-09-11 15:56:41 +00:00
|
|
|
from freqtrade.constants import USERPATH_STRATEGIES
|
2021-06-08 19:20:35 +00:00
|
|
|
from freqtrade.enums import RunMode
|
2020-01-26 12:08:58 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2021-04-06 05:47:44 +00:00
|
|
|
from freqtrade.exchange import market_is_active, validate_exchanges
|
2021-07-12 12:08:01 +00:00
|
|
|
from freqtrade.misc import parse_db_uri_for_logging, plural
|
2020-01-26 12:08:58 +00:00
|
|
|
from freqtrade.resolvers import ExchangeResolver, StrategyResolver
|
|
|
|
|
2020-09-28 17:39:41 +00:00
|
|
|
|
2020-01-26 12:08:58 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def start_list_exchanges(args: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
Print available exchanges
|
|
|
|
:param args: Cli args from Arguments()
|
|
|
|
:return: None
|
|
|
|
"""
|
2021-04-06 05:47:44 +00:00
|
|
|
exchanges = validate_exchanges(args['list_exchanges_all'])
|
|
|
|
|
2020-01-26 12:08:58 +00:00
|
|
|
if args['print_one_column']:
|
2021-04-06 05:47:44 +00:00
|
|
|
print('\n'.join([e[0] for e in exchanges]))
|
2020-01-26 12:08:58 +00:00
|
|
|
else:
|
|
|
|
if args['list_exchanges_all']:
|
2021-04-06 05:47:44 +00:00
|
|
|
print("All exchanges supported by the ccxt library:")
|
2020-01-26 12:08:58 +00:00
|
|
|
else:
|
2021-04-06 05:47:44 +00:00
|
|
|
print("Exchanges available for Freqtrade:")
|
|
|
|
exchanges = [e for e in exchanges if e[1] is not False]
|
|
|
|
|
|
|
|
print(tabulate(exchanges, headers=['Exchange name', 'Valid', 'reason']))
|
2020-01-26 12:08:58 +00:00
|
|
|
|
|
|
|
|
2022-04-23 07:11:50 +00:00
|
|
|
def _print_objs_tabular(objs: List, print_colorized: bool, base_dir: Path) -> None:
|
2020-02-14 19:28:49 +00:00
|
|
|
if print_colorized:
|
|
|
|
colorama_init(autoreset=True)
|
2020-02-15 17:43:11 +00:00
|
|
|
red = Fore.RED
|
|
|
|
yellow = Fore.YELLOW
|
|
|
|
reset = Style.RESET_ALL
|
|
|
|
else:
|
|
|
|
red = ''
|
|
|
|
yellow = ''
|
|
|
|
reset = ''
|
2020-02-14 19:28:49 +00:00
|
|
|
|
2020-02-14 18:24:30 +00:00
|
|
|
names = [s['name'] for s in objs]
|
2021-05-29 11:02:18 +00:00
|
|
|
objs_to_print = [{
|
2020-02-14 18:24:30 +00:00
|
|
|
'name': s['name'] if s['name'] else "--",
|
2022-04-23 07:11:50 +00:00
|
|
|
'location': s['location'].relative_to(base_dir),
|
2020-02-15 17:43:11 +00:00
|
|
|
'status': (red + "LOAD FAILED" + reset if s['class'] is None
|
2020-02-14 19:32:46 +00:00
|
|
|
else "OK" if names.count(s['name']) == 1
|
2020-02-15 17:43:11 +00:00
|
|
|
else yellow + "DUPLICATE NAME" + reset)
|
2020-02-14 18:24:30 +00:00
|
|
|
} for s in objs]
|
2021-05-29 11:02:18 +00:00
|
|
|
for idx, s in enumerate(objs):
|
|
|
|
if 'hyperoptable' in s:
|
|
|
|
objs_to_print[idx].update({
|
|
|
|
'hyperoptable': "Yes" if s['hyperoptable']['count'] > 0 else "No",
|
|
|
|
'buy-Params': len(s['hyperoptable'].get('buy', [])),
|
|
|
|
'sell-Params': len(s['hyperoptable'].get('sell', [])),
|
|
|
|
})
|
|
|
|
print(tabulate(objs_to_print, headers='keys', tablefmt='psql', stralign='right'))
|
2020-02-14 18:24:30 +00:00
|
|
|
|
|
|
|
|
2020-01-26 12:08:58 +00:00
|
|
|
def start_list_strategies(args: Dict[str, Any]) -> None:
|
|
|
|
"""
|
2020-02-02 15:48:29 +00:00
|
|
|
Print files with Strategy custom classes available in the directory
|
2020-01-26 12:08:58 +00:00
|
|
|
"""
|
|
|
|
config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE)
|
|
|
|
|
2020-02-02 15:12:23 +00:00
|
|
|
directory = Path(config.get('strategy_path', config['user_data_dir'] / USERPATH_STRATEGIES))
|
2022-04-23 07:11:50 +00:00
|
|
|
strategy_objs = StrategyResolver.search_all_objects(
|
|
|
|
directory, not args['print_one_column'], config.get('recursive_strategy_search', False))
|
2020-01-26 12:08:58 +00:00
|
|
|
# Sort alphabetically
|
2020-02-14 18:46:22 +00:00
|
|
|
strategy_objs = sorted(strategy_objs, key=lambda x: x['name'])
|
2021-05-29 11:02:18 +00:00
|
|
|
for obj in strategy_objs:
|
|
|
|
if obj['class']:
|
|
|
|
obj['hyperoptable'] = obj['class'].detect_all_parameters()
|
|
|
|
else:
|
|
|
|
obj['hyperoptable'] = {'count': 0}
|
2020-01-26 12:08:58 +00:00
|
|
|
|
|
|
|
if args['print_one_column']:
|
2020-02-14 18:46:22 +00:00
|
|
|
print('\n'.join([s['name'] for s in strategy_objs]))
|
2020-01-26 12:08:58 +00:00
|
|
|
else:
|
2022-04-23 07:11:50 +00:00
|
|
|
_print_objs_tabular(strategy_objs, config.get('print_colorized', False), directory)
|
2020-01-26 12:08:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def start_list_timeframes(args: Dict[str, Any]) -> None:
|
|
|
|
"""
|
2021-04-03 14:54:47 +00:00
|
|
|
Print timeframes available on Exchange
|
2020-01-26 12:08:58 +00:00
|
|
|
"""
|
|
|
|
config = setup_utils_configuration(args, RunMode.UTIL_EXCHANGE)
|
2020-06-01 18:33:26 +00:00
|
|
|
# Do not use timeframe set in the config
|
|
|
|
config['timeframe'] = None
|
2020-01-26 12:08:58 +00:00
|
|
|
|
|
|
|
# Init exchange
|
|
|
|
exchange = ExchangeResolver.load_exchange(config['exchange']['name'], config, validate=False)
|
|
|
|
|
|
|
|
if args['print_one_column']:
|
|
|
|
print('\n'.join(exchange.timeframes))
|
|
|
|
else:
|
|
|
|
print(f"Timeframes available for the exchange `{exchange.name}`: "
|
|
|
|
f"{', '.join(exchange.timeframes)}")
|
|
|
|
|
|
|
|
|
|
|
|
def start_list_markets(args: Dict[str, Any], pairs_only: bool = False) -> None:
|
|
|
|
"""
|
|
|
|
Print pairs/markets 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.UTIL_EXCHANGE)
|
|
|
|
|
|
|
|
# Init exchange
|
|
|
|
exchange = ExchangeResolver.load_exchange(config['exchange']['name'], config, validate=False)
|
|
|
|
|
|
|
|
# By default only active pairs/markets are to be shown
|
|
|
|
active_only = not args.get('list_pairs_all', False)
|
|
|
|
|
|
|
|
base_currencies = args.get('base_currencies', [])
|
|
|
|
quote_currencies = args.get('quote_currencies', [])
|
|
|
|
|
|
|
|
try:
|
|
|
|
pairs = exchange.get_markets(base_currencies=base_currencies,
|
|
|
|
quote_currencies=quote_currencies,
|
2021-11-01 07:40:55 +00:00
|
|
|
tradable_only=pairs_only,
|
2020-01-26 12:08:58 +00:00
|
|
|
active_only=active_only)
|
|
|
|
# Sort the pairs/markets by symbol
|
2021-06-13 09:45:23 +00:00
|
|
|
pairs = dict(sorted(pairs.items()))
|
2020-01-26 12:08:58 +00:00
|
|
|
except Exception as e:
|
|
|
|
raise OperationalException(f"Cannot get markets. Reason: {e}") from e
|
|
|
|
|
|
|
|
else:
|
|
|
|
summary_str = ((f"Exchange {exchange.name} has {len(pairs)} ") +
|
|
|
|
("active " if active_only else "") +
|
|
|
|
(plural(len(pairs), "pair" if pairs_only else "market")) +
|
|
|
|
(f" with {', '.join(base_currencies)} as base "
|
|
|
|
f"{plural(len(base_currencies), 'currency', 'currencies')}"
|
|
|
|
if base_currencies else "") +
|
|
|
|
(" and" if base_currencies and quote_currencies else "") +
|
|
|
|
(f" with {', '.join(quote_currencies)} as quote "
|
|
|
|
f"{plural(len(quote_currencies), 'currency', 'currencies')}"
|
|
|
|
if quote_currencies else ""))
|
|
|
|
|
|
|
|
headers = ["Id", "Symbol", "Base", "Quote", "Active",
|
2021-11-01 07:40:55 +00:00
|
|
|
"Spot", "Margin", "Future", "Leverage"]
|
|
|
|
|
|
|
|
tabular_data = [{
|
|
|
|
'Id': v['id'],
|
|
|
|
'Symbol': v['symbol'],
|
|
|
|
'Base': v['base'],
|
|
|
|
'Quote': v['quote'],
|
|
|
|
'Active': market_is_active(v),
|
|
|
|
'Spot': 'Spot' if exchange.market_is_spot(v) else '',
|
|
|
|
'Margin': 'Margin' if exchange.market_is_margin(v) else '',
|
|
|
|
'Future': 'Future' if exchange.market_is_future(v) else '',
|
|
|
|
'Leverage': exchange.get_max_leverage(v['symbol'], 20)
|
|
|
|
} for _, v in pairs.items()]
|
2020-01-26 12:08:58 +00:00
|
|
|
|
|
|
|
if (args.get('print_one_column', False) or
|
|
|
|
args.get('list_pairs_print_json', False) or
|
|
|
|
args.get('print_csv', False)):
|
|
|
|
# Print summary string in the log in case of machine-readable
|
|
|
|
# regular formats.
|
|
|
|
logger.info(f"{summary_str}.")
|
|
|
|
else:
|
|
|
|
# Print empty string separating leading logs and output in case of
|
|
|
|
# human-readable formats.
|
|
|
|
print()
|
|
|
|
|
2021-03-21 11:44:32 +00:00
|
|
|
if pairs:
|
2020-01-26 12:08:58 +00:00
|
|
|
if args.get('print_list', False):
|
|
|
|
# print data as a list, with human-readable summary
|
|
|
|
print(f"{summary_str}: {', '.join(pairs.keys())}.")
|
|
|
|
elif args.get('print_one_column', False):
|
|
|
|
print('\n'.join(pairs.keys()))
|
|
|
|
elif args.get('list_pairs_print_json', False):
|
|
|
|
print(rapidjson.dumps(list(pairs.keys()), default=str))
|
|
|
|
elif args.get('print_csv', False):
|
|
|
|
writer = csv.DictWriter(sys.stdout, fieldnames=headers)
|
|
|
|
writer.writeheader()
|
|
|
|
writer.writerows(tabular_data)
|
|
|
|
else:
|
|
|
|
# print data as a table, with the human-readable summary
|
|
|
|
print(f"{summary_str}:")
|
2020-02-27 15:10:45 +00:00
|
|
|
print(tabulate(tabular_data, headers='keys', tablefmt='psql', stralign='right'))
|
2020-01-26 12:08:58 +00:00
|
|
|
elif not (args.get('print_one_column', False) or
|
|
|
|
args.get('list_pairs_print_json', False) or
|
|
|
|
args.get('print_csv', False)):
|
|
|
|
print(f"{summary_str}.")
|
2020-05-02 09:26:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def start_show_trades(args: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
Show trades
|
|
|
|
"""
|
|
|
|
import json
|
2020-09-28 17:39:41 +00:00
|
|
|
|
2020-10-16 05:39:12 +00:00
|
|
|
from freqtrade.persistence import Trade, init_db
|
2020-05-02 09:26:12 +00:00
|
|
|
config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE)
|
2020-05-05 17:48:28 +00:00
|
|
|
|
|
|
|
if 'db_url' not in config:
|
|
|
|
raise OperationalException("--db-url is required for this command.")
|
|
|
|
|
2021-07-12 12:08:01 +00:00
|
|
|
logger.info(f'Using DB: "{parse_db_uri_for_logging(config["db_url"])}"')
|
2022-05-19 04:45:20 +00:00
|
|
|
init_db(config['db_url'])
|
2020-05-02 09:26:12 +00:00
|
|
|
tfilter = []
|
|
|
|
|
|
|
|
if config.get('trade_ids'):
|
|
|
|
tfilter.append(Trade.id.in_(config['trade_ids']))
|
|
|
|
|
2020-05-02 09:44:18 +00:00
|
|
|
trades = Trade.get_trades(tfilter).all()
|
|
|
|
logger.info(f"Printing {len(trades)} Trades: ")
|
2020-05-02 09:26:12 +00:00
|
|
|
if config.get('print_json', False):
|
|
|
|
print(json.dumps([trade.to_json() for trade in trades], indent=4))
|
|
|
|
else:
|
|
|
|
for trade in trades:
|
|
|
|
print(trade)
|