feat: Tag table -> rich

This commit is contained in:
Matthias 2024-07-09 06:49:33 +02:00
parent a605ae20a7
commit 315351b573
2 changed files with 21 additions and 25 deletions

View File

@ -1,5 +1,5 @@
import logging import logging
from typing import Any, Dict, List, Union from typing import Any, Dict, List, Literal, Union
from tabulate import tabulate from tabulate import tabulate
@ -74,7 +74,11 @@ def text_table_bt_results(
print_rich_table(output, headers, summary=title) print_rich_table(output, headers, summary=title)
def text_table_tags(tag_type: str, tag_results: List[Dict[str, Any]], stake_currency: str) -> str: def text_table_tags(
tag_type: Literal["enter_tag", "exit_tag", "exit_reason"],
tag_results: List[Dict[str, Any]],
stake_currency: str,
) -> str:
""" """
Generates and returns a text table for the given backtest data and the results dataframe 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 pair_results: List of Dictionaries - one entry per pair + final TOTAL row
@ -85,12 +89,15 @@ def text_table_tags(tag_type: str, tag_results: List[Dict[str, Any]], stake_curr
fallback: str = "" fallback: str = ""
is_list = False is_list = False
if tag_type == "enter_tag": if tag_type == "enter_tag":
headers = _get_line_header("Enter Tag", stake_currency, "Entries") title = "Enter Tag"
headers = _get_line_header(title, stake_currency, "Entries")
elif tag_type == "exit_tag": elif tag_type == "exit_tag":
headers = _get_line_header("Exit Reason", stake_currency, "Exits") title = "Exit Reason"
headers = _get_line_header(title, stake_currency, "Exits")
fallback = "exit_reason" fallback = "exit_reason"
else: else:
# Mix tag # Mix tag
title = "Mixed Tag"
headers = _get_line_header(["Enter Tag", "Exit Reason"], stake_currency, "Trades") headers = _get_line_header(["Enter Tag", "Exit Reason"], stake_currency, "Trades")
floatfmt.insert(0, "s") floatfmt.insert(0, "s")
is_list = True is_list = True
@ -108,7 +115,7 @@ def text_table_tags(tag_type: str, tag_results: List[Dict[str, Any]], stake_curr
), ),
t["trades"], t["trades"],
t["profit_mean_pct"], t["profit_mean_pct"],
t["profit_total_abs"], f"{t['profit_total_abs']:.{decimals_per_coin(stake_currency)}f}",
t["profit_total_pct"], t["profit_total_pct"],
t.get("duration_avg"), t.get("duration_avg"),
generate_wins_draws_losses(t["wins"], t["draws"], t["losses"]), generate_wins_draws_losses(t["wins"], t["draws"], t["losses"]),
@ -116,7 +123,7 @@ def text_table_tags(tag_type: str, tag_results: List[Dict[str, Any]], stake_curr
for t in tag_results for t in tag_results
] ]
# Ignore type as floatfmt does allow tuples but mypy does not know that # 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=f"{title.upper()} STATS")
def text_table_periodic_breakdown( def text_table_periodic_breakdown(
@ -395,25 +402,13 @@ def _show_tag_subresults(results: Dict[str, Any], stake_currency: str):
Print tag subresults (enter_tag, exit_reason_summary, mix_tag_stats) Print tag subresults (enter_tag, exit_reason_summary, mix_tag_stats)
""" """
if (enter_tags := results.get("results_per_enter_tag")) is not None: if (enter_tags := results.get("results_per_enter_tag")) is not None:
table = text_table_tags("enter_tag", enter_tags, stake_currency) text_table_tags("enter_tag", enter_tags, stake_currency)
if isinstance(table, str) and len(table) > 0:
print(" ENTER TAG STATS ".center(len(table.splitlines()[0]), "="))
print(table)
if (exit_reasons := results.get("exit_reason_summary")) is not None: if (exit_reasons := results.get("exit_reason_summary")) is not None:
table = text_table_tags("exit_tag", exit_reasons, stake_currency) text_table_tags("exit_tag", exit_reasons, stake_currency)
if isinstance(table, str) and len(table) > 0:
print(" EXIT REASON STATS ".center(len(table.splitlines()[0]), "="))
print(table)
if (mix_tag := results.get("mix_tag_stats")) is not None: if (mix_tag := results.get("mix_tag_stats")) is not None:
table = text_table_tags("mix_tag", mix_tag, stake_currency) text_table_tags("mix_tag", mix_tag, stake_currency)
if isinstance(table, str) and len(table) > 0:
print(" MIXED TAG STATS ".center(len(table.splitlines()[0]), "="))
print(table)
def show_backtest_result( def show_backtest_result(

View File

@ -436,7 +436,7 @@ def test_calc_streak(testdatadir):
assert calc_streak(bt_data) == (7, 18) assert calc_streak(bt_data) == (7, 18)
def test_text_table_exit_reason(): def test_text_table_exit_reason(capsys):
results = pd.DataFrame( results = pd.DataFrame(
{ {
"pair": ["ETH/BTC", "ETH/BTC", "ETH/BTC"], "pair": ["ETH/BTC", "ETH/BTC", "ETH/BTC"],
@ -453,7 +453,8 @@ def test_text_table_exit_reason():
exit_reason_stats = generate_tag_metrics( exit_reason_stats = generate_tag_metrics(
"exit_reason", starting_balance=22, results=results, skip_nan=False "exit_reason", starting_balance=22, results=results, skip_nan=False
) )
text = text_table_tags("exit_tag", exit_reason_stats, "BTC") text_table_tags("exit_tag", exit_reason_stats, "BTC")
text = capsys.readouterr().out
assert re.search( assert re.search(
r".* Exit Reason .* Exits .* Avg Profit % .* Tot Profit BTC .* Tot Profit % .* " r".* Exit Reason .* Exits .* Avg Profit % .* Tot Profit BTC .* Tot Profit % .* "
@ -461,11 +462,11 @@ def test_text_table_exit_reason():
text, text,
) )
assert re.search( assert re.search(
r".* roi .* 2 .* 15.00 .* 0.60000000 .* 2.73 .* 0:20:00 .* 2 0 0 100 .*", r".* roi .* 2 .* 15.0 .* 0.60000000 .* 2.73 .* 0:20:00 .* 2 0 0 100 .*",
text, text,
) )
assert re.search( assert re.search(
r".* stop_loss .* 1 .* -10.00 .* -0.20000000 .* -0.91 .* 0:10:00 .* 0 0 1 0 .*", r".* stop_loss .* 1 .* -10.0 .* -0.20000000 .* -0.91 .* 0:10:00 .* 0 0 1 0 .*",
text, text,
) )
assert re.search( assert re.search(