-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataPreProcessing.py
426 lines (364 loc) · 13.9 KB
/
DataPreProcessing.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import os
import datetime as dt
import pandas as pd
import pandas_datareader.data as web
import time
import numpy as np
import datetime as dt
from collections import Counter
companyList = ['MMM', 'MSFT', 'AAPL', 'XOM', 'BAC']
companyAndIndexList = ['MMM', 'MSFT', 'AAPL', 'XOM', 'BAC', 'SP500', 'NASDAQ']
def get_data_from_yahoo(tickers = companyList):
start = dt.datetime(2000,1,1)
end = dt.datetime(2017,11,10)
if not os.path.exists('stock_dfs'):
os.makedirs('stock_dfs')
for ticker in tickers:
reTry = True
reTryTimes = 5
while(reTry and reTryTimes>0):
try:
if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
df = web.DataReader(ticker,'yahoo',start,end)
df.to_csv('stock_dfs/{}.csv'.format(ticker))
print('{} has been got successfully'.format(ticker))
reTry = False
else:
print("Already have {}".format(ticker))
reTry = False
except Exception as e:
reTryTimes = reTryTimes - 1
print(e)
print('Retry getting {}, times {}'.format(ticker, reTryTimes))
reTry = True
time.sleep(5)
df = web.DataReader('^GSPC','yahoo',start,end)
df.to_csv('stock_dfs/SP500.csv')
df = web.DataReader('^IXIC','yahoo',start,end)
df.to_csv('stock_dfs/NASDAQ.csv')
def getStock(ticker):
path = '.\stock_dfs\{}.csv'.format(ticker)
if not os.path.exists(path):
print('{} not exist'.format(ticker))
return None
df = pd.read_csv(
'.\stock_dfs\{}.csv'.format(ticker),
index_col = 0,
parse_dates = True
)
return df
def SMA(df, days):
if not 'SMA{}'.format(days) in df.columns:
df['SMA{}'.format(days)] = df['Adj Close'].rolling(window = days, min_periods = 0).mean()
return df
def EMA(df, days):
if not 'EMA{}'.format(days) in df.columns:
df['EMA{}'.format(days)] = df['Adj Close'].ewm(span=days).mean()
return df
def MACD(df, fastMA_Period = 12, slowMA_Period = 26, signal_Period = 9):
EMA(df, fastMA_Period)
EMA(df, slowMA_Period)
df['MACD{}_{}_{}'.format(fastMA_Period,slowMA_Period,signal_Period)] = df['EMA{}'.format(fastMA_Period)] - df['EMA{}'.format(slowMA_Period)]
df['MACD_signal{}_{}_{}'.format(fastMA_Period,slowMA_Period,signal_Period)] = df['MACD{}_{}_{}'.format(fastMA_Period,slowMA_Period,signal_Period)].ewm(span = signal_Period).mean()
return df
def Bollinger_bands(df, days = 14):
SMA(df, days)
std = df['Adj Close'].rolling(window = days, min_periods = 0).std()
df['Upper_band{}'.format(days)] = df['SMA{}'.format(days)] + 2*std
df['Lower band{}'.format(days)] = df['SMA{}'.format(days)] - 2*std
return df
##Avg(PriceUp)/(Avg(PriceUP)+Avg(PriceDown)*100
##Where: PriceUp(t)=1*(Price(t)-Price(t-1)){Price(t)-
##Price(t-1)>0};
##PriceDown(t)=1*(Price(t-1)-Price(t)){Price(t)-
##Price(t-1)<0};
def RSI(df, days=6, method = 'EMA'):
close = df['Adj Close']
delta = close-close.shift(1)
delta.fillna(0, inplace = True)
up, down = delta.copy(), delta.copy()
up[up<0] = 0
down[down>0] = 0
roll_up = pd.DataFrame()
roll_down = pd.DataFrame ()
if(method == 'SMA'):
roll_up = up.rolling(window=days, min_periods = 0).mean()
roll_down = down.abs().rolling(window=days, min_periods = 0).mean()
if(method == 'EMA'):
roll_up = up.ewm(span=days).mean()
roll_down = down.abs().ewm(span = days).mean()
RS = roll_up/roll_down
RS.fillna(1, inplace = True)
df['RSI{}{}'.format(days,method)] = 100.0 - (100.0/(1.0 + RS))
return df
def Momentum(df, days):
df['Momentum{}'.format(days)] = df['Adj Close'] - df['Adj Close'].shift(days)
df.fillna(0, inplace = True)
return df
def RateOfChange(df, days):
df['RateOfChange{}'.format(days)] = (df['Adj Close']/df['Adj Close'].shift(days))*100
df.fillna(100, inplace = True)
return df
##CCI = (Typical Price - n-period SMA of TP) / (Constant x Mean Deviation)
##
##Typical Price (TP) = (High + Low + Close)/3
##
##Constant = .015
def CCI(df, days = 20):
TP = (df['High']+df['Low']+df['Close'])/3
SMATP = TP.rolling(window = days, min_periods = 0).mean()
STDTP = TP.rolling(window = days, min_periods = 0).std()
df['CCI{}'.format(days)] = (TP - SMATP)/(0.015*STDTP)
df.fillna(0, inplace = True)
return df
##%R = (Highest High - Close)/(Highest High - Lowest Low) * -100
##
##Lowest Low = lowest low for the look-back period
##Highest High = highest high for the look-back period
##%R is multiplied by -100 correct the inversion and move the decimal.
def WILLR(df, days = 14):
HH = df['High'].rolling(window = days, min_periods = 0).max()
LL = df['Low'].rolling(window = days, min_periods = 0).min()
WillR = (HH-df['Close'])/(HH-LL)*(-100)
df['WILLR{}'.format(days)] = WillR
return df
##ATR(t)=((n-1)*ATR(t-1)+Tr(t))/n where
##Tr(t)=Max(Abs(High-Low), Abs(Hight-Close(t-1)),
##Abs(Low-Close(t-1));
def ATR(df, days = 14):
length = len(df['Close'])
HL = df['High'] - df['Low']
HC = abs(df['High'] - df['Close'].shift(1))
LC = abs(df['Low'] - df['Close'].shift(1))
temp_df = pd.DataFrame(index = df.index)
temp_df['HL'] = HL
temp_df['HC'] = HC
temp_df['LC'] = LC
temp_df['TR'] = temp_df.max(axis = 1)
temp_df['High'] = df['High']
temp_df['Low'] = df['Low']
temp_df['Close'] = df['Close']
temp_df['ATR1'] = temp_df['TR'].rolling(window = days).mean()
temp_df['ATR'] = pd.Series(np.zeros(length), index = temp_df.index)
temp_df['ATR'].iloc[days-1:days*2-2]=temp_df['ATR1'].iloc[days-1:days*2-2]
ATR13 = temp_df['ATR'].iloc[days-1:days*2-2].values
TR14 = temp_df['TR'][days*2-2:].values
ATR = pd.Series(ATRCal(ATR13,TR14)).values
temp_df['ATR'] = ATR
df['ATR{}'.format(days)] = temp_df['ATR']
return df
def ATRCal(ATR13, TR14):
ret = list(ATR13)
for i in range(0, len(TR14)):
newATR = (sum(ret[i:i+13])+TR14[i])/14
ret.append(newATR)
for i in range(0,13):
ret.insert(0,0)
return ret
##TR(t)/TR(t-1) where
##TR(t)=EMA(EMA(EMA(Price(t)))) over n days
##period
##Triple
##Exponential
##Moving
##Average
def TEMA(df, days):
EMA(df, days)
temp_df = pd.DataFrame(index = df.index)
temp_df['DoubleEMA'] = df['EMA{}'.format(days)].ewm(span=days).mean()
temp_df['TripleEMA'] = temp_df['DoubleEMA'].ewm(span=days).mean()
temp_df['TripleEMA'] = 3*df['EMA{}'.format(days)]-3*temp_df['DoubleEMA']+temp_df['TripleEMA']
df['TripleEMA{}'.format(days)] = temp_df['TripleEMA']
return df
##If the closing price is above the prior close price then:
##Current OBV = Previous OBV + Current Volume
##
##If the closing price is below the prior close price then:
##Current OBV = Previous OBV - Current Volume
##
##If the closing prices equals the prior close price then:
##Current OBV = Previous OBV (no change)
def OBV(df, startDay):
volume = df['Volume'].ix[startDay:].values.tolist()
close = df['Adj Close'].ix[startDay:].values.tolist()
offset = len(df['Volume']) - len(volume)
obv = []
obv.append(volume[0])
for i in range(1,len(volume)):
if(close[i]>close[i-1]):
obv.append(volume[i]+obv[i-1])
elif(close[i]==close[i-1]):
obv.append(obv[i-1])
else:
obv.append(obv[i-1]-volume[i])
for i in range(0,offset):
obv.insert(0,0)
df['OBV'] = pd.Series(obv).values
return df
##Money Flow Index
##Typical Price = (High + Low + Close)/3
##
##Raw Money Flow = Typical Price x Volume
##Money Flow Ratio = (14-period Positive Money Flow)/(14-period Negative Money Flow)
##
##Money Flow Index = 100 - 100/(1 + Money Flow Ratio)
def MFI(df, days = 14):
TP = (df['High']+df['Low']+df['Close'])/3
RMF = TP*df['Volume']
delta = TP -TP.shift(1)
delta.fillna(0, inplace = True)
deltaList = delta.values.tolist()
upDownList = map(lambda x: 1 if x > 0 else ( -1 if x < 0 else 0), deltaList)
upDown = pd.Series(upDownList).values
RMF = RMF*upDown
PMF, NMF = RMF.copy(), RMF.copy()
PMF[PMF<0]=0
NMF[NMF>0]=0
PeriodPMF = PMF.rolling(window = days).sum()
PeriodNMF = NMF.rolling(window = days).sum()
MFR = abs(PeriodPMF/PeriodNMF)
MFI = 100 - 100/(1.0+MFR)
df['MFI{}'.format(days)] = MFI
return df
##Calculate the True Range (TR), Plus Directional Movement (+DM) and Minus Directional Movement (-DM) for each period.
##Smooth these periodic values using Wilder's smoothing techniques. These are explained in detail in the next section.
##Divide the 14-day smoothed Plus Directional Movement (+DM) by the 14-day smoothed True Range to find the 14-day Plus Directional Indicator (+DI14).
##Multiply by 100 to move the decimal point two places. This +DI14 is the green Plus Directional Indicator line (+DI) that is plotted along with the ADX line.
##Divide the 14-day smoothed Minus Directional Movement (-DM) by the 14-day smoothed True Range to find the 14-day Minus Directional Indicator (-DI14).
##Multiply by 100 to move the decimal point two places. This -DI14 is the red Minus Directional Indicator line (-DI) that is plotted along with the ADX line.
##The Directional Movement Index (DX) equals the absolute value of +DI14 less -DI14 divided by the sum of +DI14 and -DI14. Multiply the result by 100 to move the decimal point over two places.
##After all these steps, it is time to calculate the Average Directional Index (ADX) line. The first ADX value is simply a 14-day average of DX.
##Subsequent ADX values are smoothed by multiplying the previous 14-day ADX value by 13, adding the most recent DX value, and dividing this total by 14.
def ADX_test(df, days = 14):
HL = df['High'] - df['Low']
HC = abs(df['High'] - df['Close'].shift(1))
LC = abs(df['Low'] - df['Close'].shift(1))
temp_df = pd.DataFrame(index = df.index)
temp_df['HL'] = HL
temp_df['HC'] = HC
temp_df['LC'] = LC
temp_df['TR'] = temp_df.max(axis = 1)
#temp_df.drop(['HL','HC','LC'], axis=1,inplace=True)
temp_df['High'] = df['High']
temp_df['Low'] = df['Low']
temp_df['Close'] = df['Close']
temp_df = temp_df[['High','Low','Close','TR']]
temp_df['PDM'] = df['High']-df['High'].shift(1)
temp_df['NDM'] = df['Low'] - df['Low'].shift(1)
PDM = temp_df['PDM']
NDM = temp_df['NDM']
PDM[PDM<0]=0
NDM[NDM<0]=0
PDM[PDM<NDM]=0
NDM[NDM<PDM]=0
TR = temp_df['TR']
period_TR = TR[1:].rolling(window=days).sum()
period_PDM = PDM.rolling(window=days).sum()
period_NDM = NDM.rolling(window=days).sum()
PDI = (period_PDM/period_TR)*100
NDI = (period_NDM/period_TR)*100
Diff = abs(PDI-NDI)
Sum = PDI+NDI
DX = Diff/Sum*100
ADX = DX.rolling(window = days).mean()
temp_df['TR14']= period_TR
temp_df['PDM14']= period_PDM
temp_df['NDM14']= period_NDM
temp_df['PDI14']=PDI
temp_df['NDI14']=NDI
temp_df['Diff'] = Diff
temp_df['Sum'] = Sum
temp_df['DX']=DX
temp_df['ADX']=ADX
df['PDI{}'.format(days)] = PDI
df['NDI{}'.format(days)] = NDI
df['ADX{}'.format(days)] = ADX
return temp_df
def ADX(df, days = 14):
HL = df['High'] - df['Low']
HC = abs(df['High'] - df['Close'].shift(1))
LC = abs(df['Low'] - df['Close'].shift(1))
temp_df = pd.DataFrame(index = df.index)
temp_df['HL'] = HL
temp_df['HC'] = HC
temp_df['LC'] = LC
temp_df['TR'] = temp_df.max(axis = 1)
temp_df['PDM'] = df['High']-df['High'].shift(1)
temp_df['NDM'] = df['Low'] - df['Low'].shift(1)
PDM = temp_df['PDM']
NDM = temp_df['NDM']
PDM[PDM<0]=0
NDM[NDM<0]=0
PDM[PDM<NDM]=0
NDM[NDM<PDM]=0
TR = temp_df['TR']
period_TR = TR[1:].rolling(window=days).sum()
period_PDM = PDM.rolling(window=days).sum()
period_NDM = NDM.rolling(window=days).sum()
PDI = (period_PDM/period_TR)*100
NDI = (period_NDM/period_TR)*100
Diff = abs(PDI-NDI)
Sum = PDI+NDI
DX = Diff/Sum*100
ADX = DX.rolling(window = days).mean()
df['PDI{}'.format(days)] = PDI
df['NDI{}'.format(days)] = NDI
df['ADX{}'.format(days)] = ADX
return df
def priceUpDown(df, days = 1):
close = df['Adj Close']
if days == 1:
diff = (close.shift(-1)-close)
else:
diff = close.rolling(window = days).mean().shift(-days) - close
UpDown = diff.map(lambda x: 1 if x>0 else -1)
df['UpDown{}'.format(days)] = UpDown
return df
def buildFeatures(ticker):
df = getStock(ticker)
if df is not None:
df = SMA(df,3)
df = EMA(df,6)
df = EMA(df,12)
df = MACD(df)
df = Bollinger_bands(df)
df = RSI(df,6)
df = RSI(df,12)
df = Momentum(df,1)
df = Momentum(df,3)
df = RateOfChange(df,3)
df = RateOfChange(df,12)
df = CCI(df,12)
df = CCI(df,20)
df = WILLR(df)
df = ATR(df)
df = TEMA(df,6)
df = OBV(df,dt.date(1999,12,31))
df = MFI(df)
df = ADX(df,14)
df = ADX(df,20)
df = priceUpDown(df,1)
df = priceUpDown(df,3)
df = priceUpDown(df,5)
df = priceUpDown(df,7)
df = priceUpDown(df,10)
df = priceUpDown(df,15)
df = priceUpDown(df, 30)
return df
def generateIndicatorDf(tickers = companyAndIndexList):
path = 'stockIndicator_dfs\{}.csv'
if not os.path.exists('stockIndicator_dfs'):
os.makedirs('stockIndicator_dfs')
for ticker in tickers:
if os.path.exists(path.format(ticker)):
continue
else:
try:
df = buildFeatures(ticker)
except Exception as e:
print(e)
print('Ticker{} generate failed'.format(ticker))
if df is not None:
df.to_csv(path.format(ticker))
#get_data_from_yahoo()
#generateIndicatorDf()