From a605ae20a72d1ebb29b4b83858f6e6c98cb59e3a Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 9 Jul 2024 06:43:12 +0200 Subject: [PATCH] feat: initial backtest table to rich --- .../optimize/optimize_reports/bt_output.py | 25 +++++++++---------- tests/optimize/test_optimize_reports.py | 5 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/freqtrade/optimize/optimize_reports/bt_output.py b/freqtrade/optimize/optimize_reports/bt_output.py index 0dac7f199..c1cf28a8b 100644 --- a/freqtrade/optimize/optimize_reports/bt_output.py +++ b/freqtrade/optimize/optimize_reports/bt_output.py @@ -46,22 +46,24 @@ def generate_wins_draws_losses(wins, draws, losses): return f"{wins:>4} {draws:>4} {losses:>4} {wl_ratio:>4}" -def text_table_bt_results(pair_results: List[Dict[str, Any]], stake_currency: str) -> str: +def text_table_bt_results( + pair_results: List[Dict[str, Any]], stake_currency: str, title: str +) -> str: """ Generates and returns a text table for the given backtest data and the results dataframe :param pair_results: List of Dictionaries - one entry per pair + final TOTAL row :param stake_currency: stake-currency - used to correctly name headers + :param title: Title of the table :return: pretty printed table with tabulate as string """ headers = _get_line_header("Pair", stake_currency, "Trades") - floatfmt = _get_line_floatfmt(stake_currency) output = [ [ t["key"], t["trades"], t["profit_mean_pct"], - t["profit_total_abs"], + f"{t['profit_total_abs']:.{decimals_per_coin(stake_currency)}f}", t["profit_total_pct"], t["duration_avg"], generate_wins_draws_losses(t["wins"], t["draws"], t["losses"]), @@ -69,7 +71,7 @@ def text_table_bt_results(pair_results: List[Dict[str, Any]], stake_currency: st for t in pair_results ] # Ignore type as floatfmt does allow tuples but mypy does not know that - return tabulate(output, headers=headers, floatfmt=floatfmt, tablefmt="orgtbl", stralign="right") + print_rich_table(output, headers, summary=title) def text_table_tags(tag_type: str, tag_results: List[Dict[str, Any]], stake_currency: str) -> str: @@ -422,15 +424,12 @@ def show_backtest_result( """ # Print results print(f"Result for strategy {strategy}") - table = text_table_bt_results(results["results_per_pair"], stake_currency=stake_currency) - if isinstance(table, str): - print(" BACKTESTING REPORT ".center(len(table.splitlines()[0]), "=")) - print(table) - - table = text_table_bt_results(results["left_open_trades"], stake_currency=stake_currency) - if isinstance(table, str) and len(table) > 0: - print(" LEFT OPEN TRADES REPORT ".center(len(table.splitlines()[0]), "=")) - print(table) + text_table_bt_results( + results["results_per_pair"], stake_currency=stake_currency, title="BACKTESTING REPORT" + ) + text_table_bt_results( + results["left_open_trades"], stake_currency=stake_currency, title="LEFT OPEN TRADES REPORT" + ) _show_tag_subresults(results, stake_currency) diff --git a/tests/optimize/test_optimize_reports.py b/tests/optimize/test_optimize_reports.py index 733f822d8..dafe599c9 100644 --- a/tests/optimize/test_optimize_reports.py +++ b/tests/optimize/test_optimize_reports.py @@ -59,7 +59,7 @@ def _backup_file(file: Path, copy_file: bool = False) -> None: copyfile(file_swp, file) -def test_text_table_bt_results(): +def test_text_table_bt_results(capsys): results = pd.DataFrame( { "pair": ["ETH/BTC", "ETH/BTC", "ETH/BTC"], @@ -72,7 +72,8 @@ def test_text_table_bt_results(): pair_results = generate_pair_metrics( ["ETH/BTC"], stake_currency="BTC", starting_balance=4, results=results ) - text = text_table_bt_results(pair_results, stake_currency="BTC") + text_table_bt_results(pair_results, stake_currency="BTC", title="title") + text = capsys.readouterr().out re.search( r".* Pair .* Trades .* Avg Profit % .* Tot Profit BTC .* Tot Profit % .* " r"Avg Duration .* Win Draw Loss Win% .*",