2020-01-29 20:28:01 +00:00
|
|
|
import logging
|
2021-03-28 18:19:39 +00:00
|
|
|
import secrets
|
2020-01-29 20:28:01 +00:00
|
|
|
from pathlib import Path
|
2020-10-05 14:17:37 +00:00
|
|
|
from typing import Any, Dict, List
|
2020-01-29 20:28:01 +00:00
|
|
|
|
|
|
|
from questionary import Separator, prompt
|
|
|
|
|
2021-04-08 18:07:52 +00:00
|
|
|
from freqtrade.configuration.directory_operations import chown_user_directory
|
2020-01-29 20:59:24 +00:00
|
|
|
from freqtrade.constants import UNLIMITED_STAKE_AMOUNT
|
2020-01-29 20:47:05 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2020-09-28 17:39:41 +00:00
|
|
|
from freqtrade.exchange import MAP_EXCHANGE_CHILDCLASS, available_exchanges
|
|
|
|
from freqtrade.misc import render_template
|
|
|
|
|
|
|
|
|
2020-01-29 20:28:01 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-01-29 20:59:24 +00:00
|
|
|
def validate_is_int(val):
|
|
|
|
try:
|
|
|
|
_ = int(val)
|
|
|
|
return True
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def validate_is_float(val):
|
|
|
|
try:
|
|
|
|
_ = float(val)
|
|
|
|
return True
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-02-01 13:22:40 +00:00
|
|
|
def ask_user_overwrite(config_path: Path) -> bool:
|
|
|
|
questions = [
|
|
|
|
{
|
|
|
|
"type": "confirm",
|
|
|
|
"name": "overwrite",
|
|
|
|
"message": f"File {config_path} already exists. Overwrite?",
|
|
|
|
"default": False,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
answers = prompt(questions)
|
|
|
|
return answers['overwrite']
|
|
|
|
|
|
|
|
|
2020-01-29 20:28:01 +00:00
|
|
|
def ask_user_config() -> Dict[str, Any]:
|
|
|
|
"""
|
|
|
|
Ask user a few questions to build the configuration.
|
2020-01-29 20:47:05 +00:00
|
|
|
Interactive questions built using https://github.com/tmbo/questionary
|
2020-01-29 20:28:01 +00:00
|
|
|
:returns: Dict with keys to put into template
|
|
|
|
"""
|
2020-10-05 14:17:37 +00:00
|
|
|
questions: List[Dict[str, Any]] = [
|
2020-01-29 20:28:01 +00:00
|
|
|
{
|
|
|
|
"type": "confirm",
|
|
|
|
"name": "dry_run",
|
|
|
|
"message": "Do you want to enable Dry-run (simulated trades)?",
|
|
|
|
"default": True,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "text",
|
|
|
|
"name": "stake_currency",
|
|
|
|
"message": "Please insert your stake currency:",
|
2021-09-03 06:59:08 +00:00
|
|
|
"default": 'USDT',
|
2020-01-29 20:28:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "text",
|
|
|
|
"name": "stake_amount",
|
2021-08-30 18:00:52 +00:00
|
|
|
"message": f"Please insert your stake amount (Number or '{UNLIMITED_STAKE_AMOUNT}'):",
|
2021-09-03 06:59:08 +00:00
|
|
|
"default": "100",
|
2020-01-29 20:59:24 +00:00
|
|
|
"validate": lambda val: val == UNLIMITED_STAKE_AMOUNT or validate_is_float(val),
|
2021-08-29 17:02:48 +00:00
|
|
|
"filter": lambda val: '"' + UNLIMITED_STAKE_AMOUNT + '"'
|
|
|
|
if val == UNLIMITED_STAKE_AMOUNT
|
|
|
|
else val
|
2020-01-29 20:28:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "text",
|
|
|
|
"name": "max_open_trades",
|
2022-01-30 12:19:05 +00:00
|
|
|
"message": "Please insert max_open_trades (Integer or -1 for unlimited open trades):",
|
2020-01-29 20:28:01 +00:00
|
|
|
"default": "3",
|
2022-01-30 12:19:05 +00:00
|
|
|
"validate": lambda val: validate_is_int(val)
|
2020-01-29 20:28:01 +00:00
|
|
|
},
|
2021-11-01 12:39:25 +00:00
|
|
|
{
|
|
|
|
"type": "select",
|
|
|
|
"name": "timeframe_in_config",
|
2022-01-13 01:48:38 +00:00
|
|
|
"message": "Time",
|
2021-11-01 12:39:25 +00:00
|
|
|
"choices": ["Have the strategy define timeframe.", "Override in configuration."]
|
|
|
|
},
|
2020-01-29 20:28:01 +00:00
|
|
|
{
|
|
|
|
"type": "text",
|
2020-06-01 18:15:48 +00:00
|
|
|
"name": "timeframe",
|
|
|
|
"message": "Please insert your desired timeframe (e.g. 5m):",
|
2020-01-29 20:28:01 +00:00
|
|
|
"default": "5m",
|
2021-11-01 12:39:25 +00:00
|
|
|
"when": lambda x: x["timeframe_in_config"] == 'Override in configuration.'
|
|
|
|
|
2020-01-29 20:28:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "text",
|
|
|
|
"name": "fiat_display_currency",
|
|
|
|
"message": "Please insert your display Currency (for reporting):",
|
|
|
|
"default": 'USD',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "select",
|
|
|
|
"name": "exchange_name",
|
|
|
|
"message": "Select exchange",
|
2022-02-02 13:46:44 +00:00
|
|
|
"choices": lambda x: [
|
2020-01-29 20:28:01 +00:00
|
|
|
"binance",
|
|
|
|
"binanceus",
|
2020-02-08 12:51:55 +00:00
|
|
|
"bittrex",
|
2021-02-26 06:58:15 +00:00
|
|
|
"ftx",
|
2021-09-03 06:54:15 +00:00
|
|
|
"gateio",
|
2022-01-16 12:17:00 +00:00
|
|
|
"huobi",
|
2022-02-02 13:46:44 +00:00
|
|
|
"kraken",
|
|
|
|
"kucoin",
|
2022-02-08 18:45:39 +00:00
|
|
|
"okx",
|
2022-02-11 16:02:04 +00:00
|
|
|
Separator("------------------"),
|
2020-01-29 20:28:01 +00:00
|
|
|
"other",
|
|
|
|
],
|
|
|
|
},
|
2022-02-02 13:46:44 +00:00
|
|
|
{
|
|
|
|
"type": "confirm",
|
|
|
|
"name": "trading_mode",
|
|
|
|
"message": "Do you want to trade Perpetual Swaps (perpetual futures)?",
|
|
|
|
"default": False,
|
|
|
|
"filter": lambda val: 'futures' if val else 'spot',
|
2022-03-23 05:56:48 +00:00
|
|
|
"when": lambda x: x["exchange_name"] in ['binance', 'gateio', 'okx'],
|
2022-02-02 13:46:44 +00:00
|
|
|
},
|
2020-01-29 20:28:01 +00:00
|
|
|
{
|
|
|
|
"type": "autocomplete",
|
|
|
|
"name": "exchange_name",
|
|
|
|
"message": "Type your exchange name (Must be supported by ccxt)",
|
|
|
|
"choices": available_exchanges(),
|
|
|
|
"when": lambda x: x["exchange_name"] == 'other'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "password",
|
|
|
|
"name": "exchange_key",
|
|
|
|
"message": "Insert Exchange Key",
|
|
|
|
"when": lambda x: not x['dry_run']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "password",
|
|
|
|
"name": "exchange_secret",
|
|
|
|
"message": "Insert Exchange Secret",
|
|
|
|
"when": lambda x: not x['dry_run']
|
|
|
|
},
|
2021-09-03 06:54:15 +00:00
|
|
|
{
|
|
|
|
"type": "password",
|
|
|
|
"name": "exchange_key_password",
|
|
|
|
"message": "Insert Exchange API Key password",
|
2022-02-08 18:45:39 +00:00
|
|
|
"when": lambda x: not x['dry_run'] and x['exchange_name'] in ('kucoin', 'okx')
|
2021-09-03 06:54:15 +00:00
|
|
|
},
|
2020-01-29 20:28:01 +00:00
|
|
|
{
|
|
|
|
"type": "confirm",
|
|
|
|
"name": "telegram",
|
|
|
|
"message": "Do you want to enable Telegram?",
|
|
|
|
"default": False,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "password",
|
|
|
|
"name": "telegram_token",
|
|
|
|
"message": "Insert Telegram token",
|
|
|
|
"when": lambda x: x['telegram']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "text",
|
|
|
|
"name": "telegram_chat_id",
|
|
|
|
"message": "Insert Telegram chat id",
|
|
|
|
"when": lambda x: x['telegram']
|
|
|
|
},
|
2021-03-28 18:19:39 +00:00
|
|
|
{
|
|
|
|
"type": "confirm",
|
|
|
|
"name": "api_server",
|
|
|
|
"message": "Do you want to enable the Rest API (includes FreqUI)?",
|
|
|
|
"default": False,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "text",
|
|
|
|
"name": "api_server_listen_addr",
|
2021-10-06 18:14:59 +00:00
|
|
|
"message": ("Insert Api server Listen Address (0.0.0.0 for docker, "
|
|
|
|
"otherwise best left untouched)"),
|
2021-03-28 18:19:39 +00:00
|
|
|
"default": "127.0.0.1",
|
|
|
|
"when": lambda x: x['api_server']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "text",
|
|
|
|
"name": "api_server_username",
|
|
|
|
"message": "Insert api-server username",
|
|
|
|
"default": "freqtrader",
|
|
|
|
"when": lambda x: x['api_server']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "text",
|
|
|
|
"name": "api_server_password",
|
|
|
|
"message": "Insert api-server password",
|
|
|
|
"when": lambda x: x['api_server']
|
|
|
|
},
|
2020-01-29 20:28:01 +00:00
|
|
|
]
|
|
|
|
answers = prompt(questions)
|
|
|
|
|
2020-01-29 20:47:05 +00:00
|
|
|
if not answers:
|
|
|
|
# Interrupted questionary sessions return an empty dict.
|
|
|
|
raise OperationalException("User interrupted interactive questions.")
|
2022-02-02 13:46:44 +00:00
|
|
|
answers['margin_mode'] = (
|
|
|
|
'isolated'
|
|
|
|
if answers.get('trading_mode') == 'futures'
|
|
|
|
else ''
|
|
|
|
)
|
2021-03-28 18:19:39 +00:00
|
|
|
# Force JWT token to be a random string
|
|
|
|
answers['api_server_jwt_key'] = secrets.token_hex()
|
|
|
|
|
2020-01-29 20:47:05 +00:00
|
|
|
return answers
|
2020-01-29 20:28:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def deploy_new_config(config_path: Path, selections: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
Applies selections to the template and writes the result to config_path
|
|
|
|
:param config_path: Path object for new config file. Should not exist yet
|
2021-06-25 13:45:49 +00:00
|
|
|
:param selections: Dict containing selections taken by the user.
|
2020-01-29 20:28:01 +00:00
|
|
|
"""
|
|
|
|
from jinja2.exceptions import TemplateNotFound
|
|
|
|
try:
|
2020-02-01 12:46:58 +00:00
|
|
|
exchange_template = MAP_EXCHANGE_CHILDCLASS.get(
|
|
|
|
selections['exchange_name'], selections['exchange_name'])
|
|
|
|
|
2020-01-29 20:28:01 +00:00
|
|
|
selections['exchange'] = render_template(
|
2020-02-01 12:46:58 +00:00
|
|
|
templatefile=f"subtemplates/exchange_{exchange_template}.j2",
|
2020-01-29 20:28:01 +00:00
|
|
|
arguments=selections
|
2021-08-06 22:19:36 +00:00
|
|
|
)
|
2020-01-29 20:28:01 +00:00
|
|
|
except TemplateNotFound:
|
|
|
|
selections['exchange'] = render_template(
|
2020-05-18 09:40:25 +00:00
|
|
|
templatefile="subtemplates/exchange_generic.j2",
|
2020-01-29 20:28:01 +00:00
|
|
|
arguments=selections
|
|
|
|
)
|
|
|
|
|
|
|
|
config_text = render_template(templatefile='base_config.json.j2',
|
|
|
|
arguments=selections)
|
|
|
|
|
|
|
|
logger.info(f"Writing config to `{config_path}`.")
|
2021-02-26 06:58:15 +00:00
|
|
|
logger.info(
|
|
|
|
"Please make sure to check the configuration contents and adjust settings to your needs.")
|
|
|
|
|
2020-01-29 20:28:01 +00:00
|
|
|
config_path.write_text(config_text)
|
|
|
|
|
|
|
|
|
|
|
|
def start_new_config(args: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
Create a new strategy from a template
|
2021-06-25 13:45:49 +00:00
|
|
|
Asking the user questions to fill out the template accordingly.
|
2020-01-29 20:28:01 +00:00
|
|
|
"""
|
2020-01-29 20:47:05 +00:00
|
|
|
|
2020-01-29 20:28:01 +00:00
|
|
|
config_path = Path(args['config'][0])
|
2021-04-08 18:07:52 +00:00
|
|
|
chown_user_directory(config_path.parent)
|
2020-02-01 13:12:21 +00:00
|
|
|
if config_path.exists():
|
2020-02-01 13:22:40 +00:00
|
|
|
overwrite = ask_user_overwrite(config_path)
|
|
|
|
if overwrite:
|
|
|
|
config_path.unlink()
|
|
|
|
else:
|
|
|
|
raise OperationalException(
|
2020-02-09 10:41:29 +00:00
|
|
|
f"Configuration file `{config_path}` already exists. "
|
|
|
|
"Please delete it or use a different configuration file name.")
|
2020-02-01 13:12:21 +00:00
|
|
|
selections = ask_user_config()
|
2020-01-29 20:28:01 +00:00
|
|
|
deploy_new_config(config_path, selections)
|