-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbacktest.py
66 lines (52 loc) · 1.87 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
import backtrader as bt
import yfinance as yf
import argparse
from utilities import *
class AutoBotStrategy(bt.Strategy):
params = (
('short_window', 50),
('long_window', 200),
('rsi_period', 14),
('rsi_low', 30),
('rsi_high', 70),
)
def __init__(self):
self.dataclose = self.datas[0].close
self.short_ma = bt.indicators.SimpleMovingAverage(self.datas[0], period=self.params.short_window)
self.long_ma = bt.indicators.SimpleMovingAverage(self.datas[0], period=self.params.long_window)
self.rsi = bt.indicators.RelativeStrengthIndex(self.datas[0], period=self.params.rsi_period)
def next(self):
if self.short_ma > self.long_ma and self.rsi < self.params.rsi_low:
if not self.position:
self.buy()
elif self.short_ma < self.long_ma and self.rsi > self.params.rsi_high:
if self.position:
self.sell()
if __name__ == '__main__':
cerebro = bt.Cerebro()
cerebro.addstrategy(AutoBotStrategy)
config_path = "config/config.yaml"
parser = argparse.ArgumentParser()
args = parser.parse_args()
if args.set:
args = {v.split("=", 1)[0]: v.split("=", 1)[1] for v in list(args.set)}
args_group = args["args_group"]
if args_group.lower() == "robinhood":
configlists = ["robinhood"]
elif args_group == "manual_list":
configlists = ["quantum", "ai", "monitor"]
if configlists:
for tickers in configlists:
group = load_from_config(config_path, tickers)
for ticker in group:
# Download historical data
data = bt.feeds.PandasData(dataname=yf.download(ticker, '2020-01-01', '2021-01-01'))
if data:
cerebro.adddata(data)
cerebro.broker.set_cash(10000)
cerebro.addsizer(bt.sizers.FixedSize, stake=10)
cerebro.broker.setcommission(commission=0.001)
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.run()
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.plot()