Skip to content

Commit f98c403

Browse files
committed
Refactor backtest reports
1 parent c227a2b commit f98c403

File tree

12 files changed

+632
-195
lines changed

12 files changed

+632
-195
lines changed

examples/backtest/algorithm/strategy.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import tulipy as ti
22

33
from investing_algorithm_framework import TimeUnit, TradingStrategy, \
4-
Algorithm, OrderSide
4+
Algorithm, OrderSide, BACKTESTING_INDEX_DATETIME
55

66
"""
77
This strategy is based on the golden cross strategy. It will buy when the
@@ -76,6 +76,8 @@ def apply_strategy(self, algorithm: Algorithm, market_data):
7676
if not algorithm.has_position(target_symbol) \
7777
and is_crossover(fast, slow) \
7878
and is_above_trend(fast, trend):
79+
print(f"opening trade on {algorithm.config['BACKTESTING_INDEX_DATETIME']} with price {price}")
80+
7981
algorithm.create_limit_order(
8082
target_symbol=target_symbol,
8183
order_side=OrderSide.BUY,
@@ -86,7 +88,7 @@ def apply_strategy(self, algorithm: Algorithm, market_data):
8688

8789
if algorithm.has_position(target_symbol) \
8890
and is_below_trend(fast, slow):
89-
91+
print(f"closing trade on {algorithm.config['BACKTESTING_INDEX_DATETIME']} with price {price}")
9092
open_trades = algorithm.get_open_trades(
9193
target_symbol=target_symbol
9294
)
@@ -103,4 +105,5 @@ def apply_strategy(self, algorithm: Algorithm, market_data):
103105
current_price=market_data[f"{symbol}-ticker"]["bid"],
104106
stop_loss_percentage=self.stop_loss_percentage
105107
):
108+
print(f"stop los triggered on {algorithm.config['BACKTESTING_INDEX_DATETIME']} with price {price} ")
106109
algorithm.close_trade(open_trade)

examples/backtest_experiment/data_sources.py

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,149 @@
1+
[
2+
{
3+
"name": "9-50-100",
4+
"description": "9-50-100",
5+
"fast": 9,
6+
"slow": 50,
7+
"trend": 100,
8+
"stop_loss_percentage": 7
9+
},
10+
{
11+
"name": "10-50-100",
12+
"description": "10-50-100",
13+
"fast": 10,
14+
"slow": 50,
15+
"trend": 100,
16+
"stop_loss_percentage": 7
17+
},
18+
{
19+
"name": "11-50-100",
20+
"description": "11-50-100",
21+
"fast": 11,
22+
"slow": 50,
23+
"trend": 100,
24+
"stop_loss_percentage": 7
25+
},
26+
{
27+
"name": "9-75-150",
28+
"description": "9-75-150",
29+
"fast": 9,
30+
"slow": 75,
31+
"trend": 150,
32+
"stop_loss_percentage": 7
33+
},
34+
{
35+
"name": "10-75-150",
36+
"description": "10-75-150",
37+
"fast": 10,
38+
"slow": 75,
39+
"trend": 150,
40+
"stop_loss_percentage": 7
41+
},
42+
{
43+
"name": "11-75-150",
44+
"description": "11-75-150",
45+
"fast": 11,
46+
"slow": 75,
47+
"trend": 150,
48+
"stop_loss_percentage": 7
49+
},
50+
{
51+
"name": "20-75-150",
52+
"description": "20-75-150",
53+
"fast": 20,
54+
"slow": 75,
55+
"trend": 150,
56+
"stop_loss_percentage": 7
57+
},
58+
{
59+
"name": "21-75-150",
60+
"description": "21-75-150",
61+
"fast": 21,
62+
"slow": 75,
63+
"trend": 150,
64+
"stop_loss_percentage": 7
65+
},
66+
{
67+
"name": "22-75-150",
68+
"description": "22-75-150",
69+
"fast": 22,
70+
"slow": 75,
71+
"trend": 150,
72+
"stop_loss_percentage": 7
73+
},
74+
{
75+
"name": "23-75-150",
76+
"description": "23-75-150",
77+
"fast": 23,
78+
"slow": 75,
79+
"trend": 150,
80+
"stop_loss_percentage": 7
81+
},
82+
{
83+
"name": "24-75-150",
84+
"description": "24-75-150",
85+
"fast": 24,
86+
"slow": 75,
87+
"trend": 150,
88+
"stop_loss_percentage": 7
89+
},
90+
{
91+
"name": "25-75-150",
92+
"description": "25-75-150",
93+
"fast": 25,
94+
"slow": 75,
95+
"trend": 150,
96+
"stop_loss_percentage": 7
97+
},
98+
{
99+
"name": "20-75-200",
100+
"description": "20-75-200",
101+
"fast": 20,
102+
"slow": 75,
103+
"trend": 200,
104+
"stop_loss_percentage": 7
105+
},
106+
{
107+
"name": "21-75-200",
108+
"description": "24-75-200",
109+
"fast": 24,
110+
"slow": 75,
111+
"trend": 200,
112+
"stop_loss_percentage": 7
113+
},
114+
{
115+
"name": "22-75-200",
116+
"description": "24-75-200",
117+
"fast": 24,
118+
"slow": 75,
119+
"trend": 200,
120+
"stop_loss_percentage": 7
121+
},
122+
{
123+
"name": "23-75-200",
124+
"description": "24-75-200",
125+
"fast": 24,
126+
"slow": 75,
127+
"trend": 200,
128+
"stop_loss_percentage": 7
129+
},
130+
{
131+
"name": "24-75-200",
132+
"description": "24-75-200",
133+
"fast": 24,
134+
"slow": 75,
135+
"trend": 200,
136+
"stop_loss_percentage": 7
137+
},
138+
{
139+
"name": "25-75-150",
140+
"description": "25-75-200",
141+
"fast": 25,
142+
"slow": 75,
143+
"trend": 200,
144+
"stop_loss_percentage": 7
145+
}
146+
]
1147
from investing_algorithm_framework import CCXTOHLCVMarketDataSource, \
2148
CCXTTickerMarketDataSource
3149

examples/backtest_experiment/backtest.py renamed to examples/backtest_experiment/run_backtest.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@
2626
)
2727
reports = app.run_backtests(
2828
algorithms=algorithms,
29-
start_date=start_date,
30-
end_date=end_date,
29+
date_ranges=[
30+
(datetime(2023, 7, 2),
31+
datetime(2023, 7, 2) + timedelta(days=200)),
32+
(datetime(2022, 7, 2),
33+
datetime(2022, 7, 2) + timedelta(days=200)),
34+
(datetime(2024, 1, 1),
35+
datetime(2024, 1, 1) + timedelta(days=100)),
36+
],
3137
pending_order_check_interval="2h",
3238
)
3339
evaluation = BacktestReportsEvaluation(reports)

examples/existing_backtest_reports_evaluation/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from investing_algorithm_framework import BacktestReportsEvaluation, \
2+
pretty_print_backtest_reports_evaluation, load_backtest_reports
3+
4+
if __name__ == "__main__":
5+
reports = load_backtest_reports("backtest_reports")
6+
evaluation = BacktestReportsEvaluation(reports)
7+
pretty_print_backtest_reports_evaluation(evaluation)

0 commit comments

Comments
 (0)