2020-01-26 12:08:58 +00:00
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
2021-01-10 13:06:06 +00:00
|
|
|
import requests
|
|
|
|
|
2020-01-26 12:55:48 +00:00
|
|
|
from freqtrade.configuration import setup_utils_configuration
|
2020-09-28 17:39:41 +00:00
|
|
|
from freqtrade.configuration.directory_operations import copy_sample_files, create_userdata_dir
|
2020-02-02 15:12:23 +00:00
|
|
|
from freqtrade.constants import USERPATH_HYPEROPTS, USERPATH_STRATEGIES
|
2020-01-26 12:08:58 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2020-03-01 08:35:53 +00:00
|
|
|
from freqtrade.misc import render_template, render_template_with_fallback
|
2020-01-26 12:08:58 +00:00
|
|
|
from freqtrade.state import RunMode
|
|
|
|
|
2020-09-28 17:39:41 +00:00
|
|
|
|
2020-01-26 12:08:58 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def start_create_userdir(args: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
Create "user_data" directory to contain user data strategies, hyperopt, ...)
|
|
|
|
:param args: Cli args from Arguments()
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
if "user_data_dir" in args and args["user_data_dir"]:
|
|
|
|
userdir = create_userdata_dir(args["user_data_dir"], create_dir=True)
|
|
|
|
copy_sample_files(userdir, overwrite=args["reset"])
|
|
|
|
else:
|
|
|
|
logger.warning("`create-userdir` requires --userdir to be set.")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2020-02-02 04:00:40 +00:00
|
|
|
def deploy_new_strategy(strategy_name: str, strategy_path: Path, subtemplate: str) -> None:
|
2020-01-26 12:08:58 +00:00
|
|
|
"""
|
|
|
|
Deploy new strategy from template to strategy_path
|
|
|
|
"""
|
2020-03-01 08:35:53 +00:00
|
|
|
fallback = 'full'
|
|
|
|
indicators = render_template_with_fallback(
|
|
|
|
templatefile=f"subtemplates/indicators_{subtemplate}.j2",
|
|
|
|
templatefallbackfile=f"subtemplates/indicators_{fallback}.j2",
|
|
|
|
)
|
|
|
|
buy_trend = render_template_with_fallback(
|
|
|
|
templatefile=f"subtemplates/buy_trend_{subtemplate}.j2",
|
|
|
|
templatefallbackfile=f"subtemplates/buy_trend_{fallback}.j2",
|
|
|
|
)
|
|
|
|
sell_trend = render_template_with_fallback(
|
|
|
|
templatefile=f"subtemplates/sell_trend_{subtemplate}.j2",
|
|
|
|
templatefallbackfile=f"subtemplates/sell_trend_{fallback}.j2",
|
|
|
|
)
|
|
|
|
plot_config = render_template_with_fallback(
|
|
|
|
templatefile=f"subtemplates/plot_config_{subtemplate}.j2",
|
|
|
|
templatefallbackfile=f"subtemplates/plot_config_{fallback}.j2",
|
|
|
|
)
|
|
|
|
additional_methods = render_template_with_fallback(
|
|
|
|
templatefile=f"subtemplates/strategy_methods_{subtemplate}.j2",
|
2020-05-18 09:40:25 +00:00
|
|
|
templatefallbackfile="subtemplates/strategy_methods_empty.j2",
|
2020-03-01 08:35:53 +00:00
|
|
|
)
|
2020-01-26 12:08:58 +00:00
|
|
|
|
|
|
|
strategy_text = render_template(templatefile='base_strategy.py.j2',
|
|
|
|
arguments={"strategy": strategy_name,
|
|
|
|
"indicators": indicators,
|
|
|
|
"buy_trend": buy_trend,
|
|
|
|
"sell_trend": sell_trend,
|
|
|
|
"plot_config": plot_config,
|
2020-03-01 08:35:53 +00:00
|
|
|
"additional_methods": additional_methods,
|
2020-01-26 12:08:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
logger.info(f"Writing strategy to `{strategy_path}`.")
|
|
|
|
strategy_path.write_text(strategy_text)
|
|
|
|
|
|
|
|
|
|
|
|
def start_new_strategy(args: Dict[str, Any]) -> None:
|
|
|
|
|
|
|
|
config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE)
|
|
|
|
|
|
|
|
if "strategy" in args and args["strategy"]:
|
|
|
|
if args["strategy"] == "DefaultStrategy":
|
|
|
|
raise OperationalException("DefaultStrategy is not allowed as name.")
|
|
|
|
|
2020-08-26 18:52:09 +00:00
|
|
|
new_path = config['user_data_dir'] / USERPATH_STRATEGIES / (args['strategy'] + '.py')
|
2020-01-26 12:08:58 +00:00
|
|
|
|
|
|
|
if new_path.exists():
|
|
|
|
raise OperationalException(f"`{new_path}` already exists. "
|
|
|
|
"Please choose another Strategy Name.")
|
|
|
|
|
|
|
|
deploy_new_strategy(args['strategy'], new_path, args['template'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise OperationalException("`new-strategy` requires --strategy to be set.")
|
|
|
|
|
|
|
|
|
2020-02-02 04:00:40 +00:00
|
|
|
def deploy_new_hyperopt(hyperopt_name: str, hyperopt_path: Path, subtemplate: str) -> None:
|
2020-01-26 12:08:58 +00:00
|
|
|
"""
|
|
|
|
Deploys a new hyperopt template to hyperopt_path
|
|
|
|
"""
|
2020-03-01 08:35:53 +00:00
|
|
|
fallback = 'full'
|
|
|
|
buy_guards = render_template_with_fallback(
|
|
|
|
templatefile=f"subtemplates/hyperopt_buy_guards_{subtemplate}.j2",
|
|
|
|
templatefallbackfile=f"subtemplates/hyperopt_buy_guards_{fallback}.j2",
|
|
|
|
)
|
|
|
|
sell_guards = render_template_with_fallback(
|
|
|
|
templatefile=f"subtemplates/hyperopt_sell_guards_{subtemplate}.j2",
|
|
|
|
templatefallbackfile=f"subtemplates/hyperopt_sell_guards_{fallback}.j2",
|
|
|
|
)
|
|
|
|
buy_space = render_template_with_fallback(
|
|
|
|
templatefile=f"subtemplates/hyperopt_buy_space_{subtemplate}.j2",
|
|
|
|
templatefallbackfile=f"subtemplates/hyperopt_buy_space_{fallback}.j2",
|
|
|
|
)
|
|
|
|
sell_space = render_template_with_fallback(
|
|
|
|
templatefile=f"subtemplates/hyperopt_sell_space_{subtemplate}.j2",
|
|
|
|
templatefallbackfile=f"subtemplates/hyperopt_sell_space_{fallback}.j2",
|
|
|
|
)
|
2020-01-26 12:08:58 +00:00
|
|
|
|
|
|
|
strategy_text = render_template(templatefile='base_hyperopt.py.j2',
|
|
|
|
arguments={"hyperopt": hyperopt_name,
|
|
|
|
"buy_guards": buy_guards,
|
|
|
|
"sell_guards": sell_guards,
|
|
|
|
"buy_space": buy_space,
|
|
|
|
"sell_space": sell_space,
|
|
|
|
})
|
|
|
|
|
|
|
|
logger.info(f"Writing hyperopt to `{hyperopt_path}`.")
|
|
|
|
hyperopt_path.write_text(strategy_text)
|
|
|
|
|
|
|
|
|
|
|
|
def start_new_hyperopt(args: Dict[str, Any]) -> None:
|
|
|
|
|
|
|
|
config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE)
|
|
|
|
|
2020-08-26 18:52:09 +00:00
|
|
|
if 'hyperopt' in args and args['hyperopt']:
|
|
|
|
if args['hyperopt'] == 'DefaultHyperopt':
|
2020-01-26 12:08:58 +00:00
|
|
|
raise OperationalException("DefaultHyperopt is not allowed as name.")
|
|
|
|
|
2020-08-26 18:52:09 +00:00
|
|
|
new_path = config['user_data_dir'] / USERPATH_HYPEROPTS / (args['hyperopt'] + '.py')
|
2020-01-26 12:08:58 +00:00
|
|
|
|
|
|
|
if new_path.exists():
|
|
|
|
raise OperationalException(f"`{new_path}` already exists. "
|
2020-10-29 06:44:03 +00:00
|
|
|
"Please choose another Hyperopt Name.")
|
2020-01-26 12:08:58 +00:00
|
|
|
deploy_new_hyperopt(args['hyperopt'], new_path, args['template'])
|
|
|
|
else:
|
|
|
|
raise OperationalException("`new-hyperopt` requires --hyperopt to be set.")
|
2021-01-10 12:55:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def clean_ui_subdir(directory: Path):
|
|
|
|
if directory.is_dir():
|
2021-01-10 13:06:06 +00:00
|
|
|
logger.info("Removing UI directory content.")
|
2021-01-10 12:55:16 +00:00
|
|
|
|
|
|
|
for p in reversed(list(directory.glob('**/*'))): # iterate contents from leaves to root
|
|
|
|
if p.name == '.gitkeep':
|
|
|
|
continue
|
|
|
|
if p.is_file():
|
|
|
|
p.unlink()
|
|
|
|
elif p.is_dir():
|
|
|
|
p.rmdir()
|
|
|
|
|
|
|
|
|
2021-01-10 13:06:06 +00:00
|
|
|
def download_and_install_ui(dest_folder: Path, dl_url: str):
|
2021-01-10 12:55:16 +00:00
|
|
|
from io import BytesIO
|
|
|
|
from zipfile import ZipFile
|
|
|
|
|
|
|
|
logger.info(f"Downloading {dl_url}")
|
|
|
|
resp = requests.get(dl_url).content
|
|
|
|
with ZipFile(BytesIO(resp)) as zf:
|
|
|
|
for fn in zf.filelist:
|
|
|
|
with zf.open(fn) as x:
|
|
|
|
destfile = dest_folder / fn.filename
|
|
|
|
if fn.is_dir():
|
|
|
|
destfile.mkdir(exist_ok=True)
|
|
|
|
else:
|
|
|
|
destfile.write_bytes(x.read())
|
|
|
|
|
|
|
|
|
2021-01-10 13:46:59 +00:00
|
|
|
def get_ui_download_url() -> str:
|
2021-01-10 13:06:06 +00:00
|
|
|
base_url = 'https://api.github.com/repos/freqtrade/frequi/'
|
|
|
|
# Get base UI Repo path
|
|
|
|
|
|
|
|
resp = requests.get(f"{base_url}releases")
|
|
|
|
resp.raise_for_status()
|
|
|
|
r = resp.json()
|
|
|
|
|
|
|
|
assets = r[0]['assets_url']
|
|
|
|
resp = requests.get(assets)
|
|
|
|
r = resp.json()
|
|
|
|
|
|
|
|
dl_url = r[0]['browser_download_url']
|
2021-01-10 13:46:59 +00:00
|
|
|
return dl_url
|
|
|
|
|
|
|
|
|
|
|
|
def start_install_ui(args: Dict[str, Any]) -> None:
|
|
|
|
|
|
|
|
dest_folder = Path(__file__).parents[1] / 'rpc/api_server/ui'
|
|
|
|
# First make sure the assets are removed.
|
|
|
|
clean_ui_subdir(dest_folder)
|
2021-01-16 09:15:27 +00:00
|
|
|
if args.get('erase_ui_only'):
|
|
|
|
logger.info("Erased UI directory content. Not downloading new version.")
|
|
|
|
else:
|
|
|
|
dl_url = get_ui_download_url()
|
2021-01-10 13:46:59 +00:00
|
|
|
|
2021-01-16 09:15:27 +00:00
|
|
|
# Download a new version
|
|
|
|
download_and_install_ui(dest_folder, dl_url)
|