-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.py
75 lines (51 loc) · 1.73 KB
/
index.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
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import pandas as pd
import yfinance as yf
from pandas_datareader import data as pdr
import datetime as dt # For datetime objects
import os.path # To manage paths
import sys # To find out the script name (in argv[0])
import requests
# Import the backtrader platform
import backtrader as bt
from strategies.test_strategy import TestStrategy
from sizers.sizer import MaxRiskSizer
if __name__ == '__main__':
# dynamic variables:
# - stake
# - margin
# - stops
# Create a cerebro entity
cerebro = bt.Cerebro()
cerebro.addstrategy(TestStrategy)
yf.pdr_override()
startyear = 2020
startmonth = 1
startday = 1
start = dt.datetime(startyear, startmonth, startday)
end = dt.datetime.now()
asset = "EURUSD=X"
df = pdr.get_data_yahoo(asset, start, end)
data = bt.feeds.PandasData(dataname=df)
# Add the Data Feed to Cerebro
cerebro.adddata(data)
startcash = 1000.0
# Set our desired cash start
cerebro.broker.setcash(startcash)
# Add a FixedSize sizer according to the stake
# cerebro.addsizer(bt.sizers.FixedSize, stake=10)
cerebro.addsizer(MaxRiskSizer, risk=0.2)
# Set the commission
cerebro.broker.setcommission(commission=0.0)
# Print out the starting conditions
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
# Run over everything
cerebro.run()
portvalue = cerebro.broker.getvalue()
pnl = portvalue - startcash
# Print out the final result
print('----SUMMARY----')
print('Final Portfolio Value: ${}'.format(portvalue))
print('P/L: ${}'.format(pnl))
cerebro.plot()