-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBacktest US Sector Rotational Strategy.py
80 lines (71 loc) · 2.73 KB
/
Backtest US Sector Rotational Strategy.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
import pandas as pd
import requests
symbol_list = ['XLB','XLV','XLC','XLK','XLF','XLP','XLI','XLU','XLY','XLRE','XLE']
dict_url = {}
dict_response={}
dict_data ={}
df={}
for symbol in symbol_list:
dict_url[symbol] = "https://api.tiingo.com/tiingo/daily/"+symbol+"/prices?startDate=2005-01-03&token=ff008f598182931d7eb1f0b03600aebb4feeb732"
dict_response[symbol] = requests.get(dict_url[symbol])
dict_data[symbol] = dict_response[symbol].json()
dict_date={}
dict_date[symbol]=[]
dict_close={}
dict_close[symbol]=[]
dict_high={}
dict_high[symbol]=[]
dict_low={}
dict_low[symbol]=[]
dict_open={}
dict_open[symbol]=[]
dict_volume={}
dict_volume[symbol]=[]
dict_adjClose={}
dict_adjClose[symbol]=[]
dict_adjHigh={}
dict_adjHigh[symbol]=[]
dict_adjLow={}
dict_adjLow[symbol]=[]
dict_adjOpen={}
dict_adjOpen[symbol]=[]
dict_adjVolume={}
dict_adjVolume[symbol]=[]
dict_divCash={}
dict_divCash[symbol]=[]
dict_splitFactor={}
dict_splitFactor[symbol]=[]
# Iterate through each data point
for item in dict_data[symbol]:
dict_date[symbol].append(item['date'])
dict_close[symbol].append(item['close'])
dict_high[symbol].append(item['high'])
dict_low[symbol].append(item['low'])
dict_open[symbol].append(item['open'])
dict_volume[symbol].append(item['volume'])
dict_adjClose[symbol].append(item['adjClose'])
dict_adjHigh[symbol].append(item['adjHigh'])
dict_adjLow[symbol].append(item['adjLow'])
dict_adjOpen[symbol].append(item['adjOpen'])
dict_adjVolume[symbol].append(item['adjVolume'])
dict_divCash[symbol].append(item['divCash'])
dict_splitFactor[symbol].append(item['splitFactor'])
# Create the DataFrame
df[symbol] = pd.DataFrame({
'date': dict_date[symbol],
'close': dict_close[symbol],
'high': dict_high[symbol],
'low': dict_low[symbol],
'open': dict_open[symbol],
'volume': dict_volume[symbol],
'adjClose': dict_adjClose[symbol],
'adjHigh': dict_adjHigh[symbol],
'adjLow': dict_adjLow[symbol],
'adjOpen': dict_adjOpen[symbol],
'adjVolume': dict_adjVolume[symbol],
'divCash': dict_divCash[symbol],
'splitFactor': dict_splitFactor[symbol]
})
# Display the first few rows of the DataFrame
print(df[symbol].head())
print(df[symbol].tail())