From 23435512c461a0b3331314f4e06911d90fe94497 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 21 Jul 2019 14:13:38 +0200 Subject: [PATCH] Add create-userdir command to initialize a user directory --- freqtrade/configuration/arguments.py | 10 ++++++++-- freqtrade/configuration/cli_options.py | 5 +++++ freqtrade/configuration/configuration.py | 9 ++++++++- freqtrade/tests/conftest.py | 1 + freqtrade/utils.py | 16 +++++++++++++++- 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/freqtrade/configuration/arguments.py b/freqtrade/configuration/arguments.py index 4f0c3d31b..0651d9544 100644 --- a/freqtrade/configuration/arguments.py +++ b/freqtrade/configuration/arguments.py @@ -31,6 +31,8 @@ ARGS_EDGE = ARGS_COMMON_OPTIMIZE + ["stoploss_range"] ARGS_LIST_EXCHANGES = ["print_one_column"] +ARGS_CREATE_USERDIR = ["user_data_dir"] + ARGS_DOWNLOADER = ARGS_COMMON + ["pairs", "pairs_file", "days", "exchange", "timeframes", "erase"] ARGS_PLOT_DATAFRAME = (ARGS_COMMON + ARGS_STRATEGY + @@ -106,8 +108,7 @@ class Arguments(object): :return: None """ from freqtrade.optimize import start_backtesting, start_hyperopt, start_edge - from freqtrade.utils import start_list_exchanges - + from freqtrade.utils import start_create_userdir, start_list_exchanges subparsers = self.parser.add_subparsers(dest='subparser') # Add backtesting subcommand @@ -125,6 +126,11 @@ class Arguments(object): hyperopt_cmd.set_defaults(func=start_hyperopt) self._build_args(optionlist=ARGS_HYPEROPT, parser=hyperopt_cmd) + create_userdir_cmd = subparsers.add_parser('create-userdir', + help="Create user-data directory.") + create_userdir_cmd.set_defaults(func=start_create_userdir) + self._build_args(optionlist=ARGS_CREATE_USERDIR, parser=create_userdir_cmd) + # Add list-exchanges subcommand list_exchanges_cmd = subparsers.add_parser( 'list-exchanges', diff --git a/freqtrade/configuration/cli_options.py b/freqtrade/configuration/cli_options.py index 4ec280d0f..00c28ed07 100644 --- a/freqtrade/configuration/cli_options.py +++ b/freqtrade/configuration/cli_options.py @@ -58,6 +58,11 @@ AVAILABLE_CLI_OPTIONS = { help='Path to backtest data.', metavar='PATH', ), + "user_data_dir": Arg( + '--userdir', '--user-data-dir', + help='Path to Userdata Directory.', + metavar='PATH', + ), # Main options "strategy": Arg( '-s', '--strategy', diff --git a/freqtrade/configuration/configuration.py b/freqtrade/configuration/configuration.py index d96a291e2..0fd092728 100644 --- a/freqtrade/configuration/configuration.py +++ b/freqtrade/configuration/configuration.py @@ -6,11 +6,12 @@ import logging import sys import warnings from argparse import Namespace +from pathlib import Path from typing import Any, Callable, Dict, Optional from freqtrade import OperationalException, constants from freqtrade.configuration.check_exchange import check_exchange -from freqtrade.configuration.folder_operations import create_datadir +from freqtrade.configuration.folder_operations import create_datadir, create_userdata_dir from freqtrade.configuration.json_schema import validate_config_schema from freqtrade.loggers import setup_logging from freqtrade.misc import deep_merge_dicts @@ -173,6 +174,12 @@ class Configuration(object): Extract information for sys.argv and load datadir configuration: the --datadir option """ + if 'user_data_dir' in self.args and self.args.user_data_dir: + config.update({'user_data_dir': self.args.user_data_dir}) + create_userdata_dir(config['user_data_dir']) + elif 'user_data_dir' not in config: + config.update({'user_data_dir': str(Path.cwd() / "user_data")}) + if 'datadir' in self.args and self.args.datadir: config.update({'datadir': create_datadir(config, self.args.datadir)}) else: diff --git a/freqtrade/tests/conftest.py b/freqtrade/tests/conftest.py index 5862a2e89..de3b2e4d9 100644 --- a/freqtrade/tests/conftest.py +++ b/freqtrade/tests/conftest.py @@ -234,6 +234,7 @@ def default_conf(): }, "initial_state": "running", "db_url": "sqlite://", + "user_data_dir": "user_data", "verbosity": 3, } return configuration diff --git a/freqtrade/utils.py b/freqtrade/utils.py index d550ef43c..53325b70b 100644 --- a/freqtrade/utils.py +++ b/freqtrade/utils.py @@ -1,12 +1,13 @@ import logging +import sys from argparse import Namespace from typing import Any, Dict from freqtrade.configuration import Configuration +from freqtrade.configuration.folder_operations import create_userdata_dir from freqtrade.exchange import available_exchanges from freqtrade.state import RunMode - logger = logging.getLogger(__name__) @@ -39,3 +40,16 @@ def start_list_exchanges(args: Namespace) -> None: else: print(f"Exchanges supported by ccxt and available for Freqtrade: " f"{', '.join(available_exchanges())}") + + +def start_create_userdir(args: Namespace) -> None: + """ + Create "user_data" directory to contain user data strategies, hyperopts, ...) + :param args: Cli args from Arguments() + :return: None + """ + if "user_data_dir" in args and args.user_data_dir: + create_userdata_dir(args.user_data_dir) + else: + logger.warning("`create-userdir` requires --userdir to be set.") + sys.exit(1)