-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
48 lines (38 loc) · 1.7 KB
/
demo.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
# Note: requires pip install matplotlib==3.2.2 due to bug in backtrader
from datetime import datetime
import backtrader as bt
class MacdCross(bt.Strategy):
# list of parameters which are configurable for the strategy
# params = dict(
# psignal=9, # EMA signal
# pfast=12, # period for the fast moving average
# pslow=26 # period for the slow moving average
# )
def __init__(self):
m = bt.ind.MACD()
self.crossover = bt.ind.CrossOver(
m.lines.macd, m.lines.signal) # Proper
# self.crossover = bt.ind.CrossOver(
# m.lines.signal, m.lines.macd) # Inverse
def next_open(self):
if not self.position: # not in the market
if self.crossover > 0: # if fast crosses slow to the upside
unit_price = self.datas[0].open
cash = self.broker.get_cash()
amount_to_buy, _ = divmod(
cash / unit_price, 1) # All-in
self.order = self.buy(size=amount_to_buy) # enter long,
elif self.crossover < 0: # in the market & cross to the downside
self.order = self.close() # close long position
# Cheat-on-open is needed in order to go all-in
cerebro = bt.Cerebro(cheat_on_open=True)
# Create a data feed
data = bt.feeds.YahooFinanceData(dataname='GOFORE.HE',
fromdate=datetime(2017, 1, 1),
todate=datetime(2021, 12, 31))
cerebro.broker.setcash(10000.0)
cerebro.adddata(data) # Add the data feed
cerebro.addstrategy(MacdCross) # Add the trading strategy
cerebro.run() # run it all
cerebro.plot() # and plot it with a single command
pass