-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptimize_bt.py
47 lines (36 loc) · 1.26 KB
/
optimize_bt.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
import backtrader as bt
import extensions.strategies as st
import extensions.sizers as sz
import extensions.queries as qs
import datetime as dt
if __name__ == '__main__':
# Instantiate a Cerebro class
cerebro = bt.Cerebro(optreturn=False)
# Add a strategy and its corresponding parameter(s) to optimize
cerebro.optstrategy(
st.SmaCrossoverStrategy,
sma1_period=range(10, 21),
opt_mode=True
)
# Query historical Brent Crude Oil data from Quandl for backtesting
brent = qs.import_quandl_futures('CHRIS/ICE_B1')
# Specify the backtest timeframe from '01/01/2018' to '01/01/2019'
data = bt.feeds.PandasData(
dataname=brent,
fromdate=dt.datetime(2014, 1, 1),
todate=dt.datetime(2019, 1, 1)
)
# Add the data feed to Cerebro
cerebro.adddata(data)
# Set starting cash as $10,000
starting_cash = 10_000
cerebro.broker.setcash(starting_cash)
# Set broker commission to 0.1%
cerebro.broker.setcommission(commission=0.001)
# Add a custom Sizer class to Cerebro
cerebro.addsizer(sz.ModifiyingSizer, size=100)
# Iterate through all backtests and print the results
print('')
print('Backtest Optimization Results')
print('-' * 50)
opt_runs = cerebro.run()