Skip to content

Commit 49cc4c3

Browse files
authored
Fix pretty print backtests evaluation (#301)
1 parent de6b3bb commit 49cc4c3

File tree

2 files changed

+58
-15
lines changed

2 files changed

+58
-15
lines changed

investing_algorithm_framework/domain/utils/backtesting.py

+27-15
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,19 @@ def is_positive(number) -> bool:
5151
return number > 0
5252

5353

54-
def pretty_print_profit_evaluation(reports, precision=4):
54+
def pretty_print_profit_evaluation(
55+
reports, price_precision=2, percentage_precision=4
56+
):
5557
profit_table = {}
5658
profit_table["Algorithm name"] = [
5759
report.name for report in reports
5860
]
5961
profit_table["Profit"] = [
60-
f"{float(report.total_net_gain):.{precision}f} {report.trading_symbol}"
62+
f"{float(report.total_net_gain):.{price_precision}f} {report.trading_symbol}"
6163
for report in reports
6264
]
6365
profit_table["Profit percentage"] = [
64-
f"{float(report.total_net_gain_percentage):.{precision}f}%" for report in reports
66+
f"{float(report.total_net_gain_percentage):.{percentage_precision}f}%" for report in reports
6567
]
6668
profit_table["Percentage positive trades"] = [
6769
f"{float(report.percentage_positive_trades):.{0}f}%"
@@ -72,21 +74,25 @@ def pretty_print_profit_evaluation(reports, precision=4):
7274
for report in reports
7375
]
7476
profit_table["Total value"] = [
75-
f"{float(report.total_value):.{precision}f}" for report in reports
77+
f"{float(report.total_value):.{price_precision}f}" for report in reports
7678
]
7779
print(tabulate(profit_table, headers="keys", tablefmt="rounded_grid"))
7880

7981

80-
def pretty_print_growth_evaluation(reports, precision=4):
82+
def pretty_print_growth_evaluation(
83+
reports,
84+
price_precision=2,
85+
percentage_precision=4
86+
):
8187
growth_table = {}
8288
growth_table["Algorithm name"] = [
8389
report.name for report in reports
8490
]
8591
growth_table["Growth"] = [
86-
f"{float(report.growth):.{precision}f} {report.trading_symbol}" for report in reports
92+
f"{float(report.growth):.{price_precision}f} {report.trading_symbol}" for report in reports
8793
]
8894
growth_table["Growth percentage"] = [
89-
f"{float(report.growth_rate):.{precision}f}%" for report in reports
95+
f"{float(report.growth_rate):.{percentage_precision}f}%" for report in reports
9096
]
9197
growth_table["Percentage positive trades"] = [
9298
f"{float(report.percentage_positive_trades):.{0}f}%"
@@ -97,7 +103,7 @@ def pretty_print_growth_evaluation(reports, precision=4):
97103
for report in reports
98104
]
99105
growth_table["Total value"] = [
100-
f"{float(report.total_value):.{precision}f}" for report in reports
106+
f"{float(report.total_value):.{price_precision}f}" for report in reports
101107
]
102108
print(
103109
tabulate(growth_table, headers="keys", tablefmt="rounded_grid")
@@ -683,8 +689,8 @@ def pretty_print_backtest_reports_evaluation(
683689
:%%%#+- .=*#%%% {COLOR_GREEN}Backtest reports evaluation{COLOR_RESET}
684690
*%%%%%%%+------=*%%%%%%%- {COLOR_GREEN}---------------------------{COLOR_RESET}
685691
*%%%%%%%%%%%%%%%%%%%%%%%- {COLOR_YELLOW}Number of reports:{COLOR_RESET} {COLOR_GREEN}{number_of_backtest_reports} backtest reports{COLOR_RESET}
686-
.%%%%%%%%%%%%%%%%%%%%%%# {COLOR_YELLOW}Largest overall profit:{COLOR_RESET}{COLOR_GREEN}{COLOR_RESET}{COLOR_GREEN} (Algorithm {most_profitable.name}) {float(most_profitable.total_net_gain):.{precision}f} {most_profitable.trading_symbol} {float(most_profitable.total_net_gain_percentage):.{precision}f}% ({most_profitable.backtest_date_range.name} {most_profitable.backtest_date_range.start_date} - {most_profitable.backtest_date_range.end_date}){COLOR_RESET}
687-
#%%%####%%%%%%%%**#%%%+ {COLOR_YELLOW}Largest overall growth:{COLOR_RESET}{COLOR_GREEN} (Algorithm {most_profitable.name}) {float(most_growth.growth):.{precision}f} {most_growth.trading_symbol} {float(most_growth.growth_rate):.{precision}f}% ({most_growth.backtest_date_range.name} {most_growth.backtest_date_range.start_date} - {most_growth.backtest_date_range.end_date}){COLOR_RESET}
692+
.%%%%%%%%%%%%%%%%%%%%%%# {COLOR_YELLOW}Largest overall profit:{COLOR_RESET}{COLOR_GREEN}{COLOR_RESET}{COLOR_GREEN} (Algorithm {most_profitable.name}) {float(most_profitable.total_net_gain):.{price_precision}f} {most_profitable.trading_symbol} {float(most_profitable.total_net_gain_percentage):.{percentage_precision}f}% ({most_profitable.backtest_date_range.name} {most_profitable.backtest_date_range.start_date} - {most_profitable.backtest_date_range.end_date}){COLOR_RESET}
693+
#%%%####%%%%%%%%**#%%%+ {COLOR_YELLOW}Largest overall growth:{COLOR_RESET}{COLOR_GREEN} (Algorithm {most_profitable.name}) {float(most_growth.growth):.{price_precision}f} {most_growth.trading_symbol} {float(most_growth.growth_rate):.{percentage_precision}f}% ({most_growth.backtest_date_range.name} {most_growth.backtest_date_range.start_date} - {most_growth.backtest_date_range.end_date}){COLOR_RESET}
688694
.:-+*%%%%- {COLOR_PURPLE}-+..#{COLOR_RESET}%%%+.{COLOR_PURPLE}+- +{COLOR_RESET}%%%#*=-:
689695
.:-=*%%%%. {COLOR_PURPLE}+={COLOR_RESET} .%%# {COLOR_PURPLE}-+.-{COLOR_RESET}%%%%=-:..
690696
.:=+#%%%%%*###%%%%#*+#%%%%%%*+-:
@@ -711,14 +717,18 @@ def pretty_print_backtest_reports_evaluation(
711717
pretty_print_date_ranges(backtest_reports_evaluation.get_date_ranges())
712718
print("")
713719

714-
pretty_print_price_efficiency(reports, precision=precision)
720+
pretty_print_price_efficiency(reports, precision=price_precision)
715721
print(f"{COLOR_YELLOW}All profits ordered{COLOR_RESET}")
716722
pretty_print_profit_evaluation(
717-
backtest_reports_evaluation.get_profit_order(backtest_date_range), precision
723+
backtest_reports_evaluation.get_profit_order(backtest_date_range),
724+
price_precision=price_precision,
725+
percentage_precision=percentage_precision
718726
)
719727
print(f"{COLOR_YELLOW}All growths ordered{COLOR_RESET}")
720728
pretty_print_growth_evaluation(
721-
backtest_reports_evaluation.get_growth_order(backtest_date_range), precision
729+
backtest_reports_evaluation.get_growth_order(backtest_date_range),
730+
percentage_precision=percentage_precision,
731+
price_precision=price_precision
722732
)
723733

724734

@@ -755,7 +765,10 @@ def pretty_print_backtest(
755765
show_triggered_stop_losses_only: bool - show only the triggered stop losses
756766
show_take_profits: bool - show the take profits
757767
show_triggered_take_profits_only: bool - show only the triggered take profits
758-
precision: int - the precision of the floats
768+
amount_precesion: int - the amount precision
769+
price_precision: int - the price precision
770+
time_precision: int - the time precision
771+
percentage_precision: int - the percentage precision
759772
760773
Returns:
761774
None
@@ -785,7 +798,6 @@ def pretty_print_backtest(
785798
"""
786799

787800
print(ascii_art)
788-
# pretty_print_price_efficiency([backtest_report], precision=precision)
789801

790802
if show_positions:
791803
print(f"{COLOR_YELLOW}Positions overview{COLOR_RESET}")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
from unittest import TestCase
3+
4+
from investing_algorithm_framework.domain import BacktestReportsEvaluation, \
5+
pretty_print_backtest_reports_evaluation, load_backtest_reports
6+
7+
8+
class Test(TestCase):
9+
10+
def setUp(self):
11+
self.resource_dir = os.path.abspath(
12+
os.path.join(
13+
os.path.join(
14+
os.path.join(
15+
os.path.join(
16+
os.path.realpath(__file__),
17+
os.pardir
18+
),
19+
os.pardir
20+
),
21+
os.pardir
22+
),
23+
"resources"
24+
)
25+
)
26+
27+
def test_pretty_print(self):
28+
path = os.path.join(self.resource_dir, "backtest_reports_for_testing")
29+
reports = load_backtest_reports(path)
30+
evaluation = BacktestReportsEvaluation(reports)
31+
pretty_print_backtest_reports_evaluation(evaluation)

0 commit comments

Comments
 (0)