-
Notifications
You must be signed in to change notification settings - Fork 0
/
backtest.py
340 lines (247 loc) · 12.3 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
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
import backtrader as bt
import datetime
class RSIStrategy(bt.Strategy):
def __init__(self):
self.rsi = bt.talib.RSI(self.data, period=14)
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# Buy/Sell order submitted/accepted to/by broker - Nothing to do
return
# Check if an order has been completed
# Attention: broker could reject order if not enough cash
if order.status in [order.Completed]:
if order.isbuy():
print(
'Buy Executed, Price:' + (str) (order.executed.price) + ' Cost: '+ (str) (order.executed.value) + ' Comm:' + (str) (order.executed.comm))
self.buyprice = order.executed.price
self.buycomm = order.executed.comm
else: # Sell
print(
'Sell Executed, Price:' + (str) (order.executed.price) + ' Cost: '+ (str) (order.executed.value) + ' Comm:' + (str) (order.executed.comm))
self.bar_executed = len(self)
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
print('Order Canceled/Margin/Rejected')
self.order = None
def notify_trade(self, trade):
if not trade.isclosed:
return
print('Resulting PNL, GROSS: '+ (str) (trade.pnl) + ' NET: ' + (str) (trade.pnlcomm) + '\n')
def next(self):
if self.rsi < 30 and not self.position:
self.buy(size=1)
if self.rsi > 70 and self.position:
self.close()
class StochasticStrategy(bt.Strategy):
def __init__(self):
self.stoch = bt.talib.STOCH(self.data.high, self.data.low, self.data.close,
fastk_period=14, slowk_period=3, slowd_period=3)
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# Buy/Sell order submitted/accepted to/by broker - Nothing to do
return
# Check if an order has been completed
# Attention: broker could reject order if not enough cash
if order.status in [order.Completed]:
if order.isbuy():
print(
'Buy Executed, Price:' + (str) (order.executed.price) + ' Cost: '+ (str) (order.executed.value) + ' Comm:' + (str) (order.executed.comm))
self.buyprice = order.executed.price
self.buycomm = order.executed.comm
else: # Sell
print(
'Sell Executed, Price:' + (str) (order.executed.price) + ' Cost: '+ (str) (order.executed.value) + ' Comm:' + (str) (order.executed.comm))
self.bar_executed = len(self)
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
print('Order Canceled/Margin/Rejected')
self.order = None
def notify_trade(self, trade):
if not trade.isclosed:
return
print('Resulting PNL, GROSS: '+ (str) (trade.pnl) + ' NET: ' + (str) (trade.pnlcomm) + '\n')
def next(self):
if self.stoch < 20 and not self.position:
self.buy(size=1)
if self.stoch > 80 and self.position:
self.close()
# Plays crossover of 13,21 and enters a position only when price is above 200 EMA.
class EMAStrategy(bt.Strategy):
params = dict(fast = 13, slow = 21, long = 200)
def __init__(self):
self.fast_ema = bt.indicators.EMA(self.data, period=self.p.fast)
self.slow_ema = bt.indicators.EMA(self.data, period=self.p.slow)
self.long_ema = bt.indicators.EMA(self.data, period=self.p.long)
self.signal = bt.indicators.CrossOver(self.fast_ema, self.slow_ema)
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# Buy/Sell order submitted/accepted to/by broker - Nothing to do
return
# Check if an order has been completed
# Attention: broker could reject order if not enough cash
if order.status in [order.Completed]:
if order.isbuy():
print(
'Buy Executed, Price:' + (str) (order.executed.price) + ' Cost: '+ (str) (order.executed.value) + ' Comm:' + (str) (order.executed.comm))
self.buyprice = order.executed.price
self.buycomm = order.executed.comm
else: # Sell
print(
'Sell Executed, Price:' + (str) (order.executed.price) + ' Cost: '+ (str) (order.executed.value) + ' Comm:' + (str) (order.executed.comm))
self.bar_executed = len(self)
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
print('Order Canceled/Margin/Rejected')
self.order = None
def notify_trade(self, trade):
if not trade.isclosed:
return
print('Resulting PNL, GROSS: '+ (str) (trade.pnl) + ' NET: ' + (str) (trade.pnlcomm) + '\n')
def next(self):
if self.data.close[0] > self.long_ema[0] and self.signal > 0.0 and not self.position:
self.buy(size=1)
elif self.data.close[0] < self.long_ema[0] and self.signal < 0.0 and self.position:
self.close()
# Purely plays crossover of 13,21 EMAs, and disregards everything else
class EMAStrategyAggressive(bt.Strategy):
params = dict(fast = 13, slow = 21, long = 200)
def __init__(self):
self.fast_ema = bt.indicators.EMA(self.data, period=self.p.fast)
self.slow_ema = bt.indicators.EMA(self.data, period=self.p.slow)
self.long_ema = bt.indicators.EMA(self.data, period=self.p.long)
self.signal = bt.indicators.CrossOver(self.fast_ema, self.slow_ema)
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# Buy/Sell order submitted/accepted to/by broker - Nothing to do
return
# Check if an order has been completed
# Attention: broker could reject order if not enough cash
if order.status in [order.Completed]:
if order.isbuy():
print(
'Buy Executed, Price:' + (str) (order.executed.price) + ' Cost: '+ (str) (order.executed.value) + ' Comm:' + (str) (order.executed.comm))
self.buyprice = order.executed.price
self.buycomm = order.executed.comm
else: # Sell
print(
'Sell Executed, Price:' + (str) (order.executed.price) + ' Cost: '+ (str) (order.executed.value) + ' Comm:' + (str) (order.executed.comm))
self.bar_executed = len(self)
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
print('Order Canceled/Margin/Rejected')
self.order = None
def notify_trade(self, trade):
if not trade.isclosed:
return
print('Resulting PNL, GROSS: '+ (str) (trade.pnl) + ' NET: ' + (str) (trade.pnlcomm) + '\n')
def next(self):
if self.signal > 0.0 and not self.position:
self.buy(size=1)
elif self.signal < 0.0 and self.position:
self.close()
# Ichimoku
class Ichimoku(bt.Strategy):
params = (
('atrperiod', 14), # ATR Period (standard)
('atrdist_x', 1.5), # ATR distance for stop price
('atrdist_y', 1.35), # ATR distance for take profit price
('tenkan', 20),
('kijun', 60),
('senkou', 120),
('senkou_lead', 60), # forward push
('chikou', 60), # backwards push
)
def notify_order(self, order):
if order.status == order.Completed:
pass
if not order.alive():
self.order = None # indicate no order is pending
def __init__(self):
self.ichi = bt.indicators.Ichimoku(self.datas[0],
tenkan=self.params.tenkan,
kijun=self.params.kijun,
senkou=self.params.senkou,
senkou_lead=self.params.senkou_lead,
chikou=self.params.chikou)
# Cross of tenkan and kijun -
#1.0 if the 1st data crosses the 2nd data upwards - long
#-1.0 if the 1st data crosses the 2nd data downwards - short
self.tkcross = bt.indicators.CrossOver(self.ichi.tenkan_sen, self.ichi.kijun_sen)
# To set the stop price
self.atr = bt.indicators.ATR(self.data, period=self.p.atrperiod)
# Long Short ichimoku logic
self.long = bt.And((self.data.close[0] > self.ichi.senkou_span_a(0)),
(self.data.close[0] > self.ichi.senkou_span_b(0)),
(self.tkcross == 1))
self.short = bt.And((self.data.close[0] < self.ichi.senkou_span_a(0)),
(self.data.close[0] < self.ichi.senkou_span_b(0)),
(self.tkcross == -1))
def start(self):
self.order = None # sentinel to avoid operrations on pending order
def next(self):
if self.order:
return # pending order execution
if not self.position: # not in the market
if self.short:
self.order = self.sell()
ldist = self.atr[0] * self.p.atrdist_x
self.lstop = self.data.close[0] + ldist
pdist = self.atr[0] * self.p.atrdist_y
self.take_profit = self.data.close[0] - pdist
if self.long:
self.order = self.buy()
ldist = self.atr[0] * self.p.atrdist_x
self.lstop = self.data.close[0] - ldist
pdist = self.atr[0] * self.p.atrdist_y
self.take_profit = self.data.close[0] + pdist
else: # in the market
pclose = self.data.close[0]
# pstop = self.pstop
# if ((pstop<pclose<self.take_profit)|(pstop>pclose>self.take_profit)):
# self.close() # Close position
class HODL(bt.Strategy):
def start(self):
self.val_start = self.broker.get_cash() # keep the starting cash
def nextstart(self):
# Buy all the available cash
size = int(self.broker.get_cash() / self.data)
self.buy(size=size)
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# Buy/Sell order submitted/accepted to/by broker - Nothing to do
return
# Check if an order has been completed
# Attention: broker could reject order if not enough cash
if order.status in [order.Completed]:
if order.isbuy():
print(
'Buy Executed, Price:' + (str) (order.executed.price) + ' Cost: '+ (str) (order.executed.value) + ' Comm:' + (str) (order.executed.comm))
self.buyprice = order.executed.price
self.buycomm = order.executed.comm
self.bar_executed = len(self)
self.order = None
def notify_trade(self, trade):
if not trade.isclosed:
return
print('Resulting PNL, GROSS: '+ (str) (trade.pnl) + ' NET: ' + (str) (trade.pnlcomm) + '\n')
cerebro = bt.Cerebro()
cerebro.broker.setcash(100000.0)
# fromdate = datetime.datetime.strptime('2020-07-01', '%Y-%m-%d')
# todate = datetime.datetime.strptime('2020-07-12', '%Y-%m-%d')
# daily / dt format 2 signifies unix timestamps
data = bt.feeds.GenericCSVData(dataname='HistoricalDaily.csv', dtformat = 2)
# 15 minutes
# data = bt.feeds.GenericCSVData(dataname='Current_15minutes.csv', dtformat = 2, compression = 15, timeframe = bt.TimeFrame.Minutes)
# A particular range
fromdate = datetime.datetime.strptime('2021-01-01', '%Y-%m-%d')
todate = datetime.datetime.strptime('2021-04-12', '%Y-%m-%d')
# data = bt.feeds.GenericCSVData(dataname='Historical_30minutes.csv', dtformat = 2, compression = 15, timeframe = bt.TimeFrame.Minutes, fromdate = fromdate, todate = todate)
cerebro.adddata(data)
cerebro.addstrategy(Ichimoku)
# Broker / 0.1% ... divide by 100 to remove the %
cerebro.broker.setcommission(commission=0.001)
startingMoney = cerebro.broker.getvalue()
print('\nStarting Portfolio Value: %.2f' % startingMoney + '\n')
print('\nOrders:')
cerebro.run()
endMoney = cerebro.broker.getvalue()
print('\nFinal Portfolio Value: %.2f' % endMoney)
totalROE = ((endMoney - startingMoney) / startingMoney) * 100
print('\nTotal ROE: %.2f' % totalROE + '%')
cerebro.plot()