2020-01-26 12:08:58 +00:00
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
from pathlib import Path
|
2024-10-06 06:28:55 +00:00
|
|
|
from typing import Any
|
2020-01-26 12:08:58 +00:00
|
|
|
|
2021-09-11 06:36:57 +00:00
|
|
|
from freqtrade.constants import USERPATH_STRATEGIES
|
2021-06-08 19:20:35 +00:00
|
|
|
from freqtrade.enums import RunMode
|
2024-03-19 06:13:17 +00:00
|
|
|
from freqtrade.exceptions import ConfigurationError, OperationalException
|
2020-01-26 12:08:58 +00:00
|
|
|
|
2020-09-28 17:39:41 +00:00
|
|
|
|
2020-01-26 12:08:58 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2024-04-20 08:25:05 +00:00
|
|
|
# Timeout for requests
|
|
|
|
req_timeout = 30
|
|
|
|
|
|
|
|
|
2024-10-04 04:37:49 +00:00
|
|
|
def start_create_userdir(args: dict[str, Any]) -> None:
|
2020-01-26 12:08:58 +00:00
|
|
|
"""
|
|
|
|
Create "user_data" directory to contain user data strategies, hyperopt, ...)
|
|
|
|
:param args: Cli args from Arguments()
|
|
|
|
:return: None
|
|
|
|
"""
|
2024-10-03 03:33:52 +00:00
|
|
|
from freqtrade.configuration.directory_operations import copy_sample_files, create_userdata_dir
|
|
|
|
|
2020-01-26 12:08:58 +00:00
|
|
|
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
|
|
|
|
"""
|
2024-10-03 03:33:52 +00:00
|
|
|
from freqtrade.util import render_template, render_template_with_fallback
|
|
|
|
|
2024-05-12 14:27:03 +00:00
|
|
|
fallback = "full"
|
2023-08-15 04:58:50 +00:00
|
|
|
attributes = render_template_with_fallback(
|
|
|
|
templatefile=f"strategy_subtemplates/strategy_attributes_{subtemplate}.j2",
|
|
|
|
templatefallbackfile=f"strategy_subtemplates/strategy_attributes_{fallback}.j2",
|
|
|
|
)
|
2020-03-01 08:35:53 +00:00
|
|
|
indicators = render_template_with_fallback(
|
2022-09-18 06:50:41 +00:00
|
|
|
templatefile=f"strategy_subtemplates/indicators_{subtemplate}.j2",
|
|
|
|
templatefallbackfile=f"strategy_subtemplates/indicators_{fallback}.j2",
|
2021-08-06 22:19:36 +00:00
|
|
|
)
|
2020-03-01 08:35:53 +00:00
|
|
|
buy_trend = render_template_with_fallback(
|
2022-09-18 06:50:41 +00:00
|
|
|
templatefile=f"strategy_subtemplates/buy_trend_{subtemplate}.j2",
|
|
|
|
templatefallbackfile=f"strategy_subtemplates/buy_trend_{fallback}.j2",
|
2021-08-06 22:19:36 +00:00
|
|
|
)
|
2020-03-01 08:35:53 +00:00
|
|
|
sell_trend = render_template_with_fallback(
|
2022-09-18 06:50:41 +00:00
|
|
|
templatefile=f"strategy_subtemplates/sell_trend_{subtemplate}.j2",
|
|
|
|
templatefallbackfile=f"strategy_subtemplates/sell_trend_{fallback}.j2",
|
2021-08-06 22:19:36 +00:00
|
|
|
)
|
2020-03-01 08:35:53 +00:00
|
|
|
plot_config = render_template_with_fallback(
|
2022-09-18 06:50:41 +00:00
|
|
|
templatefile=f"strategy_subtemplates/plot_config_{subtemplate}.j2",
|
|
|
|
templatefallbackfile=f"strategy_subtemplates/plot_config_{fallback}.j2",
|
2020-03-01 08:35:53 +00:00
|
|
|
)
|
|
|
|
additional_methods = render_template_with_fallback(
|
2022-09-18 06:50:41 +00:00
|
|
|
templatefile=f"strategy_subtemplates/strategy_methods_{subtemplate}.j2",
|
|
|
|
templatefallbackfile="strategy_subtemplates/strategy_methods_empty.j2",
|
2020-03-01 08:35:53 +00:00
|
|
|
)
|
2020-01-26 12:08:58 +00:00
|
|
|
|
2024-05-12 14:27:03 +00:00
|
|
|
strategy_text = render_template(
|
|
|
|
templatefile="base_strategy.py.j2",
|
|
|
|
arguments={
|
|
|
|
"strategy": strategy_name,
|
|
|
|
"attributes": attributes,
|
|
|
|
"indicators": indicators,
|
|
|
|
"buy_trend": buy_trend,
|
|
|
|
"sell_trend": sell_trend,
|
|
|
|
"plot_config": plot_config,
|
|
|
|
"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)
|
|
|
|
|
|
|
|
|
2024-10-04 04:37:49 +00:00
|
|
|
def start_new_strategy(args: dict[str, Any]) -> None:
|
2024-10-03 03:33:52 +00:00
|
|
|
from freqtrade.configuration import setup_utils_configuration
|
|
|
|
|
2020-01-26 12:08:58 +00:00
|
|
|
config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE)
|
|
|
|
|
|
|
|
if "strategy" in args and args["strategy"]:
|
2024-11-10 08:01:44 +00:00
|
|
|
if "strategy_path" in args and args["strategy_path"]:
|
|
|
|
strategy_dir = Path(args["strategy_path"])
|
|
|
|
else:
|
|
|
|
strategy_dir = config["user_data_dir"] / USERPATH_STRATEGIES
|
|
|
|
new_path = strategy_dir / (args["strategy"] + ".py")
|
2020-01-26 12:08:58 +00:00
|
|
|
|
|
|
|
if new_path.exists():
|
2024-05-12 14:27:03 +00:00
|
|
|
raise OperationalException(
|
2024-05-12 15:51:21 +00:00
|
|
|
f"`{new_path}` already exists. Please choose another Strategy Name."
|
2024-05-12 14:27:03 +00:00
|
|
|
)
|
2020-01-26 12:08:58 +00:00
|
|
|
|
2024-05-12 14:27:03 +00:00
|
|
|
deploy_new_strategy(args["strategy"], new_path, args["template"])
|
2020-01-26 12:08:58 +00:00
|
|
|
|
|
|
|
else:
|
2024-03-19 06:13:17 +00:00
|
|
|
raise ConfigurationError("`new-strategy` requires --strategy to be set.")
|
2020-01-26 12:08:58 +00:00
|
|
|
|
|
|
|
|
2024-10-04 04:37:49 +00:00
|
|
|
def start_install_ui(args: dict[str, Any]) -> None:
|
2024-10-05 11:06:19 +00:00
|
|
|
from freqtrade.commands.deploy_ui import (
|
|
|
|
clean_ui_subdir,
|
|
|
|
download_and_install_ui,
|
|
|
|
get_ui_download_url,
|
|
|
|
read_ui_version,
|
|
|
|
)
|
|
|
|
|
2024-05-12 14:27:03 +00:00
|
|
|
dest_folder = Path(__file__).parents[1] / "rpc/api_server/ui/installed/"
|
2021-01-10 13:46:59 +00:00
|
|
|
# First make sure the assets are removed.
|
2024-05-12 14:27:03 +00:00
|
|
|
dl_url, latest_version = get_ui_download_url(args.get("ui_version"))
|
2021-01-31 13:39:46 +00:00
|
|
|
|
|
|
|
curr_version = read_ui_version(dest_folder)
|
2024-05-12 14:27:03 +00:00
|
|
|
if curr_version == latest_version and not args.get("erase_ui_only"):
|
2021-01-31 13:43:16 +00:00
|
|
|
logger.info(f"UI already up-to-date, FreqUI Version {curr_version}.")
|
2021-01-31 13:39:46 +00:00
|
|
|
return
|
|
|
|
|
2021-01-31 13:43:16 +00:00
|
|
|
clean_ui_subdir(dest_folder)
|
2024-05-12 14:27:03 +00:00
|
|
|
if args.get("erase_ui_only"):
|
2021-01-16 09:15:27 +00:00
|
|
|
logger.info("Erased UI directory content. Not downloading new version.")
|
|
|
|
else:
|
|
|
|
# Download a new version
|
2021-01-31 13:39:46 +00:00
|
|
|
download_and_install_ui(dest_folder, dl_url, latest_version)
|