From 85979c317679a58172fd15c2432d3c94cb1d9c4f Mon Sep 17 00:00:00 2001 From: Cryptomeister Nox Date: Thu, 17 Jun 2021 20:35:02 +0200 Subject: [PATCH 1/8] * Adding command for Filtering * Read latest Backtest file and print trades --- freqtrade/commands/__init__.py | 2 +- freqtrade/commands/arguments.py | 16 ++++++++++++-- freqtrade/commands/cli_options.py | 5 +++++ freqtrade/commands/optimize_commands.py | 28 +++++++++++++++++++++++++ freqtrade/optimize/optimize_reports.py | 15 +++++++++++++ 5 files changed, 63 insertions(+), 3 deletions(-) diff --git a/freqtrade/commands/__init__.py b/freqtrade/commands/__init__.py index 784b99bed..ecce709e5 100644 --- a/freqtrade/commands/__init__.py +++ b/freqtrade/commands/__init__.py @@ -16,7 +16,7 @@ from freqtrade.commands.hyperopt_commands import start_hyperopt_list, start_hype from freqtrade.commands.list_commands import (start_list_exchanges, start_list_hyperopts, start_list_markets, start_list_strategies, start_list_timeframes, start_show_trades) -from freqtrade.commands.optimize_commands import start_backtesting, start_edge, start_hyperopt +from freqtrade.commands.optimize_commands import start_backtest_filter, start_backtesting, start_edge, start_hyperopt from freqtrade.commands.pairlist_commands import start_test_pairlist from freqtrade.commands.plot_commands import start_plot_dataframe, start_plot_profit from freqtrade.commands.trade_commands import start_trading diff --git a/freqtrade/commands/arguments.py b/freqtrade/commands/arguments.py index 7f4f7edd6..f5b9f9cc2 100644 --- a/freqtrade/commands/arguments.py +++ b/freqtrade/commands/arguments.py @@ -2,6 +2,7 @@ This module contains the argument manager class """ import argparse +from freqtrade.commands.optimize_commands import start_backtest_filter from functools import partial from pathlib import Path from typing import Any, Dict, List, Optional @@ -37,6 +38,8 @@ ARGS_LIST_STRATEGIES = ["strategy_path", "print_one_column", "print_colorized"] ARGS_LIST_HYPEROPTS = ["hyperopt_path", "print_one_column", "print_colorized"] +ARGS_BACKTEST_FILTER = ["backtest_path"] + ARGS_LIST_EXCHANGES = ["print_one_column", "list_exchanges_all"] ARGS_LIST_TIMEFRAMES = ["exchange", "print_one_column"] @@ -89,7 +92,7 @@ ARGS_HYPEROPT_SHOW = ["hyperopt_list_best", "hyperopt_list_profitable", "hyperop NO_CONF_REQURIED = ["convert-data", "convert-trade-data", "download-data", "list-timeframes", "list-markets", "list-pairs", "list-strategies", "list-data", - "list-hyperopts", "hyperopt-list", "hyperopt-show", + "list-hyperopts", "hyperopt-list", "backtest-filter", "hyperopt-show", "plot-dataframe", "plot-profit", "show-trades"] NO_CONF_ALLOWED = ["create-userdir", "list-exchanges", "new-hyperopt", "new-strategy"] @@ -168,7 +171,7 @@ class Arguments: self.parser = argparse.ArgumentParser(description='Free, open source crypto trading bot') self._build_args(optionlist=['version'], parser=self.parser) - from freqtrade.commands import (start_backtesting, start_convert_data, start_create_userdir, + from freqtrade.commands import (start_backtesting, start_backtest_filter, start_convert_data, start_create_userdir, start_download_data, start_edge, start_hyperopt, start_hyperopt_list, start_hyperopt_show, start_install_ui, start_list_data, start_list_exchanges, start_list_hyperopts, @@ -256,6 +259,15 @@ class Arguments: backtesting_cmd.set_defaults(func=start_backtesting) self._build_args(optionlist=ARGS_BACKTEST, parser=backtesting_cmd) + # Add backtest-filter subcommand + backtest_filter_cmd = subparsers.add_parser( + 'backtest-filter', + help='Filter Backtest results', + parents=[_common_parser], + ) + backtest_filter_cmd.set_defaults(func=start_backtest_filter) + self._build_args(optionlist=ARGS_BACKTEST_FILTER, parser=backtest_filter_cmd) + # Add edge subcommand edge_cmd = subparsers.add_parser('edge', help='Edge module.', parents=[_common_parser, _strategy_parser]) diff --git a/freqtrade/commands/cli_options.py b/freqtrade/commands/cli_options.py index d832693ee..8ba66b32d 100644 --- a/freqtrade/commands/cli_options.py +++ b/freqtrade/commands/cli_options.py @@ -202,6 +202,11 @@ AVAILABLE_CLI_OPTIONS = { help='Specify additional lookup path for Hyperopt and Hyperopt Loss functions.', metavar='PATH', ), + "backtest_path": Arg( + '--backtest-path', + help='Specify lookup file path for backtest filter.', + metavar='PATH', + ), "epochs": Arg( '-e', '--epochs', help='Specify number of epochs (default: %(default)d).', diff --git a/freqtrade/commands/optimize_commands.py b/freqtrade/commands/optimize_commands.py index a84b3b3bd..3165852fa 100644 --- a/freqtrade/commands/optimize_commands.py +++ b/freqtrade/commands/optimize_commands.py @@ -1,3 +1,7 @@ +from freqtrade.data.btanalysis import get_latest_backtest_filename +import pandas +from pandas.io import json +from freqtrade.optimize import backtesting import logging from typing import Any, Dict @@ -52,6 +56,30 @@ def start_backtesting(args: Dict[str, Any]) -> None: backtesting = Backtesting(config) backtesting.start() +def start_backtest_filter(args: Dict[str, Any]) -> None: + """ + List backtest pairs previously filtered + """ + + config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE) + + no_header = config.get('backtest_show_pair_list', False) + results_file = get_latest_backtest_filename( + config['user_data_dir'] / 'backtest_results/') + + logger.info("Using Backtesting result {results_file}") + + # load data using Python JSON module + with open(config['user_data_dir'] / 'backtest_results/' / results_file,'r') as f: + data = json.loads(f.read()) + strategy = list(data["strategy"])[0] + trades = data["strategy"][strategy] + + print(trades) + + + logger.info("Backtest filtering complete. ") + def start_hyperopt(args: Dict[str, Any]) -> None: """ diff --git a/freqtrade/optimize/optimize_reports.py b/freqtrade/optimize/optimize_reports.py index 84e052ac4..f401614c5 100644 --- a/freqtrade/optimize/optimize_reports.py +++ b/freqtrade/optimize/optimize_reports.py @@ -668,3 +668,18 @@ def show_backtest_results(config: Dict, backtest_stats: Dict): print(table) print('=' * len(table.splitlines()[0])) print('\nFor more details, please look at the detail tables above') + +def show_backtest_results_filtered(config: Dict, backtest_stats: Dict): + stake_currency = config['stake_currency'] + + for strategy, results in backtest_stats['strategy'].items(): + show_backtest_result(strategy, results, stake_currency) + + if len(backtest_stats['strategy']) > 1: + # Print Strategy summary table + + table = text_table_strategy(backtest_stats['strategy_comparison'], stake_currency) + print(' STRATEGY SUMMARY '.center(len(table.splitlines()[0]), '=')) + print(table) + print('=' * len(table.splitlines()[0])) + print('\nFor more details, please look at the detail tables above') From 0f3809345a737ab3e8279cd02ba78fd244472395 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 30 Oct 2021 10:28:12 +0200 Subject: [PATCH 2/8] Remove backtest-path parameter --- freqtrade/commands/__init__.py | 6 +++--- freqtrade/commands/arguments.py | 3 +-- freqtrade/commands/cli_options.py | 5 ----- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/freqtrade/commands/__init__.py b/freqtrade/commands/__init__.py index 323d85825..89b6fdc3a 100644 --- a/freqtrade/commands/__init__.py +++ b/freqtrade/commands/__init__.py @@ -13,9 +13,9 @@ from freqtrade.commands.data_commands import (start_convert_data, start_convert_ from freqtrade.commands.deploy_commands import (start_create_userdir, start_install_ui, start_new_strategy) from freqtrade.commands.hyperopt_commands import start_hyperopt_list, start_hyperopt_show -from freqtrade.commands.list_commands import (start_list_exchanges, start_list_hyperopts, - start_list_markets, start_list_strategies, - start_list_timeframes, start_show_trades) +from freqtrade.commands.list_commands import (start_list_exchanges, start_list_markets, + start_list_strategies, start_list_timeframes, + start_show_trades) from freqtrade.commands.optimize_commands import (start_backtest_filter, start_backtesting, start_edge, start_hyperopt) from freqtrade.commands.pairlist_commands import start_test_pairlist diff --git a/freqtrade/commands/arguments.py b/freqtrade/commands/arguments.py index 080c5a9f6..df4c35cc0 100644 --- a/freqtrade/commands/arguments.py +++ b/freqtrade/commands/arguments.py @@ -2,7 +2,6 @@ This module contains the argument manager class """ import argparse -from freqtrade.commands.optimize_commands import start_backtest_filter from functools import partial from pathlib import Path from typing import Any, Dict, List, Optional @@ -42,7 +41,7 @@ ARGS_LIST_STRATEGIES = ["strategy_path", "print_one_column", "print_colorized"] ARGS_LIST_HYPEROPTS = ["hyperopt_path", "print_one_column", "print_colorized"] -ARGS_BACKTEST_FILTER = ["backtest_path"] +ARGS_BACKTEST_FILTER = [] ARGS_LIST_EXCHANGES = ["print_one_column", "list_exchanges_all"] diff --git a/freqtrade/commands/cli_options.py b/freqtrade/commands/cli_options.py index 60c888756..8d9b28c40 100644 --- a/freqtrade/commands/cli_options.py +++ b/freqtrade/commands/cli_options.py @@ -218,11 +218,6 @@ AVAILABLE_CLI_OPTIONS = { help='Specify additional lookup path for Hyperopt Loss functions.', metavar='PATH', ), - "backtest_path": Arg( - '--backtest-path', - help='Specify lookup file path for backtest filter.', - metavar='PATH', - ), "epochs": Arg( '-e', '--epochs', help='Specify number of epochs (default: %(default)d).', From f47270943817d5ad3a863256d953289c59485eb7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 30 Oct 2021 10:50:40 +0200 Subject: [PATCH 3/8] Add option to show sorted pairlist Allows easy copy/pasting of the pairlist to a configuration --- freqtrade/commands/arguments.py | 21 +++++++++---------- freqtrade/commands/cli_options.py | 6 ++++++ freqtrade/commands/optimize_commands.py | 26 ++++++++---------------- freqtrade/configuration/configuration.py | 4 ++++ freqtrade/optimize/optimize_reports.py | 21 ++++++++----------- 5 files changed, 36 insertions(+), 42 deletions(-) diff --git a/freqtrade/commands/arguments.py b/freqtrade/commands/arguments.py index df4c35cc0..8f68e6521 100644 --- a/freqtrade/commands/arguments.py +++ b/freqtrade/commands/arguments.py @@ -41,7 +41,7 @@ ARGS_LIST_STRATEGIES = ["strategy_path", "print_one_column", "print_colorized"] ARGS_LIST_HYPEROPTS = ["hyperopt_path", "print_one_column", "print_colorized"] -ARGS_BACKTEST_FILTER = [] +ARGS_BACKTEST_FILTER = ["exportfilename", "backtest_show_pair_list"] ARGS_LIST_EXCHANGES = ["print_one_column", "list_exchanges_all"] @@ -175,16 +175,15 @@ class Arguments: self.parser = argparse.ArgumentParser(description='Free, open source crypto trading bot') self._build_args(optionlist=['version'], parser=self.parser) - from freqtrade.commands import ( - start_backtesting, start_backtest_filter, start_convert_data, start_convert_trades, - start_create_userdir, start_download_data, start_edge, - start_hyperopt, start_hyperopt_list, start_hyperopt_show, - start_install_ui, start_list_data, start_list_exchanges, - start_list_markets, start_list_strategies, - start_list_timeframes, start_new_config, start_new_strategy, - start_plot_dataframe, start_plot_profit, start_show_trades, - start_test_pairlist, start_trading, start_webserver - ) + from freqtrade.commands import (start_backtest_filter, start_backtesting, + start_convert_data, start_convert_trades, + start_create_userdir, start_download_data, start_edge, + start_hyperopt, start_hyperopt_list, start_hyperopt_show, + start_install_ui, start_list_data, start_list_exchanges, + start_list_markets, start_list_strategies, + start_list_timeframes, start_new_config, start_new_strategy, + start_plot_dataframe, start_plot_profit, start_show_trades, + start_test_pairlist, start_trading, start_webserver) subparsers = self.parser.add_subparsers(dest='command', # Use custom message when no subhandler is added diff --git a/freqtrade/commands/cli_options.py b/freqtrade/commands/cli_options.py index 8d9b28c40..6aa4ed363 100644 --- a/freqtrade/commands/cli_options.py +++ b/freqtrade/commands/cli_options.py @@ -152,6 +152,12 @@ AVAILABLE_CLI_OPTIONS = { action='store_false', default=True, ), + "backtest_show_pair_list": Arg( + '--show-pair-list', + help='Show backtesting pairlist sorted by profit.', + action='store_true', + default=False, + ), "enable_protections": Arg( '--enable-protections', '--enableprotections', help='Enable protections for backtesting.' diff --git a/freqtrade/commands/optimize_commands.py b/freqtrade/commands/optimize_commands.py index 75392d897..0d1f304db 100644 --- a/freqtrade/commands/optimize_commands.py +++ b/freqtrade/commands/optimize_commands.py @@ -1,15 +1,13 @@ -from freqtrade.data.btanalysis import get_latest_backtest_filename -import pandas -from pandas.io import json -from freqtrade.optimize import backtesting import logging from typing import Any, Dict from freqtrade import constants from freqtrade.configuration import setup_utils_configuration +from freqtrade.data.btanalysis import load_backtest_stats from freqtrade.enums import RunMode from freqtrade.exceptions import OperationalException from freqtrade.misc import round_coin_value +from freqtrade.optimize.optimize_reports import show_backtest_results, show_filtered_pairlist logger = logging.getLogger(__name__) @@ -57,27 +55,19 @@ def start_backtesting(args: Dict[str, Any]) -> None: backtesting = Backtesting(config) backtesting.start() + def start_backtest_filter(args: Dict[str, Any]) -> None: """ - List backtest pairs previously filtered + Show previous backtest result """ config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE) - no_header = config.get('backtest_show_pair_list', False) - results_file = get_latest_backtest_filename( - config['user_data_dir'] / 'backtest_results/') - - logger.info("Using Backtesting result {results_file}") - - # load data using Python JSON module - with open(config['user_data_dir'] / 'backtest_results/' / results_file,'r') as f: - data = json.loads(f.read()) - strategy = list(data["strategy"])[0] - trades = data["strategy"][strategy] - - print(trades) + results = load_backtest_stats(config['exportfilename']) + # print(results) + show_backtest_results(config, results) + show_filtered_pairlist(config, results) logger.info("Backtest filtering complete. ") diff --git a/freqtrade/configuration/configuration.py b/freqtrade/configuration/configuration.py index 822577916..f5a674878 100644 --- a/freqtrade/configuration/configuration.py +++ b/freqtrade/configuration/configuration.py @@ -245,6 +245,10 @@ class Configuration: self._args_to_config(config, argname='timeframe_detail', logstring='Parameter --timeframe-detail detected, ' 'using {} for intra-candle backtesting ...') + + self._args_to_config(config, argname='backtest_show_pair_list', + logstring='Parameter --show-pair-list detected.') + self._args_to_config(config, argname='stake_amount', logstring='Parameter --stake-amount detected, ' 'overriding stake_amount to: {} ...') diff --git a/freqtrade/optimize/optimize_reports.py b/freqtrade/optimize/optimize_reports.py index 712cce028..5adad39c1 100644 --- a/freqtrade/optimize/optimize_reports.py +++ b/freqtrade/optimize/optimize_reports.py @@ -736,17 +736,12 @@ def show_backtest_results(config: Dict, backtest_stats: Dict): print('=' * len(table.splitlines()[0])) print('\nFor more details, please look at the detail tables above') -def show_backtest_results_filtered(config: Dict, backtest_stats: Dict): - stake_currency = config['stake_currency'] - for strategy, results in backtest_stats['strategy'].items(): - show_backtest_result(strategy, results, stake_currency) - - if len(backtest_stats['strategy']) > 1: - # Print Strategy summary table - - table = text_table_strategy(backtest_stats['strategy_comparison'], stake_currency) - print(' STRATEGY SUMMARY '.center(len(table.splitlines()[0]), '=')) - print(table) - print('=' * len(table.splitlines()[0])) - print('\nFor more details, please look at the detail tables above') +def show_filtered_pairlist(config: Dict, backtest_stats: Dict): + if config.get('backtest_show_pair_list', False): + for strategy, results in backtest_stats['strategy'].items(): + print("Pairs for Strategy: \n[") + for result in results['results_per_pair']: + if result["key"] != 'TOTAL': + print(f'"{result["key"]}", // {round(result["profit_mean_pct"], 2)}%') + print("]") From 851062ca46463139276544dbb9b8289ec705af86 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 30 Oct 2021 10:53:18 +0200 Subject: [PATCH 4/8] Rename backtest-filter to backtest_show --- freqtrade/commands/__init__.py | 2 +- freqtrade/commands/arguments.py | 16 ++++++++-------- freqtrade/commands/optimize_commands.py | 4 +--- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/freqtrade/commands/__init__.py b/freqtrade/commands/__init__.py index 89b6fdc3a..ba977b6bd 100644 --- a/freqtrade/commands/__init__.py +++ b/freqtrade/commands/__init__.py @@ -16,7 +16,7 @@ from freqtrade.commands.hyperopt_commands import start_hyperopt_list, start_hype from freqtrade.commands.list_commands import (start_list_exchanges, start_list_markets, start_list_strategies, start_list_timeframes, start_show_trades) -from freqtrade.commands.optimize_commands import (start_backtest_filter, start_backtesting, +from freqtrade.commands.optimize_commands import (start_backtest_show, start_backtesting, start_edge, start_hyperopt) from freqtrade.commands.pairlist_commands import start_test_pairlist from freqtrade.commands.plot_commands import start_plot_dataframe, start_plot_profit diff --git a/freqtrade/commands/arguments.py b/freqtrade/commands/arguments.py index 8f68e6521..be4f9188f 100644 --- a/freqtrade/commands/arguments.py +++ b/freqtrade/commands/arguments.py @@ -41,7 +41,7 @@ ARGS_LIST_STRATEGIES = ["strategy_path", "print_one_column", "print_colorized"] ARGS_LIST_HYPEROPTS = ["hyperopt_path", "print_one_column", "print_colorized"] -ARGS_BACKTEST_FILTER = ["exportfilename", "backtest_show_pair_list"] +ARGS_BACKTEST_SHOW = ["exportfilename", "backtest_show_pair_list"] ARGS_LIST_EXCHANGES = ["print_one_column", "list_exchanges_all"] @@ -175,7 +175,7 @@ class Arguments: self.parser = argparse.ArgumentParser(description='Free, open source crypto trading bot') self._build_args(optionlist=['version'], parser=self.parser) - from freqtrade.commands import (start_backtest_filter, start_backtesting, + from freqtrade.commands import (start_backtest_show, start_backtesting, start_convert_data, start_convert_trades, start_create_userdir, start_download_data, start_edge, start_hyperopt, start_hyperopt_list, start_hyperopt_show, @@ -267,14 +267,14 @@ class Arguments: backtesting_cmd.set_defaults(func=start_backtesting) self._build_args(optionlist=ARGS_BACKTEST, parser=backtesting_cmd) - # Add backtest-filter subcommand - backtest_filter_cmd = subparsers.add_parser( - 'backtest-filter', - help='Filter Backtest results', + # Add backtest-show subcommand + backtest_show_cmd = subparsers.add_parser( + 'backtest-show', + help='Show past Backtest results', parents=[_common_parser], ) - backtest_filter_cmd.set_defaults(func=start_backtest_filter) - self._build_args(optionlist=ARGS_BACKTEST_FILTER, parser=backtest_filter_cmd) + backtest_show_cmd.set_defaults(func=start_backtest_show) + self._build_args(optionlist=ARGS_BACKTEST_SHOW, parser=backtest_show_cmd) # Add edge subcommand edge_cmd = subparsers.add_parser('edge', help='Edge module.', diff --git a/freqtrade/commands/optimize_commands.py b/freqtrade/commands/optimize_commands.py index 0d1f304db..c35e78153 100644 --- a/freqtrade/commands/optimize_commands.py +++ b/freqtrade/commands/optimize_commands.py @@ -56,7 +56,7 @@ def start_backtesting(args: Dict[str, Any]) -> None: backtesting.start() -def start_backtest_filter(args: Dict[str, Any]) -> None: +def start_backtest_show(args: Dict[str, Any]) -> None: """ Show previous backtest result """ @@ -69,8 +69,6 @@ def start_backtest_filter(args: Dict[str, Any]) -> None: show_backtest_results(config, results) show_filtered_pairlist(config, results) - logger.info("Backtest filtering complete. ") - def start_hyperopt(args: Dict[str, Any]) -> None: """ From 650d6c276a3bd54686217fbcc48b7f34f58f13b8 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 30 Oct 2021 10:58:03 +0200 Subject: [PATCH 5/8] Add documentation --- docs/utils.md | 40 +++++++++++++++++++++++++++++++++ freqtrade/commands/arguments.py | 18 +++++++-------- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/docs/utils.md b/docs/utils.md index a65ba5db4..6934e0a5c 100644 --- a/docs/utils.md +++ b/docs/utils.md @@ -577,6 +577,46 @@ Common arguments: ``` +## Show previous Backtest results + +Allows you to show previous backtest results. +Adding `--show-pair-list` outputs a sorted pair list you can easily copy/paste into your configuration (omitting bad pairs). + +??? Warning "Strategy overfitting" + Only using winning pairs can lead to an overfitted strategy, which will not work well on future data. Make sure to extensively test your strategy in dry-run before risking real money. + +``` +usage: freqtrade backtest-show [-h] [-v] [--logfile FILE] [-V] [-c PATH] + [-d PATH] [--userdir PATH] + [--export-filename PATH] [--show-pair-list] + +optional arguments: + -h, --help show this help message and exit + --export-filename PATH + Save backtest results to the file with this filename. + Requires `--export` to be set as well. Example: + `--export-filename=user_data/backtest_results/backtest + _today.json` + --show-pair-list Show backtesting pairlist sorted by profit. + +Common arguments: + -v, --verbose Verbose mode (-vv for more, -vvv to get all messages). + --logfile FILE Log to the file specified. Special values are: + 'syslog', 'journald'. See the documentation for more + details. + -V, --version show program's version number and exit + -c PATH, --config PATH + Specify configuration file (default: + `userdir/config.json` or `config.json` whichever + exists). Multiple --config options may be used. Can be + set to `-` to read config from stdin. + -d PATH, --datadir PATH + Path to directory with historical backtesting data. + --userdir PATH, --user-data-dir PATH + Path to userdata directory. + +``` + ## List Hyperopt results You can list the hyperoptimization epochs the Hyperopt module evaluated previously with the `hyperopt-list` sub-command. diff --git a/freqtrade/commands/arguments.py b/freqtrade/commands/arguments.py index be4f9188f..9d14bb38d 100644 --- a/freqtrade/commands/arguments.py +++ b/freqtrade/commands/arguments.py @@ -175,15 +175,15 @@ class Arguments: self.parser = argparse.ArgumentParser(description='Free, open source crypto trading bot') self._build_args(optionlist=['version'], parser=self.parser) - from freqtrade.commands import (start_backtest_show, start_backtesting, - start_convert_data, start_convert_trades, - start_create_userdir, start_download_data, start_edge, - start_hyperopt, start_hyperopt_list, start_hyperopt_show, - start_install_ui, start_list_data, start_list_exchanges, - start_list_markets, start_list_strategies, - start_list_timeframes, start_new_config, start_new_strategy, - start_plot_dataframe, start_plot_profit, start_show_trades, - start_test_pairlist, start_trading, start_webserver) + from freqtrade.commands import (start_backtest_show, start_backtesting, start_convert_data, + start_convert_trades, start_create_userdir, + start_download_data, start_edge, start_hyperopt, + start_hyperopt_list, start_hyperopt_show, start_install_ui, + start_list_data, start_list_exchanges, start_list_markets, + start_list_strategies, start_list_timeframes, + start_new_config, start_new_strategy, start_plot_dataframe, + start_plot_profit, start_show_trades, start_test_pairlist, + start_trading, start_webserver) subparsers = self.parser.add_subparsers(dest='command', # Use custom message when no subhandler is added From 72ecb45d868dbf2dbcb10a2a2597a795ae451436 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 30 Oct 2021 16:53:48 +0200 Subject: [PATCH 6/8] Add test for backtest_show logic --- freqtrade/optimize/optimize_reports.py | 2 +- tests/optimize/test_optimize_reports.py | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/freqtrade/optimize/optimize_reports.py b/freqtrade/optimize/optimize_reports.py index 5adad39c1..d6fcf8a04 100644 --- a/freqtrade/optimize/optimize_reports.py +++ b/freqtrade/optimize/optimize_reports.py @@ -740,7 +740,7 @@ def show_backtest_results(config: Dict, backtest_stats: Dict): def show_filtered_pairlist(config: Dict, backtest_stats: Dict): if config.get('backtest_show_pair_list', False): for strategy, results in backtest_stats['strategy'].items(): - print("Pairs for Strategy: \n[") + print(f"Pairs for Strategy {strategy}: \n[") for result in results['results_per_pair']: if result["key"] != 'TOTAL': print(f'"{result["key"]}", // {round(result["profit_mean_pct"], 2)}%') diff --git a/tests/optimize/test_optimize_reports.py b/tests/optimize/test_optimize_reports.py index b5eb09923..7f5f5e8a9 100644 --- a/tests/optimize/test_optimize_reports.py +++ b/tests/optimize/test_optimize_reports.py @@ -10,7 +10,7 @@ from arrow import Arrow from freqtrade.configuration import TimeRange from freqtrade.constants import DATETIME_PRINT_FORMAT, LAST_BT_RESULT_FN from freqtrade.data import history -from freqtrade.data.btanalysis import get_latest_backtest_filename, load_backtest_data +from freqtrade.data.btanalysis import get_latest_backtest_filename, load_backtest_data, load_backtest_stats from freqtrade.edge import PairInfo from freqtrade.enums import SellType from freqtrade.optimize.optimize_reports import (_get_resample_from_period, generate_backtest_stats, @@ -19,7 +19,7 @@ from freqtrade.optimize.optimize_reports import (_get_resample_from_period, gene generate_periodic_breakdown_stats, generate_sell_reason_stats, generate_strategy_comparison, - generate_trading_stats, store_backtest_stats, + generate_trading_stats, show_filtered_pairlist, store_backtest_stats, text_table_bt_results, text_table_sell_reason, text_table_strategy) from freqtrade.resolvers.strategy_resolver import StrategyResolver @@ -407,3 +407,16 @@ def test__get_resample_from_period(): assert _get_resample_from_period('month') == '1M' with pytest.raises(ValueError, match=r"Period noooo is not supported."): _get_resample_from_period('noooo') + + +def test_show_filtered_pairlist(testdatadir, default_conf, capsys): + filename = testdatadir / "backtest-result_new.json" + bt_data = load_backtest_stats(filename) + default_conf['backtest_show_pair_list'] = True + + show_filtered_pairlist(default_conf, bt_data) + + out, err = capsys.readouterr() + assert 'Pairs for Strategy StrategyTestV2: \n[' in out + assert 'TOTAL' not in out + assert '"ETH/BTC", // ' in out From 20904f1ca49d3d59d65eaf35b8699d1a33c025ee Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 30 Oct 2021 17:05:12 +0200 Subject: [PATCH 7/8] Add tests for new command --- freqtrade/commands/optimize_commands.py | 8 +++---- freqtrade/optimize/optimize_reports.py | 2 +- tests/commands/test_commands.py | 28 +++++++++++++++++++------ tests/optimize/test_optimize_reports.py | 13 ++++++------ 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/freqtrade/commands/optimize_commands.py b/freqtrade/commands/optimize_commands.py index c35e78153..4acc0d939 100644 --- a/freqtrade/commands/optimize_commands.py +++ b/freqtrade/commands/optimize_commands.py @@ -3,11 +3,9 @@ from typing import Any, Dict from freqtrade import constants from freqtrade.configuration import setup_utils_configuration -from freqtrade.data.btanalysis import load_backtest_stats from freqtrade.enums import RunMode from freqtrade.exceptions import OperationalException from freqtrade.misc import round_coin_value -from freqtrade.optimize.optimize_reports import show_backtest_results, show_filtered_pairlist logger = logging.getLogger(__name__) @@ -63,11 +61,13 @@ def start_backtest_show(args: Dict[str, Any]) -> None: config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE) + from freqtrade.optimize.optimize_reports import show_backtest_results, show_sorted_pairlist + from freqtrade.data.btanalysis import load_backtest_stats + results = load_backtest_stats(config['exportfilename']) - # print(results) show_backtest_results(config, results) - show_filtered_pairlist(config, results) + show_sorted_pairlist(config, results) def start_hyperopt(args: Dict[str, Any]) -> None: diff --git a/freqtrade/optimize/optimize_reports.py b/freqtrade/optimize/optimize_reports.py index d6fcf8a04..09de655ef 100644 --- a/freqtrade/optimize/optimize_reports.py +++ b/freqtrade/optimize/optimize_reports.py @@ -737,7 +737,7 @@ def show_backtest_results(config: Dict, backtest_stats: Dict): print('\nFor more details, please look at the detail tables above') -def show_filtered_pairlist(config: Dict, backtest_stats: Dict): +def show_sorted_pairlist(config: Dict, backtest_stats: Dict): if config.get('backtest_show_pair_list', False): for strategy, results in backtest_stats['strategy'].items(): print(f"Pairs for Strategy {strategy}: \n[") diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index 6e717afdf..fcccca539 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -8,12 +8,12 @@ from zipfile import ZipFile import arrow import pytest -from freqtrade.commands import (start_convert_data, start_convert_trades, start_create_userdir, - start_download_data, start_hyperopt_list, start_hyperopt_show, - start_install_ui, start_list_data, start_list_exchanges, - start_list_markets, start_list_strategies, start_list_timeframes, - start_new_strategy, start_show_trades, start_test_pairlist, - start_trading, start_webserver) +from freqtrade.commands import (start_backtest_show, start_convert_data, start_convert_trades, + start_create_userdir, start_download_data, start_hyperopt_list, + start_hyperopt_show, start_install_ui, start_list_data, + start_list_exchanges, start_list_markets, start_list_strategies, + start_list_timeframes, start_new_strategy, start_show_trades, + start_test_pairlist, start_trading, start_webserver) from freqtrade.commands.deploy_commands import (clean_ui_subdir, download_and_install_ui, get_ui_download_url, read_ui_version) from freqtrade.configuration import setup_utils_configuration @@ -1389,3 +1389,19 @@ def test_show_trades(mocker, fee, capsys, caplog): with pytest.raises(OperationalException, match=r"--db-url is required for this command."): start_show_trades(pargs) + + +def test_backtest_show(mocker, testdatadir, capsys): + sbr = mocker.patch('freqtrade.optimize.optimize_reports.show_backtest_results') + args = [ + "backtest-show", + "--export-filename", + f"{testdatadir / 'backtest-result_new.json'}", + "--show-pair-list" + ] + pargs = get_args(args) + pargs['config'] = None + start_backtest_show(pargs) + assert sbr.call_count == 1 + out, err = capsys.readouterr() + assert "Pairs for Strategy" in out diff --git a/tests/optimize/test_optimize_reports.py b/tests/optimize/test_optimize_reports.py index 7f5f5e8a9..e56572522 100644 --- a/tests/optimize/test_optimize_reports.py +++ b/tests/optimize/test_optimize_reports.py @@ -10,7 +10,8 @@ from arrow import Arrow from freqtrade.configuration import TimeRange from freqtrade.constants import DATETIME_PRINT_FORMAT, LAST_BT_RESULT_FN from freqtrade.data import history -from freqtrade.data.btanalysis import get_latest_backtest_filename, load_backtest_data, load_backtest_stats +from freqtrade.data.btanalysis import (get_latest_backtest_filename, load_backtest_data, + load_backtest_stats) from freqtrade.edge import PairInfo from freqtrade.enums import SellType from freqtrade.optimize.optimize_reports import (_get_resample_from_period, generate_backtest_stats, @@ -19,9 +20,9 @@ from freqtrade.optimize.optimize_reports import (_get_resample_from_period, gene generate_periodic_breakdown_stats, generate_sell_reason_stats, generate_strategy_comparison, - generate_trading_stats, show_filtered_pairlist, store_backtest_stats, - text_table_bt_results, text_table_sell_reason, - text_table_strategy) + generate_trading_stats, show_sorted_pairlist, + store_backtest_stats, text_table_bt_results, + text_table_sell_reason, text_table_strategy) from freqtrade.resolvers.strategy_resolver import StrategyResolver from tests.data.test_history import _backup_file, _clean_test_file @@ -409,12 +410,12 @@ def test__get_resample_from_period(): _get_resample_from_period('noooo') -def test_show_filtered_pairlist(testdatadir, default_conf, capsys): +def test_show_sorted_pairlist(testdatadir, default_conf, capsys): filename = testdatadir / "backtest-result_new.json" bt_data = load_backtest_stats(filename) default_conf['backtest_show_pair_list'] = True - show_filtered_pairlist(default_conf, bt_data) + show_sorted_pairlist(default_conf, bt_data) out, err = capsys.readouterr() assert 'Pairs for Strategy StrategyTestV2: \n[' in out From c15f73aa1f35aaea7db1d701ed6d745fd80ecbb6 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 31 Oct 2021 09:55:19 +0100 Subject: [PATCH 8/8] Rename command to backtesting-show --- docs/utils.md | 6 +++--- freqtrade/commands/__init__.py | 2 +- freqtrade/commands/arguments.py | 28 ++++++++++++------------- freqtrade/commands/optimize_commands.py | 4 ++-- tests/commands/test_commands.py | 8 +++---- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/docs/utils.md b/docs/utils.md index 6934e0a5c..4a032db26 100644 --- a/docs/utils.md +++ b/docs/utils.md @@ -586,9 +586,9 @@ Adding `--show-pair-list` outputs a sorted pair list you can easily copy/paste i Only using winning pairs can lead to an overfitted strategy, which will not work well on future data. Make sure to extensively test your strategy in dry-run before risking real money. ``` -usage: freqtrade backtest-show [-h] [-v] [--logfile FILE] [-V] [-c PATH] - [-d PATH] [--userdir PATH] - [--export-filename PATH] [--show-pair-list] +usage: freqtrade backtesting-show [-h] [-v] [--logfile FILE] [-V] [-c PATH] + [-d PATH] [--userdir PATH] + [--export-filename PATH] [--show-pair-list] optional arguments: -h, --help show this help message and exit diff --git a/freqtrade/commands/__init__.py b/freqtrade/commands/__init__.py index ba977b6bd..129836000 100644 --- a/freqtrade/commands/__init__.py +++ b/freqtrade/commands/__init__.py @@ -16,7 +16,7 @@ from freqtrade.commands.hyperopt_commands import start_hyperopt_list, start_hype from freqtrade.commands.list_commands import (start_list_exchanges, start_list_markets, start_list_strategies, start_list_timeframes, start_show_trades) -from freqtrade.commands.optimize_commands import (start_backtest_show, start_backtesting, +from freqtrade.commands.optimize_commands import (start_backtesting, start_backtesting_show, start_edge, start_hyperopt) from freqtrade.commands.pairlist_commands import start_test_pairlist from freqtrade.commands.plot_commands import start_plot_dataframe, start_plot_profit diff --git a/freqtrade/commands/arguments.py b/freqtrade/commands/arguments.py index 9d14bb38d..032f7dd51 100644 --- a/freqtrade/commands/arguments.py +++ b/freqtrade/commands/arguments.py @@ -175,15 +175,15 @@ class Arguments: self.parser = argparse.ArgumentParser(description='Free, open source crypto trading bot') self._build_args(optionlist=['version'], parser=self.parser) - from freqtrade.commands import (start_backtest_show, start_backtesting, start_convert_data, - start_convert_trades, start_create_userdir, - start_download_data, start_edge, start_hyperopt, - start_hyperopt_list, start_hyperopt_show, start_install_ui, - start_list_data, start_list_exchanges, start_list_markets, - start_list_strategies, start_list_timeframes, - start_new_config, start_new_strategy, start_plot_dataframe, - start_plot_profit, start_show_trades, start_test_pairlist, - start_trading, start_webserver) + from freqtrade.commands import (start_backtesting, start_backtesting_show, + start_convert_data, start_convert_trades, + start_create_userdir, start_download_data, start_edge, + start_hyperopt, start_hyperopt_list, start_hyperopt_show, + start_install_ui, start_list_data, start_list_exchanges, + start_list_markets, start_list_strategies, + start_list_timeframes, start_new_config, start_new_strategy, + start_plot_dataframe, start_plot_profit, start_show_trades, + start_test_pairlist, start_trading, start_webserver) subparsers = self.parser.add_subparsers(dest='command', # Use custom message when no subhandler is added @@ -267,14 +267,14 @@ class Arguments: backtesting_cmd.set_defaults(func=start_backtesting) self._build_args(optionlist=ARGS_BACKTEST, parser=backtesting_cmd) - # Add backtest-show subcommand - backtest_show_cmd = subparsers.add_parser( - 'backtest-show', + # Add backtesting-show subcommand + backtesting_show_cmd = subparsers.add_parser( + 'backtesting-show', help='Show past Backtest results', parents=[_common_parser], ) - backtest_show_cmd.set_defaults(func=start_backtest_show) - self._build_args(optionlist=ARGS_BACKTEST_SHOW, parser=backtest_show_cmd) + backtesting_show_cmd.set_defaults(func=start_backtesting_show) + self._build_args(optionlist=ARGS_BACKTEST_SHOW, parser=backtesting_show_cmd) # Add edge subcommand edge_cmd = subparsers.add_parser('edge', help='Edge module.', diff --git a/freqtrade/commands/optimize_commands.py b/freqtrade/commands/optimize_commands.py index 4acc0d939..f230b696c 100644 --- a/freqtrade/commands/optimize_commands.py +++ b/freqtrade/commands/optimize_commands.py @@ -54,15 +54,15 @@ def start_backtesting(args: Dict[str, Any]) -> None: backtesting.start() -def start_backtest_show(args: Dict[str, Any]) -> None: +def start_backtesting_show(args: Dict[str, Any]) -> None: """ Show previous backtest result """ config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE) - from freqtrade.optimize.optimize_reports import show_backtest_results, show_sorted_pairlist from freqtrade.data.btanalysis import load_backtest_stats + from freqtrade.optimize.optimize_reports import show_backtest_results, show_sorted_pairlist results = load_backtest_stats(config['exportfilename']) diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index fcccca539..e0d0cc38d 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -8,7 +8,7 @@ from zipfile import ZipFile import arrow import pytest -from freqtrade.commands import (start_backtest_show, start_convert_data, start_convert_trades, +from freqtrade.commands import (start_backtesting_show, start_convert_data, start_convert_trades, start_create_userdir, start_download_data, start_hyperopt_list, start_hyperopt_show, start_install_ui, start_list_data, start_list_exchanges, start_list_markets, start_list_strategies, @@ -1391,17 +1391,17 @@ def test_show_trades(mocker, fee, capsys, caplog): start_show_trades(pargs) -def test_backtest_show(mocker, testdatadir, capsys): +def test_backtesting_show(mocker, testdatadir, capsys): sbr = mocker.patch('freqtrade.optimize.optimize_reports.show_backtest_results') args = [ - "backtest-show", + "backtesting-show", "--export-filename", f"{testdatadir / 'backtest-result_new.json'}", "--show-pair-list" ] pargs = get_args(args) pargs['config'] = None - start_backtest_show(pargs) + start_backtesting_show(pargs) assert sbr.call_count == 1 out, err = capsys.readouterr() assert "Pairs for Strategy" in out