-
Notifications
You must be signed in to change notification settings - Fork 0
/
pairBacktestingKalman.py
367 lines (323 loc) · 14.1 KB
/
pairBacktestingKalman.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pairAnalysis import calExpMovingAverages, linearRegressionSlope
from pykalman import KalmanFilter
plot_diff, plot_lb, plot_ub, spreads, normalized_spreads, res_stds = [], [], [], [], [], []
normalization_lookback = 100
transaction_costs = []
def regressionOutput(x, slope, intercept):
return slope * x + intercept
def calSharpeRatio(percentage_returns):
return np.mean(percentage_returns)/np.std(percentage_returns)
def calCompoundReturn(rel_returns):
mul = 1
for r in rel_returns:
mul *= (1 + r)
return mul - 1
def currentPos(x, y, reg_slope, lower_bound, upper_bound, mean):
y_pred = regressionOutput(
x, reg_slope, 0)
print(y_pred, y, reg_slope)
plot_lb.append(lower_bound)
plot_ub.append(upper_bound)
spreads.append(y-y_pred)
normalized_spread = (y-y_pred)/np.std(spreads[-normalization_lookback:])
normalized_spreads.append(normalized_spread)
if spreads[-1] > upper_bound:
return 2
elif spreads[-1] < lower_bound:
return -2
elif spreads[-1] > mean:
return 1
else:
return -1
def kalmanUpdate(x, y, theta, P, W, sigma_e, num_obs):
theta_1 = theta
P_1 = P + W
print("Prior Theta and P", theta_1, P_1)
y_1_tilde = y - np.dot(x, theta_1)
print("Residual mean", y_1_tilde)
# residual covariance
V_1 = np.eye(num_obs)*sigma_e
S_1 = np.dot(x, P_1)
S_1 = np.dot(S_1, np.transpose(x))
S_1 = S_1 + V_1
# S_1 = np.dot(np.dot(x, P_1), np.transpose(x)) + V_1
print("Residual covariance", S_1)
# Kalman Gain
K_1 = np.dot(np.dot(P_1, np.transpose(x)), np.linalg.inv(S_1))
print("Kalman Gain", K_1)
# Posterior
theta_1 = theta_1 + np.dot(K_1, y_1_tilde)
P_1 = P_1 - np.dot(np.dot(K_1, x), P_1)
print("Posterior Theta and P", theta_1, P_1)
return theta_1, P_1, S_1
def backtestKalman(initial_train_df1, initial_train_df2, test_df1, test_df2, ma_window_size, lower_bound, upper_bound, window_time, waiting_time, slope_diff_threshold, stop_loss, start_trading_threshold):
# ma1, ma2 = calExpMovingAverages(
# initial_train_df1, initial_train_df2, ma_window_size)
initial_theta = np.array([0.5]).reshape(1, 1)
initial_P = np.array([1e-6]).reshape(1, 1)
W = np.array([1e-6]).reshape(1, 1)
sigma_e = 3.0
theta = initial_theta
P = initial_P
reg_slope = linearRegressionSlope(
initial_train_df1.iloc[:, 4], initial_train_df2.iloc[:, 4])
# Trading States
position = 0
returns, percentage_returns = [], []
is_open = False
open_time = 0
close_durations = []
open_times = []
cum_slope_diff = 0
prev_slope = reg_slope
mean = 0
stop_trading = False
stop_trading_days = 0
c_stop = 0
for i in range(1, len(test_df1)):
print("Checking bar", i)
price1 = test_df1.iloc[i, 1]
price2 = test_df2.iloc[i, 1]
print("Original prices", price1, price2)
# Checking Kalman update condition
if i % window_time == 0 and i + window_time < len(test_df1):
print("Updating Kalman Filter")
theta, P, S = kalmanUpdate(np.array(test_df1.iloc[i-window_time:i, 4]).reshape(
window_time, 1), np.array(test_df2.iloc[i-window_time:i, 4]).reshape(window_time, 1), theta, P, W, sigma_e, window_time)
res_covariance = S[0][0]
prev_slope = reg_slope
reg_slope = theta[0][0]
if stop_trading:
stop_trading_days += 1
if stop_trading_days == start_trading_threshold:
stop_trading = False
stop_trading_days = 0
continue
res_stds.append(res_covariance**(0.5))
if is_open:
return_till_now = position*open_slope * \
(price1 - open1) + position*(open2 - price2)
cum_slope_diff += (reg_slope - prev_slope)
open_time += 1
if np.abs(cum_slope_diff) > slope_diff_threshold:
# if open_time == waiting_time:
print("##############\nClose position", i)
close1 = price1
close2 = price2
print("Close Prices", close1, close2)
rel_return1 = position*open_slope*(close1 - open1)
rel_return2 = position*(open2 - close2)
rel_return = rel_return1 + rel_return2
percentage_return = rel_return1 / \
(open_slope*open1) + rel_return2/(open2)
print(rel_return1/(open_slope*open1), rel_return2/(open2))
# if percentage_return > 0:
percentage_returns.append(percentage_return)
print(rel_return1, rel_return2)
returns.append(rel_return)
close_durations.append(open_time)
position = 0
is_open = False
open_time = 0
cum_slope_diff = 0
transaction_costs.append(0.001*(open_slope*close1+close2))
stop_trading = True
c_stop += 1
continue
current_state = currentPos(
price1, price2, open_slope, lower_bound, upper_bound, mean)
else:
current_state = currentPos(
price1, price2, reg_slope, lower_bound, upper_bound, mean)
print(current_state)
# Checking for Opening and Closing conditions
if current_state == 2 and position == 0:
print("##############\nAbove line Open", i)
# short ETH, long BTC
open1 = price1
open2 = price2
print("Open Prices", open1, open2)
open_times.append(i)
position = 1
is_open = True
open_slope = reg_slope
transaction_costs.append(0.001*(open_slope*open1+open2))
elif current_state == -2 and position == 0:
print("##############\nBelow line Open", i)
# short BTC, long ETH
open1 = price1
open2 = price2
print("Open Prices", open1, open2)
open_times.append(i)
position = -1
is_open = True
open_slope = reg_slope
transaction_costs.append(0.001*(open_slope*open1+open2))
elif (current_state == -1 or current_state == -2) and position == 1:
print("##############\nClose position", i)
# close all positions
close1 = price1
close2 = price2
print("Close Prices", close1, close2)
rel_return1 = open_slope*(close1 - open1)
rel_return2 = (open2 - close2)
rel_return = rel_return1 + rel_return2
percentage_return = rel_return1 / \
(open_slope*open1) + rel_return2/(open2)
print(rel_return1/(open_slope*open1), rel_return2/(open2))
percentage_returns.append(percentage_return)
print(rel_return1, rel_return2)
returns.append(rel_return)
close_durations.append(open_time)
position = 0
is_open = False
open_time = 0
cum_slope_diff = 0
transaction_costs.append(0.001*(open_slope*close1+close2))
elif (current_state == 1 or current_state == 2) and position == -1:
print("##############\nClose position", i)
# close all positions
close1 = price1
close2 = price2
print("Close Prices", close1, close2)
rel_return1 = -open_slope*(close1 - open1)
rel_return2 = -(open2 - close2)
rel_return = rel_return1 + rel_return2
percentage_return = rel_return1 / \
(open_slope*open1) + rel_return2/(open2)
print(rel_return1/(open_slope*open1), rel_return2/(open2))
percentage_returns.append(percentage_return)
print(rel_return1, rel_return2)
returns.append(rel_return)
close_durations.append(open_time)
position = 0
is_open = False
open_time = 0
cum_slope_diff = 0
transaction_costs.append(0.001*(open_slope*close1+close2))
return np.array(returns), open_times, close_durations, np.array(percentage_returns)
if __name__ == "__main__":
initial_train_df1 = pd.concat([
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2021-09.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2021-10.csv", header=None)], ignore_index=True)
initial_train_df2 = pd.concat([
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2021-09.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2021-10.csv", header=None)], ignore_index=True)
test_df1 = pd.concat([
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2021-11.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2021-12.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2022-01.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2022-02.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2022-03.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2022-04.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2022-05.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2022-06.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2022-07.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2022-08.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2022-09.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2022-10.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2022-11.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2022-12.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2023-01.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2023-02.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2023-03.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2023-04.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2023-05.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2023-06.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/BTCUSDT-1m-2023-07.csv", header=None)
], ignore_index=True)
test_df2 = pd.concat([
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2021-11.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2021-12.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2022-01.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2022-02.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2022-03.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2022-04.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2022-05.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2022-06.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2022-07.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2022-08.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2022-09.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2022-10.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2022-11.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2022-12.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2023-01.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2023-02.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2023-03.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2023-04.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2023-05.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2023-06.csv", header=None),
pd.read_csv(
"pair_analysis_data/minute/ETHUSDT-1m-2023-07.csv", header=None)
], ignore_index=True)
print(len(initial_train_df1), len(test_df1))
ma_window_size = 1
lower_bound = -0.05
upper_bound = 0.05
window_time = 1
waiting_time = 1000
slope_diff_threshold = 0.003
stop_loss = 200
start_trading_threshold = 50
returns, open_times, close_durations, percentage_returns = backtestKalman(
initial_train_df1, initial_train_df2, test_df1, test_df2, ma_window_size, lower_bound, upper_bound, window_time, waiting_time, slope_diff_threshold, stop_loss, start_trading_threshold)
for i in range(len(returns)):
# if percentage_returns[i] < 0:
print(returns[i], open_times[i], close_durations[i],
percentage_returns[i] * 100)
# print(list(zip(returns, open_times, close_durations, percentage_returns * 100)))
print("Number of positions closed", len(returns))
print("Total absolute return", sum(returns))
print("Total transaction costs", sum(transaction_costs))
print("Total Simple Return", sum(percentage_returns)*100)
print("Total Compounded Return", calCompoundReturn(percentage_returns)*100)
percentage_returns = percentage_returns * 100
print("Sharpe Ratio", calSharpeRatio(percentage_returns))