-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMA_back_test.py
436 lines (398 loc) · 16.1 KB
/
SMA_back_test.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import pandas as pd
from scipy import optimize
from datetime import datetime, timedelta
import ta
import numpy as np
from sklearn import linear_model
class SMAVectorBacktester(object):
''' Class for the vectorized backtesting of SMA-based trading strategies.
Attributes
==========
symbol: str
RIC symbol with which to work with
SMA1: int
time window in days for shorter SMA
SMA2: int
time window in days for longer SMA
start: str
start date for data retrieval
end: str
end date for data retrieval
Methods
=======
get_data:
retrieves and prepares the base data set
set_parameters:
sets one or two new SMA parameters
run_strategy:
runs the backtest for the SMA-based strategy
plot_results:
plots the performance of the strategy compared to the symbol
update_and_run:
updates SMA parameters and returns the (negative) absolute performance
optimize_parameters:
implements a brute force optimizeation for the two SMA parameters
'''
def __init__(self, df, symbol, SMA1, SMA2, start, end):
self.df = df
self.symbol = symbol
self.SMA1 = SMA1
self.SMA2 = SMA2
self.start = start
self.end = end
self.results = None
self.get_data()
def get_data(self):
''' Retrieves and prepares the data.
'''
raw = self.df.dropna()
raw = pd.DataFrame(raw[self.symbol])
raw = raw.loc[self.start:self.end]
raw.rename(columns={self.symbol: 'price'}, inplace=True)
raw['return'] = np.log(raw / raw.shift(1))
raw['SMA1'] = raw['price'].rolling(self.SMA1).mean()
raw['SMA2'] = raw['price'].rolling(self.SMA2).mean()
self.data = raw
def set_parameters(self, SMA1=None, SMA2=None):
''' Updates SMA parameters and resp. time series.
'''
if SMA1 is not None:
self.SMA1 = SMA1
self.data['SMA1'] = self.data['price'].rolling(
self.SMA1).mean()
if SMA2 is not None:
self.SMA2 = SMA2
self.data['SMA2'] = self.data['price'].rolling(self.SMA2).mean()
def run_strategy(self):
''' Backtests the trading strategy.
'''
data = self.data.copy().dropna()
data['position'] = np.where(data['SMA1'] > data['SMA2'], 1, -1)
data['strategy'] = data['position'].shift(1) * data['return']
data.dropna(inplace=True)
data['creturns'] = data['return'].cumsum().apply(np.exp)
data['cstrategy'] = data['strategy'].cumsum().apply(np.exp)
self.results = data
# gross performance of the strategy
aperf = data['cstrategy'].iloc[-1]
# out-/underperformance of strategy
operf = aperf - data['creturns'].iloc[-1]
return round(aperf, 2), round(operf, 2)
def plot_results(self):
''' Plots the cumulative performance of the trading strategy
compared to the symbol.
'''
if self.results is None:
print('No results to plot yet. Run a strategy.')
title = '%s | SMA1=%d, SMA2=%d' % (self.symbol,
self.SMA1, self.SMA2)
self.results[['creturns', 'cstrategy']].plot(title=title,
figsize=(10, 6))
def update_and_run(self, SMA):
''' Updates SMA parameters and returns negative absolute performance
(for minimazation algorithm).
Parameters
==========
SMA: tuple
SMA parameter tuple
'''
self.set_parameters(int(SMA[0]), int(SMA[1]))
return -self.run_strategy()[0]
def optimize_parameters(self, SMA1_range, SMA2_range):
''' Finds global maximum given the SMA parameter ranges.
Parameters
==========
SMA1_range, SMA2_range: tuple
tuples of the form (start, end, step size)
'''
opt = optimize.brute(self.update_and_run, (SMA1_range, SMA2_range), finish=None)
return opt, -self.update_and_run(opt)
if __name__ == '__main__':
smabt = SMAVectorBacktester('EUR=', 42, 252,
'2010-1-1', '2020-12-31')
print(smabt.run_strategy())
smabt.set_parameters(SMA1=20, SMA2=100)
print(smabt.run_strategy())
print(smabt.optimize_parameters((30, 56, 4), (200, 300, 4)))
class MomVectorBacktester(object):
''' Class for the vectorized backtesting of
Momentum-based trading strategies.
Attributes
==========
symbol: str
RIC (financial instrument) to work with
start: str
start date for data selection
end: str
end date for data selection
amount: int, float
amount to be invested at the beginning
tc: float
proportional transaction costs (e.g. 0.5% = 0.005) per trade
Methods
=======
get_data:
retrieves and prepares the base data set
run_strategy:
runs the backtest for the momentum-based strategy
plot_results:
plots the performance of the strategy compared to the symbol
'''
def __init__(self, df, symbol, start, end, amount, tc):
self.df = df
self.symbol = symbol
self.start = start
self.end = end
self.amount = amount
self.tc = tc
self.results = None
self.get_data()
def get_data(self):
''' Retrieves and prepares the data.
'''
raw = self.df.dropna()
raw = pd.DataFrame(raw[self.symbol])
raw = raw.loc[self.start:self.end]
raw.rename(columns={self.symbol: 'price'}, inplace=True)
raw['return'] = np.log(raw / raw.shift(1))
self.data = raw
def run_strategy(self, momentum=1):
''' Backtests the trading strategy.
'''
self.momentum = momentum
data = self.data.copy().dropna()
data['position'] = np.sign(data['return'].rolling(momentum).mean())
data['strategy'] = data['position'].shift(1) * data['return']
# determine when a trade takes place
data.dropna(inplace=True)
trades = data['position'].diff().fillna(0) != 0
# subtract transaction costs from return when trade takes place
data['strategy'][trades] -= self.tc
data['creturns'] = self.amount * data['return'].cumsum().apply(np.exp)
data['cstrategy'] = self.amount * \
data['strategy'].cumsum().apply(np.exp)
self.results = data
# absolute performance of the strategy
aperf = self.results['cstrategy'].iloc[-1]
# out-/underperformance of strategy
operf = aperf - self.results['creturns'].iloc[-1]
return round(aperf, 2), round(operf, 2)
def plot_results(self):
''' Plots the cumulative performance of the trading strategy
compared to the symbol.
'''
if self.results is None:
print('No results to plot yet. Run a strategy.')
title = '%s | TC = %.4f' % (self.symbol, self.tc)
self.results[['creturns', 'cstrategy']].plot(title=title,
figsize=(10, 6))
if __name__ == '__main__':
mombt = MomVectorBacktester('XAU=', '2010-1-1', '2020-12-31',
10000, 0.0)
print(mombt.run_strategy())
print(mombt.run_strategy(momentum=2))
mombt = MomVectorBacktester('XAU=', '2010-1-1', '2020-12-31',
10000, 0.001)
print(mombt.run_strategy(momentum=2))
class MRVectorBacktester(MomVectorBacktester):
''' Class for the vectorized backtesting of
Mean Reversion-based trading strategies.
Attributes
==========
symbol: str
RIC symbol with which to work with
start: str
start date for data retrieval
end: str
end date for data retrieval
amount: int, float
amount to be invested at the beginning
tc: float
proportional transaction costs (e.g. 0.5% = 0.005) per trade
Methods
=======
get_data:
retrieves and prepares the base data set
run_strategy:
runs the backtest for the mean reversion-based strategy
plot_results:
plots the performance of the strategy compared to the symbol
'''
def run_strategy(self, SMA, threshold):
''' Backtests the trading strategy.
'''
data = self.data.copy().dropna()
data['sma'] = data['price'].rolling(SMA).mean()
data['distance'] = data['price'] - data['sma']
data.dropna(inplace=True)
# sell signals
data['position'] = np.where(data['distance'] > threshold,
-1, np.nan)
# buy signals
data['position'] = np.where(data['distance'] < -threshold,
1, data['position'])
# crossing of current price and SMA (zero distance)
data['position'] = np.where(data['distance'] *
data['distance'].shift(1) < 0,
0, data['position'])
data['position'] = data['position'].ffill().fillna(0)
data['strategy'] = data['position'].shift(1) * data['return']
# determine when a trade takes place
trades = data['position'].diff().fillna(0) != 0
# subtract transaction costs from return when trade takes place
data['strategy'][trades] -= self.tc
data['creturns'] = self.amount * \
data['return'].cumsum().apply(np.exp)
data['cstrategy'] = self.amount * \
data['strategy'].cumsum().apply(np.exp)
self.results = data
# absolute performance of the strategy
aperf = self.results['cstrategy'].iloc[-1]
# out-/underperformance of strategy
operf = aperf - self.results['creturns'].iloc[-1]
return round(aperf, 2), round(operf, 2)
if __name__ == '__main__':
mrbt = MRVectorBacktester('GDX', '2010-1-1', '2020-12-31',
10000, 0.0)
print(mrbt.run_strategy(SMA=25, threshold=5))
mrbt = MRVectorBacktester('GDX', '2010-1-1', '2020-12-31',
10000, 0.001)
print(mrbt.run_strategy(SMA=25, threshold=5))
mrbt = MRVectorBacktester('GLD', '2010-1-1', '2020-12-31',
10000, 0.001)
print(mrbt.run_strategy(SMA=42, threshold=7.5))
class ScikitVectorBacktester(object):
''' Class for the vectorized backtesting of
Machine Learning-based trading strategies.
Attributes
==========
symbol: str
TR RIC (financial instrument) to work with
start: str
start date for data selection
end: str
end date for data selection
amount: int, float
amount to be invested at the beginning
tc: float
proportional transaction costs (e.g. 0.5% = 0.005) per trade
model: str
either 'regression' or 'logistic'
Methods
=======
get_data:
retrieves and prepares the base data set
select_data:
selects a sub-set of the data
prepare_features:
prepares the features data for the model fitting
fit_model:
implements the fitting step
run_strategy:
runs the backtest for the regression-based strategy
plot_results:
plots the performance of the strategy compared to the symbol
'''
def __init__(self, df, symbol, start, end, amount, tc, model):
self.df = df
self.symbol = symbol
self.start = start
self.end = end
self.amount = amount
self.tc = tc
self.results = None
if model == 'regression':
self.model = linear_model.LinearRegression()
elif model == 'logistic':
self.model = linear_model.LogisticRegression(C=1e6,
solver='lbfgs', multi_class='ovr', max_iter=1000)
else:
raise ValueError('Model not known or not yet implemented.')
self.get_data()
def get_data(self):
''' Retrieves and prepares the data.
'''
#raw = pd.read_csv('http://hilpisch.com/pyalgo_eikon_eod_data.csv',
# index_col=0, parse_dates=True).dropna()
raw = self.df.dropna()
raw = pd.DataFrame(raw[self.symbol])
raw = raw.loc[self.start:self.end]
raw.rename(columns={self.symbol: 'price'}, inplace=True)
raw['returns'] = np.log(raw / raw.shift(1))
self.data = raw.dropna()
def select_data(self, start, end):
''' Selects sub-sets of the financial data.
'''
data = self.data[(self.data.index >= start) &
(self.data.index <= end)].copy()
return data
def prepare_features(self, start, end):
''' Prepares the feature columns for the regression and prediction steps.
'''
self.data_subset = self.select_data(start, end)
self.feature_columns = []
for lag in range(1, self.lags + 1):
col = 'lag_{}'.format(lag)
self.data_subset[col] = self.data_subset['returns'].shift(lag)
self.feature_columns.append(col)
self.data_subset.dropna(inplace=True)
def fit_model(self, start, end):
''' Implements the fitting step.
'''
self.prepare_features(start, end)
self.model.fit(self.data_subset[self.feature_columns],
np.sign(self.data_subset['returns']))
def run_strategy(self, start_in, end_in, start_out, end_out, lags=3):
''' Backtests the trading strategy.
'''
self.lags = lags
self.fit_model(start_in, end_in)
# data = self.select_data(start_out, end_out)
self.prepare_features(start_out, end_out)
prediction = self.model.predict(
self.data_subset[self.feature_columns])
self.data_subset['prediction'] = prediction
self.data_subset['strategy'] = (self.data_subset['prediction'] *
self.data_subset['returns'])
# determine when a trade takes place
trades = self.data_subset['prediction'].diff().fillna(0) != 0
# subtract transaction costs from return when trade takes place
self.data_subset['strategy'][trades] -= self.tc
self.data_subset['creturns'] = (self.amount *
self.data_subset['returns'].cumsum().apply(np.exp))
self.data_subset['cstrategy'] = (self.amount *
self.data_subset['strategy'].cumsum().apply(np.exp))
self.results = self.data_subset
# absolute performance of the strategy
aperf = self.results['cstrategy'].iloc[-1]
# out-/underperformance of strategy
operf = aperf - self.results['creturns'].iloc[-1]
return round(aperf, 2), round(operf, 2)
def plot_results(self):
''' Plots the cumulative performance of the trading strategy
compared to the symbol.
'''
if self.results is None:
print('No results to plot yet. Run a strategy.')
title = '%s | TC = %.4f' % (self.symbol, self.tc)
self.results[['creturns', 'cstrategy']].plot(title=title,
figsize=(10, 6))
if __name__ == '__main__':
scibt = ScikitVectorBacktester('.SPX', '2010-1-1', '2019-12-31',
10000, 0.0, 'regression')
print(scibt.run_strategy('2010-1-1', '2019-12-31',
'2010-1-1', '2019-12-31'))
print(scibt.run_strategy('2010-1-1', '2016-12-31',
'2017-1-1', '2019-12-31'))
scibt = ScikitVectorBacktester('.SPX', '2010-1-1', '2019-12-31',
10000, 0.0, 'logistic')
print(scibt.run_strategy('2010-1-1', '2019-12-31',
'2010-1-1', '2019-12-31'))
print(scibt.run_strategy('2010-1-1', '2016-12-31',
'2017-1-1', '2019-12-31'))
scibt = ScikitVectorBacktester('.SPX', '2010-1-1', '2019-12-31',
10000, 0.001, 'logistic')
print(scibt.run_strategy('2010-1-1', '2019-12-31',
'2010-1-1', '2019-12-31', lags=15))
print(scibt.run_strategy('2010-1-1', '2013-12-31',
'2014-1-1', '2019-12-31', lags=15))