From 2bed41da5dcc899b45c0d984b81b44cfc5094340 Mon Sep 17 00:00:00 2001 From: rextea Date: Fri, 26 Mar 2021 18:40:50 +0300 Subject: [PATCH 1/9] Add days breakdown table to backtesting --- docs/backtesting.md | 1 + freqtrade/commands/arguments.py | 2 +- freqtrade/commands/cli_options.py | 6 ++ freqtrade/configuration/configuration.py | 3 + freqtrade/optimize/optimize_reports.py | 71 +++++++++++++++++++++--- 5 files changed, 74 insertions(+), 9 deletions(-) diff --git a/docs/backtesting.md b/docs/backtesting.md index d02c59f05..91faa07bb 100644 --- a/docs/backtesting.md +++ b/docs/backtesting.md @@ -67,6 +67,7 @@ optional arguments: Requires `--export` to be set as well. Example: `--export-filename=user_data/backtest_results/backtest _today.json` + --show-days Print a days breakdown table of the backtest results Common arguments: -v, --verbose Verbose mode (-vv for more, -vvv to get all messages). diff --git a/freqtrade/commands/arguments.py b/freqtrade/commands/arguments.py index 9468a7f7d..b71819ef2 100644 --- a/freqtrade/commands/arguments.py +++ b/freqtrade/commands/arguments.py @@ -21,7 +21,7 @@ ARGS_COMMON_OPTIMIZE = ["timeframe", "timerange", "dataformat_ohlcv", ARGS_BACKTEST = ARGS_COMMON_OPTIMIZE + ["position_stacking", "use_max_market_positions", "enable_protections", "dry_run_wallet", - "strategy_list", "export", "exportfilename"] + "strategy_list", "export", "exportfilename", "show_days"] ARGS_HYPEROPT = ARGS_COMMON_OPTIMIZE + ["hyperopt", "hyperopt_path", "position_stacking", "use_max_market_positions", diff --git a/freqtrade/commands/cli_options.py b/freqtrade/commands/cli_options.py index 15c13cec9..dc193ee4f 100644 --- a/freqtrade/commands/cli_options.py +++ b/freqtrade/commands/cli_options.py @@ -183,6 +183,12 @@ AVAILABLE_CLI_OPTIONS = { type=float, metavar='FLOAT', ), + "show_days": Arg( + '--show-days', + help='Print days breakdown for backtest results', + action='store_true', + default=False, + ), # Edge "stoploss_range": Arg( '--stoplosses', diff --git a/freqtrade/configuration/configuration.py b/freqtrade/configuration/configuration.py index a40a4fd83..1eb6351d0 100644 --- a/freqtrade/configuration/configuration.py +++ b/freqtrade/configuration/configuration.py @@ -260,6 +260,9 @@ class Configuration: self._args_to_config(config, argname='export', logstring='Parameter --export detected: {} ...') + self._args_to_config(config, argname='show_days', + logstring='Parameter --show-days detected ...') + # Edge section: if 'stoploss_range' in self.args and self.args["stoploss_range"]: txt_range = eval(self.args["stoploss_range"]) diff --git a/freqtrade/optimize/optimize_reports.py b/freqtrade/optimize/optimize_reports.py index 099976aa9..d15988669 100644 --- a/freqtrade/optimize/optimize_reports.py +++ b/freqtrade/optimize/optimize_reports.py @@ -13,7 +13,6 @@ from freqtrade.data.btanalysis import (calculate_csum, calculate_market_change, calculate_max_drawdown) from freqtrade.misc import decimals_per_coin, file_dump_json, round_coin_value - logger = logging.getLogger(__name__) @@ -32,7 +31,7 @@ def store_backtest_stats(recordfilename: Path, stats: Dict[str, DataFrame]) -> N filename = Path.joinpath( recordfilename.parent, f'{recordfilename.stem}-{datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}' - ).with_suffix(recordfilename.suffix) + ).with_suffix(recordfilename.suffix) file_dump_json(filename, stats) latest_filename = Path.joinpath(filename.parent, LAST_BT_RESULT_FN) @@ -75,8 +74,8 @@ def _generate_result_line(result: DataFrame, starting_balance: int, first_column 'profit_total': profit_total, 'profit_total_pct': round(profit_total * 100.0, 2), 'duration_avg': str(timedelta( - minutes=round(result['trade_duration'].mean())) - ) if not result.empty else '0:00', + minutes=round(result['trade_duration'].mean())) + ) if not result.empty else '0:00', # 'duration_max': str(timedelta( # minutes=round(result['trade_duration'].max())) # ) if not result.empty else '0:00', @@ -161,12 +160,11 @@ def generate_strategy_metrics(all_results: Dict) -> List[Dict]: for strategy, results in all_results.items(): tabular_data.append(_generate_result_line( results['results'], results['config']['dry_run_wallet'], strategy) - ) + ) return tabular_data def generate_edge_table(results: dict) -> str: - floatfmt = ('s', '.10g', '.2f', '.2f', '.2f', '.2f', 'd', 'd', 'd') tabular_data = [] headers = ['Pair', 'Stoploss', 'Win Rate', 'Risk Reward Ratio', @@ -191,6 +189,29 @@ def generate_edge_table(results: dict) -> str: floatfmt=floatfmt, tablefmt="orgtbl", stralign="right") # type: ignore +def generate_days_breakdown_stats(results: DataFrame, starting_balance: int) -> Dict[str, Any]: + days = results.resample('1d', on='close_date') + days_stats = [] + for name, day in days: + profit_abs = day['profit_abs'].sum().round(10) + profit_total = day['profit_abs'].sum() / starting_balance + wins = sum(day['profit_abs'] > 0) + draws = sum(day['profit_abs'] == 0) + loses = sum(day['profit_abs'] < 0) + profit_percentage = round(profit_total * 100.0, 2) + days_stats.append( + { + 'date': name.strftime('%d/%m/%Y'), + 'profit_percentage': profit_percentage, + 'profit_abs': profit_abs, + 'wins': wins, + 'draws': draws, + 'loses': loses + } + ) + return days_stats + + def generate_daily_stats(results: DataFrame) -> Dict[str, Any]: if len(results) == 0: return { @@ -266,6 +287,8 @@ def generate_backtest_stats(btdata: Dict[str, DataFrame], starting_balance=starting_balance, results=results.loc[results['is_open']], skip_nan=True) + days_breakdown_stats = generate_days_breakdown_stats(results=results, + starting_balance=starting_balance) daily_stats = generate_daily_stats(results) best_pair = max([pair for pair in pair_results if pair['key'] != 'TOTAL'], key=lambda x: x['profit_sum']) if len(pair_results) > 1 else None @@ -283,6 +306,7 @@ def generate_backtest_stats(btdata: Dict[str, DataFrame], 'results_per_pair': pair_results, 'sell_reason_summary': sell_reason_stats, 'left_open_trades': left_open_results, + 'days_breakdown_stats': days_breakdown_stats, 'total_trades': len(results), 'total_volume': float(results['stake_amount'].sum()), 'avg_stake_amount': results['stake_amount'].mean() if len(results) > 0 else 0, @@ -425,6 +449,28 @@ def text_table_sell_reason(sell_reason_stats: List[Dict[str, Any]], stake_curren return tabulate(output, headers=headers, tablefmt="orgtbl", stralign="right") +def text_table_days_breakdown(days_breakdown_stats: List[Dict[str, Any]], stake_currency: str) -> str: + """ + Generate small table with Backtest results by days + :param days_breakdown_stats: Days breakdown metrics + :param stake_currency: Stakecurrency used + :return: pretty printed table with tabulate as string + """ + headers = [ + 'Day', + 'Profit %', + f'Tot Profit {stake_currency}', + 'Wins', + 'Draws', + 'Losses', + ] + output = [[ + d['date'], d['profit_percentage'], round_coin_value(d['profit_abs'], stake_currency, False), + d['wins'], d['draws'], d['loses'], + ] for d in days_breakdown_stats] + return tabulate(output, headers=headers, tablefmt="orgtbl", stralign="right") + + def text_table_strategy(strategy_results, stake_currency: str) -> str: """ Generate summary table per strategy @@ -463,6 +509,8 @@ def text_table_add_metrics(strat_results: Dict) -> str: strat_results['stake_currency'])), ('Total profit %', f"{round(strat_results['profit_total'] * 100, 2)}%"), ('Trades per day', strat_results['trades_per_day']), + ('Avg. daily profit %', + f"{round(strat_results['profit_total'] / strat_results['backtest_days'] * 100, 2)}%"), ('Avg. stake amount', round_coin_value(strat_results['avg_stake_amount'], strat_results['stake_currency'])), ('Total trade volume', round_coin_value(strat_results['total_volume'], @@ -482,7 +530,7 @@ def text_table_add_metrics(strat_results: Dict) -> str: ('Worst day', round_coin_value(strat_results['backtest_worst_day_abs'], strat_results['stake_currency'])), ('Days win/draw/lose', f"{strat_results['winning_days']} / " - f"{strat_results['draw_days']} / {strat_results['losing_days']}"), + f"{strat_results['draw_days']} / {strat_results['losing_days']}"), ('Avg. Duration Winners', f"{strat_results['winner_holding_avg']}"), ('Avg. Duration Loser', f"{strat_results['loser_holding_avg']}"), ('', ''), # Empty line to improve readability @@ -510,7 +558,7 @@ def text_table_add_metrics(strat_results: Dict) -> str: strat_results['stake_currency']) stake_amount = round_coin_value( strat_results['stake_amount'], strat_results['stake_currency'] - ) if strat_results['stake_amount'] != UNLIMITED_STAKE_AMOUNT else 'unlimited' + ) if strat_results['stake_amount'] != UNLIMITED_STAKE_AMOUNT else 'unlimited' message = ("No trades made. " f"Your starting balance was {start_balance}, " @@ -542,6 +590,13 @@ def show_backtest_results(config: Dict, backtest_stats: Dict): print(' LEFT OPEN TRADES REPORT '.center(len(table.splitlines()[0]), '=')) print(table) + if config.get('show_days', False): + table = text_table_days_breakdown(days_breakdown_stats=results['days_breakdown_stats'], + stake_currency=stake_currency) + if isinstance(table, str) and len(table) > 0: + print(' DAYS BREAKDOWN '.center(len(table.splitlines()[0]), '=')) + print(table) + table = text_table_add_metrics(results) if isinstance(table, str) and len(table) > 0: print(' SUMMARY METRICS '.center(len(table.splitlines()[0]), '=')) From 76a02ff70aa4016ed6755fa1f00cbb6246edab97 Mon Sep 17 00:00:00 2001 From: rextea Date: Fri, 26 Mar 2021 18:49:17 +0300 Subject: [PATCH 2/9] fix indentations --- freqtrade/optimize/optimize_reports.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/freqtrade/optimize/optimize_reports.py b/freqtrade/optimize/optimize_reports.py index d15988669..286fa5c46 100644 --- a/freqtrade/optimize/optimize_reports.py +++ b/freqtrade/optimize/optimize_reports.py @@ -13,6 +13,7 @@ from freqtrade.data.btanalysis import (calculate_csum, calculate_market_change, calculate_max_drawdown) from freqtrade.misc import decimals_per_coin, file_dump_json, round_coin_value + logger = logging.getLogger(__name__) @@ -31,7 +32,7 @@ def store_backtest_stats(recordfilename: Path, stats: Dict[str, DataFrame]) -> N filename = Path.joinpath( recordfilename.parent, f'{recordfilename.stem}-{datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}' - ).with_suffix(recordfilename.suffix) + ).with_suffix(recordfilename.suffix) file_dump_json(filename, stats) latest_filename = Path.joinpath(filename.parent, LAST_BT_RESULT_FN) @@ -74,8 +75,8 @@ def _generate_result_line(result: DataFrame, starting_balance: int, first_column 'profit_total': profit_total, 'profit_total_pct': round(profit_total * 100.0, 2), 'duration_avg': str(timedelta( - minutes=round(result['trade_duration'].mean())) - ) if not result.empty else '0:00', + minutes=round(result['trade_duration'].mean())) + ) if not result.empty else '0:00', # 'duration_max': str(timedelta( # minutes=round(result['trade_duration'].max())) # ) if not result.empty else '0:00', @@ -530,7 +531,7 @@ def text_table_add_metrics(strat_results: Dict) -> str: ('Worst day', round_coin_value(strat_results['backtest_worst_day_abs'], strat_results['stake_currency'])), ('Days win/draw/lose', f"{strat_results['winning_days']} / " - f"{strat_results['draw_days']} / {strat_results['losing_days']}"), + f"{strat_results['draw_days']} / {strat_results['losing_days']}"), ('Avg. Duration Winners', f"{strat_results['winner_holding_avg']}"), ('Avg. Duration Loser', f"{strat_results['loser_holding_avg']}"), ('', ''), # Empty line to improve readability @@ -558,7 +559,7 @@ def text_table_add_metrics(strat_results: Dict) -> str: strat_results['stake_currency']) stake_amount = round_coin_value( strat_results['stake_amount'], strat_results['stake_currency'] - ) if strat_results['stake_amount'] != UNLIMITED_STAKE_AMOUNT else 'unlimited' + ) if strat_results['stake_amount'] != UNLIMITED_STAKE_AMOUNT else 'unlimited' message = ("No trades made. " f"Your starting balance was {start_balance}, " From 7d8cd736b8113904c427ccba13b2a5113e959be3 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 17 Oct 2021 16:48:31 +0200 Subject: [PATCH 3/9] Support days-breakdown also for hyperopt results --- freqtrade/commands/arguments.py | 2 +- freqtrade/commands/hyperopt_commands.py | 2 +- freqtrade/optimize/optimize_reports.py | 14 ++++++++------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/freqtrade/commands/arguments.py b/freqtrade/commands/arguments.py index 00aa0ded0..11c1e9191 100644 --- a/freqtrade/commands/arguments.py +++ b/freqtrade/commands/arguments.py @@ -89,7 +89,7 @@ ARGS_HYPEROPT_LIST = ["hyperopt_list_best", "hyperopt_list_profitable", ARGS_HYPEROPT_SHOW = ["hyperopt_list_best", "hyperopt_list_profitable", "hyperopt_show_index", "print_json", "hyperoptexportfilename", "hyperopt_show_no_header", - "disableparamexport"] + "disableparamexport", "show_days"] NO_CONF_REQURIED = ["convert-data", "convert-trade-data", "download-data", "list-timeframes", "list-markets", "list-pairs", "list-strategies", "list-data", diff --git a/freqtrade/commands/hyperopt_commands.py b/freqtrade/commands/hyperopt_commands.py index 614c4b3f5..d2f8c188c 100755 --- a/freqtrade/commands/hyperopt_commands.py +++ b/freqtrade/commands/hyperopt_commands.py @@ -96,7 +96,7 @@ def start_hyperopt_show(args: Dict[str, Any]) -> None: if 'strategy_name' in metrics: strategy_name = metrics['strategy_name'] show_backtest_result(strategy_name, metrics, - metrics['stake_currency']) + metrics['stake_currency'], config.get('show_days', False)) HyperoptTools.try_export_params(config, strategy_name, val) diff --git a/freqtrade/optimize/optimize_reports.py b/freqtrade/optimize/optimize_reports.py index 384ca006b..a6eedc6c7 100644 --- a/freqtrade/optimize/optimize_reports.py +++ b/freqtrade/optimize/optimize_reports.py @@ -4,7 +4,7 @@ from pathlib import Path from typing import Any, Dict, List, Union from numpy import int64 -from pandas import DataFrame +from pandas import DataFrame, to_datetime from tabulate import tabulate from freqtrade.constants import DATETIME_PRINT_FORMAT, LAST_BT_RESULT_FN, UNLIMITED_STAKE_AMOUNT @@ -213,7 +213,9 @@ def generate_edge_table(results: dict) -> str: floatfmt=floatfmt, tablefmt="orgtbl", stralign="right") # type: ignore -def generate_days_breakdown_stats(results: DataFrame, starting_balance: int) -> Dict[str, Any]: +def generate_days_breakdown_stats(trade_list: List, starting_balance: int) -> List[Dict[str, Any]]: + results = DataFrame.from_records(trade_list) + results['close_date'] = to_datetime(results['close_date'], utc=True) days = results.resample('1d', on='close_date') days_stats = [] for name, day in days: @@ -341,8 +343,6 @@ def generate_strategy_stats(btdata: Dict[str, DataFrame], starting_balance=starting_balance, results=results.loc[results['is_open']], skip_nan=True) - days_breakdown_stats = generate_days_breakdown_stats( - results=results, starting_balance=starting_balance) daily_stats = generate_daily_stats(results) trade_stats = generate_trading_stats(results) best_pair = max([pair for pair in pair_results if pair['key'] != 'TOTAL'], @@ -362,7 +362,7 @@ def generate_strategy_stats(btdata: Dict[str, DataFrame], 'results_per_pair': pair_results, 'sell_reason_summary': sell_reason_stats, 'left_open_trades': left_open_results, - 'days_breakdown_stats': days_breakdown_stats, + # 'days_breakdown_stats': days_breakdown_stats, 'total_trades': len(results), 'total_volume': float(results['stake_amount'].sum()), @@ -690,7 +690,9 @@ def show_backtest_result(strategy: str, results: Dict[str, Any], stake_currency: print(table) if show_days: - table = text_table_days_breakdown(days_breakdown_stats=results['days_breakdown_stats'], + days_breakdown_stats = generate_days_breakdown_stats( + trade_list=results['trades'], starting_balance=results['starting_balance']) + table = text_table_days_breakdown(days_breakdown_stats=days_breakdown_stats, stake_currency=stake_currency) if isinstance(table, str) and len(table) > 0: print(' DAYS BREAKDOWN '.center(len(table.splitlines()[0]), '=')) From de5497c76660242d41839df07c22b18ed48ba7b1 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 20 Oct 2021 19:39:37 +0200 Subject: [PATCH 4/9] backtest_days cannot be below 1 --- freqtrade/optimize/optimize_reports.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/optimize/optimize_reports.py b/freqtrade/optimize/optimize_reports.py index a6eedc6c7..abfccaa86 100644 --- a/freqtrade/optimize/optimize_reports.py +++ b/freqtrade/optimize/optimize_reports.py @@ -353,7 +353,7 @@ def generate_strategy_stats(btdata: Dict[str, DataFrame], results['open_timestamp'] = results['open_date'].view(int64) // 1e6 results['close_timestamp'] = results['close_date'].view(int64) // 1e6 - backtest_days = (max_date - min_date).days + backtest_days = (max_date - min_date).days or 1 strat_stats = { 'trades': results.to_dict(orient='records'), 'locks': [lock.to_json() for lock in content['locks']], @@ -380,7 +380,7 @@ def generate_strategy_stats(btdata: Dict[str, DataFrame], 'backtest_run_start_ts': content['backtest_start_time'], 'backtest_run_end_ts': content['backtest_end_time'], - 'trades_per_day': round(len(results) / backtest_days, 2) if backtest_days > 0 else 0, + 'trades_per_day': round(len(results) / backtest_days, 2), 'market_change': market_change, 'pairlist': list(btdata.keys()), 'stake_amount': config['stake_amount'], From 7197f4ce77d0fe331fbfcdb7edd64b2de16f6c60 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 20 Oct 2021 20:01:31 +0200 Subject: [PATCH 5/9] Don't show daily % profit (it's wrong) --- freqtrade/optimize/optimize_reports.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/freqtrade/optimize/optimize_reports.py b/freqtrade/optimize/optimize_reports.py index abfccaa86..9a4591e67 100644 --- a/freqtrade/optimize/optimize_reports.py +++ b/freqtrade/optimize/optimize_reports.py @@ -220,15 +220,12 @@ def generate_days_breakdown_stats(trade_list: List, starting_balance: int) -> Li days_stats = [] for name, day in days: profit_abs = day['profit_abs'].sum().round(10) - profit_total = day['profit_abs'].sum() / starting_balance wins = sum(day['profit_abs'] > 0) draws = sum(day['profit_abs'] == 0) loses = sum(day['profit_abs'] < 0) - profit_percentage = round(profit_total * 100.0, 2) days_stats.append( { 'date': name.strftime('%d/%m/%Y'), - 'profit_percentage': profit_percentage, 'profit_abs': profit_abs, 'wins': wins, 'draws': draws, @@ -542,14 +539,13 @@ def text_table_days_breakdown(days_breakdown_stats: List[Dict[str, Any]], """ headers = [ 'Day', - 'Profit %', f'Tot Profit {stake_currency}', 'Wins', 'Draws', 'Losses', ] output = [[ - d['date'], d['profit_percentage'], round_coin_value(d['profit_abs'], stake_currency, False), + d['date'], round_coin_value(d['profit_abs'], stake_currency, False), d['wins'], d['draws'], d['loses'], ] for d in days_breakdown_stats] return tabulate(output, headers=headers, tablefmt="orgtbl", stralign="right") From fa028c2134440bbf794920d52e7822a128cdccf7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 21 Oct 2021 06:58:40 +0200 Subject: [PATCH 6/9] Support day/week/month breakdowns --- freqtrade/commands/arguments.py | 4 +- freqtrade/commands/cli_options.py | 10 ++--- freqtrade/commands/hyperopt_commands.py | 2 +- freqtrade/configuration/configuration.py | 4 +- freqtrade/constants.py | 5 +++ freqtrade/optimize/optimize_reports.py | 47 +++++++++++++++--------- 6 files changed, 45 insertions(+), 27 deletions(-) diff --git a/freqtrade/commands/arguments.py b/freqtrade/commands/arguments.py index 11c1e9191..53cdda95d 100644 --- a/freqtrade/commands/arguments.py +++ b/freqtrade/commands/arguments.py @@ -23,7 +23,7 @@ ARGS_COMMON_OPTIMIZE = ["timeframe", "timerange", "dataformat_ohlcv", ARGS_BACKTEST = ARGS_COMMON_OPTIMIZE + ["position_stacking", "use_max_market_positions", "enable_protections", "dry_run_wallet", "timeframe_detail", - "strategy_list", "export", "exportfilename", "show_days"] + "strategy_list", "export", "exportfilename", "backtest_breakdown"] ARGS_HYPEROPT = ARGS_COMMON_OPTIMIZE + ["hyperopt", "hyperopt_path", "position_stacking", "use_max_market_positions", @@ -89,7 +89,7 @@ ARGS_HYPEROPT_LIST = ["hyperopt_list_best", "hyperopt_list_profitable", ARGS_HYPEROPT_SHOW = ["hyperopt_list_best", "hyperopt_list_profitable", "hyperopt_show_index", "print_json", "hyperoptexportfilename", "hyperopt_show_no_header", - "disableparamexport", "show_days"] + "disableparamexport", "backtest_breakdown"] NO_CONF_REQURIED = ["convert-data", "convert-trade-data", "download-data", "list-timeframes", "list-markets", "list-pairs", "list-strategies", "list-data", diff --git a/freqtrade/commands/cli_options.py b/freqtrade/commands/cli_options.py index 758e1d9ec..2c2c957df 100644 --- a/freqtrade/commands/cli_options.py +++ b/freqtrade/commands/cli_options.py @@ -193,11 +193,11 @@ AVAILABLE_CLI_OPTIONS = { type=float, metavar='FLOAT', ), - "show_days": Arg( - '--show-days', - help='Print days breakdown for backtest results', - action='store_true', - default=False, + "backtest_breakdown": Arg( + '--breakdown', + help='Show backtesting breakdown per [day, week, month].', + nargs='+', + choices=constants.BACKTEST_BREAKDOWNS ), # Edge "stoploss_range": Arg( diff --git a/freqtrade/commands/hyperopt_commands.py b/freqtrade/commands/hyperopt_commands.py index d2f8c188c..344828282 100755 --- a/freqtrade/commands/hyperopt_commands.py +++ b/freqtrade/commands/hyperopt_commands.py @@ -96,7 +96,7 @@ def start_hyperopt_show(args: Dict[str, Any]) -> None: if 'strategy_name' in metrics: strategy_name = metrics['strategy_name'] show_backtest_result(strategy_name, metrics, - metrics['stake_currency'], config.get('show_days', False)) + metrics['stake_currency'], config.get('backtest_breakdown', [])) HyperoptTools.try_export_params(config, strategy_name, val) diff --git a/freqtrade/configuration/configuration.py b/freqtrade/configuration/configuration.py index 845e87b83..c6bad9305 100644 --- a/freqtrade/configuration/configuration.py +++ b/freqtrade/configuration/configuration.py @@ -269,8 +269,8 @@ class Configuration: self._args_to_config(config, argname='export', logstring='Parameter --export detected: {} ...') - self._args_to_config(config, argname='show_days', - logstring='Parameter --show-days detected ...') + self._args_to_config(config, argname='backtest_breakdown', + logstring='Parameter --breakdown detected ...') self._args_to_config(config, argname='disableparamexport', logstring='Parameter --disableparamexport detected: {} ...') diff --git a/freqtrade/constants.py b/freqtrade/constants.py index c6b8f0e62..8bef6610c 100644 --- a/freqtrade/constants.py +++ b/freqtrade/constants.py @@ -32,6 +32,7 @@ AVAILABLE_PAIRLISTS = ['StaticPairList', 'VolumePairList', 'ShuffleFilter', 'SpreadFilter', 'VolatilityFilter'] AVAILABLE_PROTECTIONS = ['CooldownPeriod', 'LowProfitPairs', 'MaxDrawdown', 'StoplossGuard'] AVAILABLE_DATAHANDLERS = ['json', 'jsongz', 'hdf5'] +BACKTEST_BREAKDOWNS = ['day', 'week', 'month'] DRY_RUN_WALLET = 1000 DATETIME_PRINT_FORMAT = '%Y-%m-%d %H:%M:%S' MATH_CLOSE_PREC = 1e-14 # Precision used for float comparisons @@ -146,6 +147,10 @@ CONF_SCHEMA = { 'sell_profit_offset': {'type': 'number'}, 'ignore_roi_if_buy_signal': {'type': 'boolean'}, 'ignore_buying_expired_candle_after': {'type': 'number'}, + 'backtest_breakdown': { + 'type': 'array', + 'items': {'type': 'string', 'enum': BACKTEST_BREAKDOWNS} + }, 'bot_name': {'type': 'string'}, 'unfilledtimeout': { 'type': 'object', diff --git a/freqtrade/optimize/optimize_reports.py b/freqtrade/optimize/optimize_reports.py index 9a4591e67..a97f85637 100644 --- a/freqtrade/optimize/optimize_reports.py +++ b/freqtrade/optimize/optimize_reports.py @@ -213,17 +213,28 @@ def generate_edge_table(results: dict) -> str: floatfmt=floatfmt, tablefmt="orgtbl", stralign="right") # type: ignore -def generate_days_breakdown_stats(trade_list: List, starting_balance: int) -> List[Dict[str, Any]]: +def _get_resample_from_period(period: str) -> str: + if period == 'day': + return '1d' + if period == 'week': + return '1w' + if period == 'month': + return '1m' + raise ValueError(f"Period {period} is not supported.") + + +def generate_periodic_breakdown_stats(trade_list: List, period: str) -> List[Dict[str, Any]]: results = DataFrame.from_records(trade_list) results['close_date'] = to_datetime(results['close_date'], utc=True) - days = results.resample('1d', on='close_date') - days_stats = [] - for name, day in days: + resample = _get_resample_from_period(period) + period = results.resample(resample, on='close_date') + stats = [] + for name, day in period: profit_abs = day['profit_abs'].sum().round(10) wins = sum(day['profit_abs'] > 0) draws = sum(day['profit_abs'] == 0) loses = sum(day['profit_abs'] < 0) - days_stats.append( + stats.append( { 'date': name.strftime('%d/%m/%Y'), 'profit_abs': profit_abs, @@ -232,7 +243,7 @@ def generate_days_breakdown_stats(trade_list: List, starting_balance: int) -> Li 'loses': loses } ) - return days_stats + return stats def generate_trading_stats(results: DataFrame) -> Dict[str, Any]: @@ -529,8 +540,8 @@ def text_table_sell_reason(sell_reason_stats: List[Dict[str, Any]], stake_curren return tabulate(output, headers=headers, tablefmt="orgtbl", stralign="right") -def text_table_days_breakdown(days_breakdown_stats: List[Dict[str, Any]], - stake_currency: str) -> str: +def text_table_periodic_breakdown(days_breakdown_stats: List[Dict[str, Any]], + stake_currency: str, period: str) -> str: """ Generate small table with Backtest results by days :param days_breakdown_stats: Days breakdown metrics @@ -538,7 +549,7 @@ def text_table_days_breakdown(days_breakdown_stats: List[Dict[str, Any]], :return: pretty printed table with tabulate as string """ headers = [ - 'Day', + period.capitalize(), f'Tot Profit {stake_currency}', 'Wins', 'Draws', @@ -663,7 +674,7 @@ def text_table_add_metrics(strat_results: Dict) -> str: def show_backtest_result(strategy: str, results: Dict[str, Any], stake_currency: str, - show_days=False): + backtest_breakdown=[]): """ Print results for one strategy """ @@ -685,13 +696,13 @@ def show_backtest_result(strategy: str, results: Dict[str, Any], stake_currency: print(' LEFT OPEN TRADES REPORT '.center(len(table.splitlines()[0]), '=')) print(table) - if show_days: - days_breakdown_stats = generate_days_breakdown_stats( - trade_list=results['trades'], starting_balance=results['starting_balance']) - table = text_table_days_breakdown(days_breakdown_stats=days_breakdown_stats, - stake_currency=stake_currency) + for period in backtest_breakdown: + days_breakdown_stats = generate_periodic_breakdown_stats( + trade_list=results['trades'], period=period) + table = text_table_periodic_breakdown(days_breakdown_stats=days_breakdown_stats, + stake_currency=stake_currency, period=period) if isinstance(table, str) and len(table) > 0: - print(' DAYS BREAKDOWN '.center(len(table.splitlines()[0]), '=')) + print(f' {period.upper()} BREAKDOWN '.center(len(table.splitlines()[0]), '=')) print(table) table = text_table_add_metrics(results) @@ -708,7 +719,9 @@ def show_backtest_results(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, config.get('show_days', False)) + show_backtest_result( + strategy, results, stake_currency, + config.get('backtest_breakdown', [])) if len(backtest_stats['strategy']) > 1: # Print Strategy summary table From 7b5346b984508fb48f646a3f36e8aea566d51f0c Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 21 Oct 2021 07:09:17 +0200 Subject: [PATCH 7/9] Add test for breakdown-stats --- freqtrade/optimize/optimize_reports.py | 4 ++- tests/optimize/test_backtesting.py | 2 ++ tests/optimize/test_optimize_reports.py | 34 ++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/freqtrade/optimize/optimize_reports.py b/freqtrade/optimize/optimize_reports.py index a97f85637..a2590c10b 100644 --- a/freqtrade/optimize/optimize_reports.py +++ b/freqtrade/optimize/optimize_reports.py @@ -219,12 +219,14 @@ def _get_resample_from_period(period: str) -> str: if period == 'week': return '1w' if period == 'month': - return '1m' + return '1M' raise ValueError(f"Period {period} is not supported.") def generate_periodic_breakdown_stats(trade_list: List, period: str) -> List[Dict[str, Any]]: results = DataFrame.from_records(trade_list) + if len(results) == 0: + return [] results['close_date'] = to_datetime(results['close_date'], utc=True) resample = _get_resample_from_period(period) period = results.resample(resample, on='close_date') diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py index 2248cd4c1..b5fa44d01 100644 --- a/tests/optimize/test_backtesting.py +++ b/tests/optimize/test_backtesting.py @@ -1102,6 +1102,7 @@ def test_backtest_start_multi_strat_nomock(default_conf, mocker, caplog, testdat '--timerange', '1510694220-1510700340', '--enable-position-stacking', '--disable-max-market-positions', + '--breakdown', 'day', '--strategy-list', 'StrategyTestV2', 'TestStrategyLegacyV1', @@ -1130,6 +1131,7 @@ def test_backtest_start_multi_strat_nomock(default_conf, mocker, caplog, testdat captured = capsys.readouterr() assert 'BACKTESTING REPORT' in captured.out assert 'SELL REASON STATS' in captured.out + assert 'DAY BREAKDOWN' in captured.out assert 'LEFT OPEN TRADES REPORT' in captured.out assert '2017-11-14 21:17:00 -> 2017-11-14 22:58:00 | Max open trades : 1' in captured.out assert 'STRATEGY SUMMARY' in captured.out diff --git a/tests/optimize/test_optimize_reports.py b/tests/optimize/test_optimize_reports.py index 83caefd2d..4bf20e547 100644 --- a/tests/optimize/test_optimize_reports.py +++ b/tests/optimize/test_optimize_reports.py @@ -13,9 +13,9 @@ from freqtrade.data import history from freqtrade.data.btanalysis import get_latest_backtest_filename, load_backtest_data from freqtrade.edge import PairInfo from freqtrade.enums import SellType -from freqtrade.optimize.optimize_reports import (generate_backtest_stats, generate_daily_stats, - generate_edge_table, generate_pair_metrics, - generate_sell_reason_stats, +from freqtrade.optimize.optimize_reports import (_get_resample_from_period, generate_backtest_stats, + generate_daily_stats, generate_edge_table, + generate_pair_metrics, generate_periodic_breakdown_stats, generate_sell_reason_stats, generate_strategy_comparison, generate_trading_stats, store_backtest_stats, text_table_bt_results, text_table_sell_reason, @@ -377,3 +377,31 @@ def test_generate_edge_table(): assert generate_edge_table(results).count('| ETH/BTC |') == 1 assert generate_edge_table(results).count( '| Risk Reward Ratio | Required Risk Reward | Expectancy |') == 1 + + +def test_generate_periodic_breakdown_stats(testdatadir): + filename = testdatadir / "backtest-result_new.json" + bt_data = load_backtest_data(filename).to_dict(orient='records') + + res = generate_periodic_breakdown_stats(bt_data, 'day') + assert isinstance(res, list) + assert len(res) == 21 + day = res[0] + assert 'date' in day + assert 'draws' in day + assert 'loses' in day + assert 'wins' in day + assert 'profit_abs' in day + + # Select empty dataframe! + res = generate_periodic_breakdown_stats([], 'day') + assert res == [] + + +def test__get_resample_from_period(): + + assert _get_resample_from_period('day') == '1d' + assert _get_resample_from_period('week') == '1w' + assert _get_resample_from_period('month') == '1M' + with pytest.raises(ValueError, match=r"Period noooo is not supported."): + _get_resample_from_period('noooo') From e458c9867a54991ac5067f517fe2d088187e7674 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 21 Oct 2021 07:42:19 +0200 Subject: [PATCH 8/9] Styling fixes --- freqtrade/commands/arguments.py | 3 ++- freqtrade/optimize/optimize_reports.py | 6 +++--- tests/optimize/test_optimize_reports.py | 4 +++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/freqtrade/commands/arguments.py b/freqtrade/commands/arguments.py index 53cdda95d..87ef49a9b 100644 --- a/freqtrade/commands/arguments.py +++ b/freqtrade/commands/arguments.py @@ -23,7 +23,8 @@ ARGS_COMMON_OPTIMIZE = ["timeframe", "timerange", "dataformat_ohlcv", ARGS_BACKTEST = ARGS_COMMON_OPTIMIZE + ["position_stacking", "use_max_market_positions", "enable_protections", "dry_run_wallet", "timeframe_detail", - "strategy_list", "export", "exportfilename", "backtest_breakdown"] + "strategy_list", "export", "exportfilename", + "backtest_breakdown"] ARGS_HYPEROPT = ARGS_COMMON_OPTIMIZE + ["hyperopt", "hyperopt_path", "position_stacking", "use_max_market_positions", diff --git a/freqtrade/optimize/optimize_reports.py b/freqtrade/optimize/optimize_reports.py index a2590c10b..96549316d 100644 --- a/freqtrade/optimize/optimize_reports.py +++ b/freqtrade/optimize/optimize_reports.py @@ -228,10 +228,10 @@ def generate_periodic_breakdown_stats(trade_list: List, period: str) -> List[Dic if len(results) == 0: return [] results['close_date'] = to_datetime(results['close_date'], utc=True) - resample = _get_resample_from_period(period) - period = results.resample(resample, on='close_date') + resample_period = _get_resample_from_period(period) + resampled = results.resample(resample_period, on='close_date') stats = [] - for name, day in period: + for name, day in resampled: profit_abs = day['profit_abs'].sum().round(10) wins = sum(day['profit_abs'] > 0) draws = sum(day['profit_abs'] == 0) diff --git a/tests/optimize/test_optimize_reports.py b/tests/optimize/test_optimize_reports.py index 4bf20e547..b5eb09923 100644 --- a/tests/optimize/test_optimize_reports.py +++ b/tests/optimize/test_optimize_reports.py @@ -15,7 +15,9 @@ from freqtrade.edge import PairInfo from freqtrade.enums import SellType from freqtrade.optimize.optimize_reports import (_get_resample_from_period, generate_backtest_stats, generate_daily_stats, generate_edge_table, - generate_pair_metrics, generate_periodic_breakdown_stats, generate_sell_reason_stats, + generate_pair_metrics, + generate_periodic_breakdown_stats, + generate_sell_reason_stats, generate_strategy_comparison, generate_trading_stats, store_backtest_stats, text_table_bt_results, text_table_sell_reason, From 053fb076e42124285ee9cedf5848a0da2f1b5f75 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 21 Oct 2021 10:56:18 +0200 Subject: [PATCH 9/9] Add documentation for breakdown command --- docs/backtesting.md | 34 ++++++++++++++++++++++++++++++---- docs/utils.md | 3 +++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/docs/backtesting.md b/docs/backtesting.md index 4a9532894..37724b02a 100644 --- a/docs/backtesting.md +++ b/docs/backtesting.md @@ -21,6 +21,7 @@ usage: freqtrade backtesting [-h] [-v] [--logfile FILE] [-V] [-c PATH] [--timeframe-detail TIMEFRAME_DETAIL] [--strategy-list STRATEGY_LIST [STRATEGY_LIST ...]] [--export {none,trades}] [--export-filename PATH] + [--breakdown {day,week,month} [{day,week,month} ...]] optional arguments: -h, --help show this help message and exit @@ -30,7 +31,7 @@ optional arguments: Specify what timerange of data to use. --data-format-ohlcv {json,jsongz,hdf5} Storage format for downloaded candle (OHLCV) data. - (default: `None`). + (default: `json`). --max-open-trades INT Override the value of the `max_open_trades` configuration setting. @@ -65,8 +66,7 @@ optional arguments: set either in config or via command line. When using this together with `--export trades`, the strategy- name is injected into the filename (so `backtest- - data.json` becomes `backtest-data- - SampleStrategy.json` + data.json` becomes `backtest-data-SampleStrategy.json` --export {none,trades} Export backtest results (default: trades). --export-filename PATH @@ -74,7 +74,8 @@ optional arguments: Requires `--export` to be set as well. Example: `--export-filename=user_data/backtest_results/backtest _today.json` - --show-days Print a days breakdown table of the backtest results + --breakdown {day,week,month} [{day,week,month} ...] + Show backtesting breakdown per [day, week, month]. Common arguments: -v, --verbose Verbose mode (-vv for more, -vvv to get all messages). @@ -430,6 +431,31 @@ It contains some useful key metrics about performance of your strategy on backte - `Drawdown Start` / `Drawdown End`: Start and end datetime for this largest drawdown (can also be visualized via the `plot-dataframe` sub-command). - `Market change`: Change of the market during the backtest period. Calculated as average of all pairs changes from the first to the last candle using the "close" column. +### Daily / Weekly / Monthly breakdown + +You can get an overview over daily / weekly or monthly results by using the `--breakdown <>` switch. + +To visualize daily and weekly breakdowns, you can use the following: + +``` bash +freqtrade backtesting --strategy MyAwesomeStrategy --breakdown day month +``` + +``` output +======================== DAY BREAKDOWN ========================= +| Day | Tot Profit USDT | Wins | Draws | Losses | +|------------+-------------------+--------+---------+----------| +| 03/07/2021 | 200.0 | 2 | 0 | 0 | +| 04/07/2021 | -50.31 | 0 | 0 | 2 | +| 05/07/2021 | 220.611 | 3 | 2 | 0 | +| 06/07/2021 | 150.974 | 3 | 0 | 2 | +| 07/07/2021 | -70.193 | 1 | 0 | 2 | +| 08/07/2021 | 212.413 | 2 | 0 | 3 | + +``` + +The output will show a table containing the realized absolute Profit (in stake currency) for the given timeperiod, as well as wins, draws and losses that materialized (closed) on this day. + ### Further backtest-result analysis To further analyze your backtest results, you can [export the trades](#exporting-trades-to-file). diff --git a/docs/utils.md b/docs/utils.md index d8fbcacb7..4845828ab 100644 --- a/docs/utils.md +++ b/docs/utils.md @@ -667,6 +667,7 @@ usage: freqtrade hyperopt-show [-h] [-v] [--logfile FILE] [-V] [-c PATH] [--profitable] [-n INT] [--print-json] [--hyperopt-filename FILENAME] [--no-header] [--disable-param-export] + [--breakdown {day,week,month} [{day,week,month} ...]] optional arguments: -h, --help show this help message and exit @@ -680,6 +681,8 @@ optional arguments: --no-header Do not print epoch details header. --disable-param-export Disable automatic hyperopt parameter export. + --breakdown {day,week,month} [{day,week,month} ...] + Show backtesting breakdown per [day, week, month]. Common arguments: -v, --verbose Verbose mode (-vv for more, -vvv to get all messages).