feat: initial backtest table to rich
This commit is contained in:
@@ -46,22 +46,24 @@ def generate_wins_draws_losses(wins, draws, losses):
|
|||||||
return f"{wins:>4} {draws:>4} {losses:>4} {wl_ratio:>4}"
|
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
|
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
|
||||||
:param stake_currency: stake-currency - used to correctly name headers
|
:param stake_currency: stake-currency - used to correctly name headers
|
||||||
|
:param title: Title of the table
|
||||||
:return: pretty printed table with tabulate as string
|
:return: pretty printed table with tabulate as string
|
||||||
"""
|
"""
|
||||||
|
|
||||||
headers = _get_line_header("Pair", stake_currency, "Trades")
|
headers = _get_line_header("Pair", stake_currency, "Trades")
|
||||||
floatfmt = _get_line_floatfmt(stake_currency)
|
|
||||||
output = [
|
output = [
|
||||||
[
|
[
|
||||||
t["key"],
|
t["key"],
|
||||||
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["duration_avg"],
|
t["duration_avg"],
|
||||||
generate_wins_draws_losses(t["wins"], t["draws"], t["losses"]),
|
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
|
for t in pair_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=title)
|
||||||
|
|
||||||
|
|
||||||
def text_table_tags(tag_type: str, tag_results: List[Dict[str, Any]], stake_currency: str) -> str:
|
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 results
|
||||||
print(f"Result for strategy {strategy}")
|
print(f"Result for strategy {strategy}")
|
||||||
table = text_table_bt_results(results["results_per_pair"], stake_currency=stake_currency)
|
text_table_bt_results(
|
||||||
if isinstance(table, str):
|
results["results_per_pair"], stake_currency=stake_currency, title="BACKTESTING REPORT"
|
||||||
print(" BACKTESTING REPORT ".center(len(table.splitlines()[0]), "="))
|
)
|
||||||
print(table)
|
text_table_bt_results(
|
||||||
|
results["left_open_trades"], stake_currency=stake_currency, title="LEFT OPEN TRADES REPORT"
|
||||||
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)
|
|
||||||
|
|
||||||
_show_tag_subresults(results, stake_currency)
|
_show_tag_subresults(results, stake_currency)
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ def _backup_file(file: Path, copy_file: bool = False) -> None:
|
|||||||
copyfile(file_swp, file)
|
copyfile(file_swp, file)
|
||||||
|
|
||||||
|
|
||||||
def test_text_table_bt_results():
|
def test_text_table_bt_results(capsys):
|
||||||
results = pd.DataFrame(
|
results = pd.DataFrame(
|
||||||
{
|
{
|
||||||
"pair": ["ETH/BTC", "ETH/BTC", "ETH/BTC"],
|
"pair": ["ETH/BTC", "ETH/BTC", "ETH/BTC"],
|
||||||
@@ -72,7 +72,8 @@ def test_text_table_bt_results():
|
|||||||
pair_results = generate_pair_metrics(
|
pair_results = generate_pair_metrics(
|
||||||
["ETH/BTC"], stake_currency="BTC", starting_balance=4, results=results
|
["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(
|
re.search(
|
||||||
r".* Pair .* Trades .* Avg Profit % .* Tot Profit BTC .* Tot Profit % .* "
|
r".* Pair .* Trades .* Avg Profit % .* Tot Profit BTC .* Tot Profit % .* "
|
||||||
r"Avg Duration .* Win Draw Loss Win% .*",
|
r"Avg Duration .* Win Draw Loss Win% .*",
|
||||||
|
|||||||
Reference in New Issue
Block a user