-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacktest.py
147 lines (131 loc) · 5.47 KB
/
backtest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# trading_backtest/backtest.py
import json
import sqlite3
import uuid
from strategies.EmaCrossAdx.EmaCrossAdx import EmaCrossAdx
from strategies.SmaCrossAdx.SmaCrossAdx import SmaCrossAdx
from strategies.SeriousMACD.SeriousMACD import SeriousMACD
from strategies.SuperTrend.SuperTrend import SuperTrend
from strategies.RSISimple.RSISimple import RSISimple
from strategies.HullmaCrossAdx.HullmaCrossAdx import HullmaCrossAdx
from strategies.GoldDigger.GoldDigger import GoldDigger
from backtesting import Backtest
from utils.price_fetcher import get_price_data
from utils.results_extractor import extract_data
DB = None
def init_db():
global DB
try:
DB = sqlite3.connect("./strategy_tester.db")
DB.row_factory = sqlite3.Row
except sqlite3.Error as e:
print(f"Error connecting to database: {e}")
return e
def run_backtest(selected_options):
init_db()
print("Running backtest with the following options:")
print(f"Strategy: {selected_options['strategy']}")
print(f"Tickers: {selected_options['tickers']}")
print(f"Timeframe set: {selected_options['timeframe_set']}")
print(f"Backtest set: {selected_options['backtest_set']}")
backtest_set = {
key: value
for key, value in selected_options["backtest_set"].items()
if key != "name"
}
timeframes = selected_options["timeframe_set"]["timeframes"]
print(timeframes)
if selected_options["strategy"]["name"] == "MaCross":
strategy = SmaCrossAdx
strategy_id = 1
elif selected_options["strategy"]["name"] == "SeriousMACD":
strategy = SeriousMACD
strategy_id = 2
elif selected_options["strategy"]["name"] == "SuperTrend":
strategy = SuperTrend
strategy_id = 3
elif selected_options["strategy"]["name"] == "RSISimple":
strategy = RSISimple
strategy_id = 4
elif selected_options["strategy"]["name"] == "EmaCrossAdx":
strategy = EmaCrossAdx
strategy_id = 5
elif selected_options["strategy"]["name"] == "HullMaCrossAdx":
strategy = HullmaCrossAdx
strategy_id = 6
elif selected_options["strategy"]["name"] == "GoldDigger":
strategy = GoldDigger
strategy_id = 7
for timeframe in timeframes:
START_DATE = timeframe["start"]
END_DATE = timeframe["end"]
FREQUENCY = timeframe["interval"]
for ticker in selected_options["tickers"]:
print(f"Running backtest for ticker: {ticker}")
df_prices = get_price_data(ticker, START_DATE, END_DATE, FREQUENCY)
bt = Backtest(
df_prices, strategy, cash=100_000, commission=0, exclusive_orders=True
)
stats = bt.run(**backtest_set)
filename = f"reports/backtest/backtest_results_{uuid.uuid4()}"
if selected_options["backtest_results"] == "compact":
print(stats)
print(stats._trades)
result_data = extract_data(str(stats))
print("-----------------------------")
print("Ticker: ", ticker)
print("Return [%]", result_data["Return [%]"])
print("Buy & Hold Return [%]", result_data["Buy & Hold Return [%]"])
print("Max. Drawdown [%]", result_data["Max. Drawdown [%]"])
print("# Trades", result_data["# Trades"])
print("Win Rate [%]", result_data["Win Rate [%]"])
print("Sharpe Ratio", result_data["Sharpe Ratio"])
print("Kelly Criterion", result_data["Kelly Criterion"])
cursor = DB.cursor()
cursor.execute(
"""
INSERT INTO backtest_slice (
backtest_session_id,
configuration_id,
strategy_id,
strategy_parameters,
ticker,
start,
end,
interval,
return,
buyhold_return,
max_drawdown,
trades,
win_rate,
sharpe_ratio,
kelly_criterion,
filename
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
""",
(
selected_options["selected_session"]["id"],
selected_options["timeframe_set"]["name"],
strategy_id,
json.dumps(backtest_set),
ticker,
START_DATE,
END_DATE,
FREQUENCY,
result_data["Return [%]"],
result_data["Buy & Hold Return [%]"],
result_data["Max. Drawdown [%]"],
result_data["# Trades"],
result_data["Win Rate [%]"],
result_data["Sharpe Ratio"],
result_data["Kelly Criterion"],
filename,
),
)
DB.commit()
# print(result_data)
elif selected_options["backtest_results"] == "detailed":
print(stats)
# print(stats._trades)
if selected_options["backtest_plot"]:
bt.plot(filename=filename, open_browser=False)