-
Notifications
You must be signed in to change notification settings - Fork 0
/
getData.py
197 lines (158 loc) · 5.03 KB
/
getData.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#getData.py
import os
import pandas as pd
import pandas_datareader.data as web
import datetime
import numpy as np
import csv
#Set Time Frame
#(year, month, day)
PREDICTED ='SPY'
START_DATE = datetime.datetime(2000, 1, 1)
END_DATE = datetime.datetime(2016, 3, 20)
lagTime = 30
rawDataDirectory = 'historical_data'
def loadTickers():
filename = 'tickers.csv'
f = open(filename, 'r')
lines = f.readlines()
f.close()
stockTickers = []
indexTickers = []
for line in lines[1:]:
line = line.strip()
if '^' in line:
indexTickers.append(line)
else:
stockTickers.append(line)
print stockTickers, indexTickers
return stockTickers, indexTickers
def getHistoricalData(ticker):
df = web.DataReader(ticker, 'yahoo', START_DATE, END_DATE)
# Date (index), Open, High, Low, Close, Volume, Adj Close
#process data ahead of time
#df = df.ix[:,[-1]]
filename = ticker + '.csv'
df.to_csv(filename, sep = ',')
def getDateAndPrice(ticker):
# csv format
# Date, Open, High, Low, Close, Volume, Adj Close
f = open(ticker + '.csv', 'r')
lines = f.readlines()
f.close()
dates = []
prices = []
for line in lines[1:]:
line = line.strip()
line = line.split(',')
date, price = line[0], float(line[1])
dates.append(date)
prices.append(price)
return dates, prices
def calcDailyPercentChange(prices):
df_prices = pd.DataFrame(np.array(prices)) \
.pct_change() \
.as_matrix()
deltaPrice = [x[0] for x in df_prices]
return np.array(deltaPrice)
def calc30DayVol(percentChange):
vol = np.zeros_like(percentChange)
length = len(percentChange)
#calc 30 day historical vol for rolling window
for day in range(lagTime,length):
delta = 0.
for i in range(1,lagTime+1):
delta += abs(percentChange[day-i])
sigma30 = 100 * delta/lagTime
vol[day] = sigma30
return vol
def calcRSI(prices, n=14):
deltas = np.diff(prices)
seed = deltas[:n+1]
up = seed[seed >= 0].sum()/n
down = -seed[seed < 0].sum()/n
RS = up/down
RSI = np.zeros_like(prices)
RSI[:n] = 100. - 100./(1.+ RS)
for i in range(lagTime, len(prices)):
delta = deltas[i-1]
if delta > 0:
upval = delta
downval = 0.
else:
upval = 0.
downval = -delta
up = (up * (n-1) + upval)/n
down = (down * (n-1) + downval)/n
RS = up/down
RSI[i] = 100. - 100./(1.+ RS)
return RSI
def combineTechnicalIndicators(ticker):
dates, prices = getDateAndPrice(ticker)
np_dates = np.chararray(len(dates), itemsize=len(dates[0]))
for day in range(len(dates)):
np_dates[day] = dates[day]
percentChange = calcDailyPercentChange(prices)
vol = calc30DayVol(percentChange)
RSI = calcRSI(prices)
if ticker == PREDICTED:
np_prices = np.array(prices)
label = np.zeros_like(np_prices)
#create label for price of SPY
for x in range(len(np_prices[:-lagTime])):
print x
if np_prices[x] < np_prices[x + lagTime]:
label[x] = 1
else:
label[x] = 0
features = np.column_stack((np_dates, percentChange, vol, RSI, label))
headers = ['date', 'return_'+ ticker, 'vol_'+ ticker, 'RSI_'+ ticker, 'label']
else:
features = np.column_stack((np_dates, percentChange, vol, RSI))
headers = ['date', 'return_'+ ticker, 'vol_'+ ticker, 'RSI_'+ ticker]
df_features = pd.DataFrame(features, columns=headers)
print df_features[25:35]
return df_features
def joinFeatures(tickers):
df_list = []
for ticker in tickers:
getHistoricalData(ticker)
print ticker + ' data acquired.'
df_list.append(combineTechnicalIndicators(ticker))
print ticker + ' transformations made.'
feature_matrix = reduce(lambda left,right: pd.merge(left,right,on='date'), df_list)
feature_matrix.drop(feature_matrix.index[:lagTime+1], inplace=True)
feature_matrix.drop(feature_matrix.index[-lagTime:], inplace=True)
print feature_matrix
return feature_matrix
def main():
wd = os.getcwd()
if not os.path.exists(rawDataDirectory):
os.makedirs(rawDataDirectory)
stockTickers, indexTickers = loadTickers()
os.chdir(wd + '/' + rawDataDirectory)
# ticker ='AAPL'
# getHistoricalData(ticker)
feature_matrix = joinFeatures(stockTickers)
os.chdir(wd)
feature_matrix.set_index('date')
feature_matrix.to_csv('feature_matrix.csv', sep = ',')
# print type(dates)
# print type(dates[1])
# print type(prices)
# print type(percentChange)
# print type(vol)
# print type(RSI)
#
# print len(dates)
# print len(prices)
# print len(percentChange)
# print len(vol)
# print len(RSI)
# print dates
# print prices
# print percentChange[0:50]
# print vol[0:50]
# print RSI[0:50]
if __name__ == '__main__':
main()