Skip to content

Commit dcb7cc2

Browse files
authored
Create Backtest US Sector Rotational Strategy.py
1 parent 40f58df commit dcb7cc2

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import pandas as pd
2+
import requests
3+
4+
symbol_list = ['XLB','XLV','XLC','XLK','XLF','XLP','XLI','XLU','XLY','XLRE','XLE']
5+
6+
dict_url = {}
7+
dict_response={}
8+
dict_data ={}
9+
10+
df={}
11+
12+
for symbol in symbol_list:
13+
dict_url[symbol] = "https://api.tiingo.com/tiingo/daily/"+symbol+"/prices?startDate=2005-01-03&token=ff008f598182931d7eb1f0b03600aebb4feeb732"
14+
15+
dict_response[symbol] = requests.get(dict_url[symbol])
16+
dict_data[symbol] = dict_response[symbol].json()
17+
18+
dict_date={}
19+
dict_date[symbol]=[]
20+
dict_close={}
21+
dict_close[symbol]=[]
22+
dict_high={}
23+
dict_high[symbol]=[]
24+
dict_low={}
25+
dict_low[symbol]=[]
26+
dict_open={}
27+
dict_open[symbol]=[]
28+
dict_volume={}
29+
dict_volume[symbol]=[]
30+
dict_adjClose={}
31+
dict_adjClose[symbol]=[]
32+
dict_adjHigh={}
33+
dict_adjHigh[symbol]=[]
34+
dict_adjLow={}
35+
dict_adjLow[symbol]=[]
36+
dict_adjOpen={}
37+
dict_adjOpen[symbol]=[]
38+
dict_adjVolume={}
39+
dict_adjVolume[symbol]=[]
40+
dict_divCash={}
41+
dict_divCash[symbol]=[]
42+
dict_splitFactor={}
43+
dict_splitFactor[symbol]=[]
44+
45+
# Iterate through each data point
46+
for item in dict_data[symbol]:
47+
dict_date[symbol].append(item['date'])
48+
dict_close[symbol].append(item['close'])
49+
dict_high[symbol].append(item['high'])
50+
dict_low[symbol].append(item['low'])
51+
dict_open[symbol].append(item['open'])
52+
dict_volume[symbol].append(item['volume'])
53+
dict_adjClose[symbol].append(item['adjClose'])
54+
dict_adjHigh[symbol].append(item['adjHigh'])
55+
dict_adjLow[symbol].append(item['adjLow'])
56+
dict_adjOpen[symbol].append(item['adjOpen'])
57+
dict_adjVolume[symbol].append(item['adjVolume'])
58+
dict_divCash[symbol].append(item['divCash'])
59+
dict_splitFactor[symbol].append(item['splitFactor'])
60+
61+
# Create the DataFrame
62+
df[symbol] = pd.DataFrame({
63+
'date': dict_date[symbol],
64+
'close': dict_close[symbol],
65+
'high': dict_high[symbol],
66+
'low': dict_low[symbol],
67+
'open': dict_open[symbol],
68+
'volume': dict_volume[symbol],
69+
'adjClose': dict_adjClose[symbol],
70+
'adjHigh': dict_adjHigh[symbol],
71+
'adjLow': dict_adjLow[symbol],
72+
'adjOpen': dict_adjOpen[symbol],
73+
'adjVolume': dict_adjVolume[symbol],
74+
'divCash': dict_divCash[symbol],
75+
'splitFactor': dict_splitFactor[symbol]
76+
})
77+
78+
# Display the first few rows of the DataFrame
79+
print(df[symbol].head())
80+
print(df[symbol].tail())

0 commit comments

Comments
 (0)