forked from stefan-jansen/machine-learning-for-trading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_backtesting_with_backtrader.py
executable file
·304 lines (197 loc) · 7.39 KB
/
03_backtesting_with_backtrader.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
#!/usr/bin/env python
# coding: utf-8
# # Backtesting an ML strategy with Backtrader
# ## Imports & Settings
# In[1]:
import warnings
warnings.filterwarnings('ignore')
# In[2]:
get_ipython().run_line_magic('matplotlib', 'inline')
from pathlib import Path
import csv
from time import time
import datetime
import numpy as np
import pandas as pd
import pandas_datareader.data as web
import matplotlib.pyplot as plt
import seaborn as sns
import backtrader as bt
from backtrader.feeds import PandasData
import pyfolio as pf
# In[3]:
pd.set_option('display.expand_frame_repr', False)
np.random.seed(42)
sns.set_style('darkgrid')
# In[4]:
def format_time(t):
m_, s = divmod(t, 60)
h, m = divmod(m_, 60)
return f'{h:>02.0f}:{m:>02.0f}:{s:>02.0f}'
# ## Backtrader Setup
# ### Custom Commission Scheme
# In[5]:
class FixedCommisionScheme(bt.CommInfoBase):
"""
Simple fixed commission scheme for demo
"""
params = (
('commission', .02),
('stocklike', True),
('commtype', bt.CommInfoBase.COMM_FIXED),
)
def _getcommission(self, size, price, pseudoexec):
return abs(size) * self.p.commission
# ### DataFrame Loader
# In[6]:
OHLCV = ['open', 'high', 'low', 'close', 'volume']
# In[7]:
class SignalData(PandasData):
"""
Define pandas DataFrame structure
"""
cols = OHLCV + ['predicted']
# create lines
lines = tuple(cols)
# define parameters
params = {c: -1 for c in cols}
params.update({'datetime': None})
params = tuple(params.items())
# ### Strategy
# Includes an option to only trade on certain weekdays in lines 39/40.
# In[8]:
class MLStrategy(bt.Strategy):
params = (('n_positions', 10),
('min_positions', 5),
('verbose', False),
('log_file', 'backtest.csv'))
def log(self, txt, dt=None):
""" Logger for the strategy"""
dt = dt or self.datas[0].datetime.datetime(0)
with Path(self.p.log_file).open('a') as f:
log_writer = csv.writer(f)
log_writer.writerow([dt.isoformat()] + txt.split(','))
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
return
# Check if an order has been completed
# broker could reject order if not enough cash
if self.p.verbose:
if order.status in [order.Completed]:
p = order.executed.price
if order.isbuy():
self.log(f'{order.data._name},BUY executed,{p:.2f}')
elif order.issell():
self.log(f'{order.data._name},SELL executed,{p:.2f}')
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log(f'{order.data._name},Order Canceled/Margin/Rejected')
# bt calls prenext instead of next unless
# all datafeeds have current values
# => call next to avoid duplicating logic
def prenext(self):
self.next()
def next(self):
today = self.datas[0].datetime.date()
# if today.weekday() not in [0, 3]: # only trade on Mondays;
# return
positions = [d._name for d, pos in self.getpositions().items() if pos]
up, down = {}, {}
missing = not_missing = 0
for data in self.datas:
if data.datetime.date() == today:
if data.predicted[0] > 0:
up[data._name] = data.predicted[0]
elif data.predicted[0] < 0:
down[data._name] = data.predicted[0]
# sort dictionaries ascending/descending by value
# returns list of tuples
shorts = sorted(down, key=down.get)[:self.p.n_positions]
longs = sorted(up, key=up.get, reverse=True)[:self.p.n_positions]
n_shorts, n_longs = len(shorts), len(longs)
# only take positions if at least min_n longs and shorts
if n_shorts < self.p.min_positions or n_longs < self.p.min_positions:
longs, shorts = [], []
for ticker in positions:
if ticker not in longs + shorts:
self.order_target_percent(data=ticker, target=0)
self.log(f'{ticker},CLOSING ORDER CREATED')
short_target = -1 / max(self.p.n_positions, n_shorts)
long_target = 1 / max(self.p.n_positions, n_longs)
for ticker in shorts:
self.order_target_percent(data=ticker, target=short_target)
self.log('{ticker},SHORT ORDER CREATED')
for ticker in longs:
self.order_target_percent(data=ticker, target=long_target)
self.log('{ticker},LONG ORDER CREATED')
# ### Create and Configure Cerebro Instance
# In[9]:
cerebro = bt.Cerebro() # create a "Cerebro" instance
cash = 10000
# comminfo = FixedCommisionScheme()
# cerebro.broker.addcommissioninfo(comminfo)
cerebro.broker.setcash(cash)
# ### Add input data
# In[10]:
idx = pd.IndexSlice
data = pd.read_hdf('00_data/backtest.h5', 'data').sort_index()
tickers = data.index.get_level_values(0).unique()
for ticker in tickers:
df = data.loc[idx[ticker, :], :].droplevel('ticker', axis=0)
df.index.name = 'datetime'
bt_data = SignalData(dataname=df)
cerebro.adddata(bt_data, name=ticker)
# ### Run Strategy Backtest
# In[11]:
cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio')
cerebro.addstrategy(MLStrategy, n_positions=25, min_positions=20,
verbose=True, log_file='bt_log.csv')
start = time()
results = cerebro.run()
ending_value = cerebro.broker.getvalue()
duration = time() - start
print(f'Final Portfolio Value: {ending_value:,.2f}')
print(f'Duration: {format_time(duration)}')
# ### Plot result
# Disabled because large number of datafeeds all plot separately.
# In[12]:
# cerebro.plot() # plot the results
# figure = cerebro.plot(style='candlebars')[0][0]
# figure.savefig(f'figures/backtrader.png')
# ### Get `pyfolio` inputs
# In[13]:
# prepare pyfolio inputs
pyfolio_analyzer = results[0].analyzers.getbyname('pyfolio')
returns, positions, transactions, gross_lev = pyfolio_analyzer.get_pf_items()
returns.to_hdf('backtrader.h5', 'returns')
positions.to_hdf('backtrader.h5', 'positions')
transactions.to_hdf('backtrader.h5', 'transactions/')
gross_lev.to_hdf('backtrader.h5', 'gross_lev')
# ## Run pyfolio analysis
# In[14]:
returns = pd.read_hdf('backtrader.h5', 'returns')
positions = pd.read_hdf('backtrader.h5', 'positions')
transactions = pd.read_hdf('backtrader.h5', 'transactions/')
gross_lev = pd.read_hdf('backtrader.h5', 'gross_lev')
# In[15]:
benchmark = web.DataReader('SP500', 'fred', '2014', '2018').squeeze()
benchmark = benchmark.pct_change().tz_localize('UTC')
# In[16]:
daily_tx = transactions.groupby(level=0)
longs = daily_tx.value.apply(lambda x: x.where(x>0).sum())
shorts = daily_tx.value.apply(lambda x: x.where(x<0).sum())
# In[17]:
fig, axes = plt.subplots(ncols=2, figsize=(15, 5))
df = returns.to_frame('Strategy').join(benchmark.to_frame('Benchmark (S&P 500)'))
df.add(1).cumprod().sub(1).plot(ax=axes[0], title='Cumulative Return')
longs.plot(label='Long',ax=axes[1], title='Positions')
shorts.plot(ax=axes[1], label='Short')
positions.cash.plot(ax=axes[1], label='PF Value')
axes[1].legend()
sns.despine()
fig.tight_layout();
# In[20]:
pf.create_full_tear_sheet(returns,
transactions=transactions,
positions=positions,
benchmark_rets=benchmark.dropna())
# In[ ]: