2020-01-29 20:28:01 +00:00
|
|
|
import logging
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
from questionary import Separator, prompt
|
|
|
|
|
2020-01-29 20:59:24 +00:00
|
|
|
from freqtrade.constants import UNLIMITED_STAKE_AMOUNT
|
2020-02-01 12:46:58 +00:00
|
|
|
from freqtrade.exchange import available_exchanges, MAP_EXCHANGE_CHILDCLASS
|
2020-01-29 20:28:01 +00:00
|
|
|
from freqtrade.misc import render_template
|
2020-01-29 20:47:05 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
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-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
|
|
|
|
"""
|
|
|
|
questions = [
|
|
|
|
{
|
|
|
|
"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:",
|
|
|
|
"default": 'BTC',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "text",
|
|
|
|
"name": "stake_amount",
|
|
|
|
"message": "Please insert your stake amount:",
|
|
|
|
"default": "0.01",
|
2020-01-29 20:59:24 +00:00
|
|
|
"validate": lambda val: val == UNLIMITED_STAKE_AMOUNT or validate_is_float(val),
|
2020-01-29 20:28:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "text",
|
|
|
|
"name": "max_open_trades",
|
2020-01-29 20:59:24 +00:00
|
|
|
"message": f"Please insert max_open_trades (Integer or '{UNLIMITED_STAKE_AMOUNT}'):",
|
2020-01-29 20:28:01 +00:00
|
|
|
"default": "3",
|
2020-01-29 20:59:24 +00:00
|
|
|
"validate": lambda val: val == UNLIMITED_STAKE_AMOUNT or validate_is_int(val)
|
2020-01-29 20:28:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "text",
|
|
|
|
"name": "ticker_interval",
|
|
|
|
"message": "Please insert your ticker interval:",
|
|
|
|
"default": "5m",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "text",
|
|
|
|
"name": "fiat_display_currency",
|
|
|
|
"message": "Please insert your display Currency (for reporting):",
|
|
|
|
"default": 'USD',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "select",
|
|
|
|
"name": "exchange_name",
|
|
|
|
"message": "Select exchange",
|
|
|
|
"choices": [
|
|
|
|
"bittrex",
|
|
|
|
"binance",
|
|
|
|
"binanceje",
|
|
|
|
"binanceus",
|
|
|
|
"kraken",
|
|
|
|
Separator(),
|
|
|
|
"other",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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']
|
|
|
|
},
|
|
|
|
]
|
|
|
|
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.")
|
2020-01-29 20:28:01 +00:00
|
|
|
|
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
|
|
|
|
:param selecions: Dict containing selections taken by the user.
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
)
|
|
|
|
except TemplateNotFound:
|
|
|
|
selections['exchange'] = render_template(
|
|
|
|
templatefile=f"subtemplates/exchange_generic.j2",
|
|
|
|
arguments=selections
|
|
|
|
)
|
|
|
|
|
|
|
|
config_text = render_template(templatefile='base_config.json.j2',
|
|
|
|
arguments=selections)
|
|
|
|
|
|
|
|
logger.info(f"Writing config to `{config_path}`.")
|
|
|
|
config_path.write_text(config_text)
|
|
|
|
|
|
|
|
|
|
|
|
def start_new_config(args: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
Create a new strategy from a template
|
|
|
|
Asking the user questions to fill out the templateaccordingly.
|
|
|
|
"""
|
2020-01-29 20:47:05 +00:00
|
|
|
|
2020-01-29 20:28:01 +00:00
|
|
|
config_path = Path(args['config'][0])
|
2020-02-01 13:12:21 +00:00
|
|
|
if config_path.exists():
|
|
|
|
raise OperationalException(
|
|
|
|
f"Configuration `{config_path}` already exists. "
|
|
|
|
"Please use another configuration name or delete the existing configuration.")
|
|
|
|
selections = ask_user_config()
|
2020-01-29 20:28:01 +00:00
|
|
|
deploy_new_config(config_path, selections)
|