-
Notifications
You must be signed in to change notification settings - Fork 5
/
stock-forecast_tweet.py
446 lines (338 loc) · 16.7 KB
/
stock-forecast_tweet.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import sys, csv, json
import requests
from newsapi.articles import Articles
from newsapi.sources import Sources
import numpy as np
import csv, json
import pandas as pd
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import subjectivity
from nltk.sentiment import SentimentAnalyzer
from nltk.sentiment.util import *
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import unicodedata
import math
import h5py
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.python.framework import ops
#data collection
key = '96af62a035db45bda517a9ca62a25ac3'
a = Articles(API_KEY=key)
s = Sources(API_KEY=key)
#Creating 2 exception
#1 for API
#1 for Timeframe for the news
class APIKeyException(Exception):
def __init__(self, message):
self.message = message
class InvalidQueryException(Exception):
def __init__(self, message):
self.message = message
#Initializes the ArchiveAPI class to downlaod data to json file
#Raises an exception if no API key is given.
#param key: New York Times API Key
class ArchiveAPI(object):
def __init__(self, key=None):
self.key = key
self.root = 'http://api.nytimes.com/svc/archive/v1/{}/{}.json?api-key={}'
if not self.key:
nyt_dev_page = 'http://developer.nytimes.com/docs/reference/keys'
exception_str = 'Warning: API Key required. Please visit {}'
raise NoAPIKeyException(exception_str.format(nyt_dev_page))
def query(self, year=None, month=None, key=None,):
"""
Calls the archive API and returns the results as a dictionary.
:param key: Defaults to the API key used to initialize the ArchiveAPI class.
"""
if not key:
key = self.key
if (year < 1882) or not (0 < month < 13):
# currently the Archive API only supports year >= 1882
exception_str = 'Invalid query: See http://developer.nytimes.com/archive_api.json'
raise InvalidQueryException(exception_str)
url = self.root.format(year, month, key)
r = requests.get(url)
return r.json()
api = ArchiveAPI('0ba6dc04a8cb44e0a890c00df88c393a')
years = [2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007]
months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
for year in years:
for month in months:
mydict = api.query(year, month)
file_str = '/Users/user/Desktop/stock_rnn_data/' + str(year) + '-' + '{:02}'.format(month) + '.json'
with open(file_str, 'w') as fout:
try:
json.dump(mydict, fout)
except:
pass
fout.close()
#preparing data with open('/Users/user/Desktop/stock_rnn_data/DJIA_indices_data.csv', 'r',encoding="utf-8") as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
# Converting the csv file reader to a lists
data_list = list(spamreader)
#separating header from data
header = data_list[0]
data_list = data_list[1:]
data_list = np.asarray(data_list)
selected_data = data_list[:, [0, 4, 6]]
#dataframe index=date
df = pd.DataFrame(data=selected_data[0:,1:],
index=selected_data[0:,0],
columns=['close', 'adj close'],
dtype='float64')
print (df.tail())
#Interpolating data
f1 = df
idx = pd.date_range('12-29-2006', '12-31-2016')
df1.index = pd.DatetimeIndex(df1.index)
df1 = df1.reindex(idx, fill_value=np.NaN)
# df1.count() # gives 2518 count
interpolated_df = df1.interpolate() # Fill in the gap
interpolated_df.count() # gives 3651 count
print (df1.head(25))
# Removing extra date rows added in data for calculating interpolation
interpolated_df = interpolated_df[3:]
print (interpolated_df.head())
#Merging NYTimes data
#Function to parse and convert date format
#Try 2 formats for date or raise error
date_format = ["%Y-%m-%dT%H:%M:%SZ", "%Y-%m-%dT%H:%M:%S+%f"]
def try_parsing_date(text):
for fmt in date_format:
try:
return datetime.strptime(text, fmt).strftime('%Y-%m-%d')
except ValueError:
pass
raise ValueError('no valid date format found')
years = [2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007]
months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
dict_keys = ['pub_date', 'headline'] #, 'lead_paragraph']
articles_dict = dict.fromkeys(dict_keys)
#Filtering to read only the following news
# Filtering list for type_of_material
type_of_material_list = ['blog', 'brief', 'news', 'editorial', 'op-ed', 'list','analysis']
# Filtering list for section_name
section_name_list = ['business', 'national', 'world', 'u.s.' , 'politics', 'opinion', 'tech', 'science', 'health']
news_desk_list = ['business', 'national', 'world', 'u.s.' , 'politics', 'opinion', 'tech', 'science', 'health', 'foreign']
current_date = '2016-10-01'
from datetime import datetime
current_article_str = ''
#Adding article column to dataframe
interpolated_df["articles"] = ''
count_articles_filtered = 0
count_total_articles = 0
count_main_not_exist = 0
count_unicode_error = 0
count_attribute_error = 0
for year in years: # search for every month
for month in months:
file_str = '/Users/user/Desktop/stock_rnn_data/' + str(year) + '-' + '{:02}'.format(month) + '.json'
with open(file_str) as data_file:
NYTimes_data = json.load(data_file)
count_total_articles = count_total_articles + len(NYTimes_data["response"]["docs"][:]) #add article number
for i in range(len(NYTimes_data["response"]["docs"][:])): # search in every docs for type of material or section = in the list
try:
if any(substring in NYTimes_data["response"]["docs"][:][i]['type_of_material'].lower() for substring in type_of_material_list):
if any(substring in NYTimes_data["response"]["docs"][:][i]['section_name'].lower() for substring in section_name_list):
#count += 1
count_articles_filtered += 1
#print 'i: ' + str(i) dick_key = ['pub_date', 'headline']
articles_dict = { your_key: NYTimes_data["response"]["docs"][:][i][your_key] for your_key in dict_keys }
articles_dict['headline'] = articles_dict['headline']['main'] # Selecting just 'main' from headline
#articles_dict['headline'] = articles_dict['lead_paragraph'] # Selecting lead_paragraph
date = try_parsing_date(articles_dict['pub_date'])
#print 'article_dict: ' + articles_dict['headline']
# putting same day article str into one str
if date == current_date:
current_article_str = current_article_str + '. ' + articles_dict['headline']
else:
interpolated_df.set_value(current_date, 'articles', interpolated_df.loc[current_date, 'articles'] + '. ' + current_article_str)
current_date = date
#interpolated_df.set_value(date, 'articles', current_article_str)
#print str(date) + current_article_str
current_article_str = articles_dict['headline']
# For last condition in a year
if (date == current_date) and (i == len(NYTimes_data["response"]["docs"][:]) - 1):
interpolated_df.set_value(date, 'articles', current_article_str)
#Exception for section_name or type_of_material absent
except AttributeError:
#print 'attribute error'
#print NYTimes_data["response"]["docs"][:][i]
count_attribute_error += 1
# If article matches news_desk_list if none section_name found
try:
if any(substring in NYTimes_data["response"]["docs"][:][i]['news_desk'].lower() for substring in news_desk_list):
#count += 1
count_articles_filtered += 1
#print 'i: ' + str(i)
articles_dict = { your_key: NYTimes_data["response"]["docs"][:][i][your_key] for your_key in dict_keys }
articles_dict['headline'] = articles_dict['headline']['main'] # Selecting just 'main' from headline
#articles_dict['headline'] = articles_dict['lead_paragraph'] # Selecting lead_paragraph
date = try_parsing_date(articles_dict['pub_date'])
#print 'article_dict: ' + articles_dict['headline']
if date == current_date:
current_article_str = current_article_str + '. ' + articles_dict['headline']
else:
interpolated_df.set_value(current_date, 'articles', interpolated_df.loc[current_date, 'articles'] + '. ' + current_article_str)
current_date = date
#interpolated_df.set_value(date, 'articles', current_article_str)
#print str(date) + current_article_str
current_article_str = articles_dict['headline']
# For last condition in a year
if (date == current_date) and (i == len(NYTimes_data["response"]["docs"][:]) - 1):
interpolated_df.set_value(date, 'articles', current_article_str)
except AttributeError:
pass
pass
except KeyError:
print ('key error')
#print NYTimes_data["response"]["docs"][:][i]
count_main_not_exist += 1
pass
except TypeError:
print ("type error")
#print NYTimes_data["response"]["docs"][:][i]
count_main_not_exist += 1
pass
# Saving the data as pickle file
interpolated_df.to_pickle('/Users/user/Desktop/stock_rnn_data/pickled_ten_year_filtered_lead_para.pkl')
# Save pandas frame in csv form
interpolated_df.to_csv('/Users/user/Desktop/stock_rnn_data/sample_interpolated_df_10_years_filtered_lead_para.csv',
sep='\t', encoding='utf-8')
# Reading the data as pickle file
dataframe_read = pd.read_pickle('/Users/user/Desktop/stock_rnn_data/pickled_ten_year_filtered_lead_para.pkl')
#deep neural network
df_stocks = pd.read_pickle('/Users/user/Desktop/stock_rnn_data/pickled_ten_year_filtered_lead_para.pkl')
print (df_stocks.head())
df_stocks['prices'] = df_stocks['adj close'].apply(np.int64)
# selecting the prices and articles
df_stocks = df_stocks[['prices', 'articles']]
df_stocks.head()
df_stocks['articles'] = df_stocks['articles'].map(lambda x: x.lstrip('.-'))
df_stocks.head()
df = df_stocks[['prices']].copy()
df.head()
#new features
df["compound"] = ''
df["neg"] = ''
df["neu"] = ''
df["pos"] = ''
df.head()
df_stocks.T
nltk.download()
#unicodedata.normalize = Return the normal form form for the Unicode string unistr.
sid = SentimentIntensityAnalyzer()
for date, row in df_stocks.T.iteritems():
try:
sentence = unicodedata.normalize('NFKD', df_stocks.loc[date, 'articles'])
ss = sid.polarity_scores(sentence)
df.set_value(date, 'compound', ss['compound'])
df.set_value(date, 'neg', ss['neg'])
df.set_value(date, 'neu', ss['neu'])
df.set_value(date, 'pos', ss['pos'])
except TypeError:
print (df_stocks.loc[date, 'articles'])
print (date)
df.head()
datasetNorm = (df - df.mean()) / (df.max() - df.min())
datasetNorm.reset_index(inplace=True)
del datasetNorm['index']
datasetNorm['next_prices'] = datasetNorm['prices'].shift(-1)
datasetNorm.head(5)
#hyperparameter
num_epochs = 1000
batch_size = 1
total_series_length = len(datasetNorm.index)
truncated_backprop_length = 3 #The size of the sequence
state_size = 12 #The number of neurons
num_features = 4
num_classes = 1 #[1,0]
num_batches = total_series_length//batch_size//truncated_backprop_length
min_test_size = 100
print('The total series length is: %d' %total_series_length)
print('The current configuration gives us %d batches of %d observations each one looking %d steps in the past'
%(num_batches,batch_size,truncated_backprop_length))
#The total series length is: 3653
#The current configuration gives us 1217 batches of 1 observations each one looking 3 steps in the past
#Train-Test split
datasetTrain = datasetNorm[datasetNorm.index < num_batches*batch_size*truncated_backprop_length]
for i in range(min_test_size,len(datasetNorm.index)):
if(i % truncated_backprop_length*batch_size == 0):
test_first_idx = len(datasetNorm.index)-i
break
datasetTest = datasetNorm[datasetNorm.index >= test_first_idx]
xTrain = datasetTrain[['prices','neu','neg','pos']].as_matrix()
yTrain = datasetTrain['next_prices'].as_matrix()
xTrain.shape
xTest = datasetTest[['prices','neu','neg','pos']].as_matrix()
yTest = datasetTest['next_prices'].as_matrix()
yTest.shape
#visualize
plt.figure(figsize=(25,5))
plt.plot(xTrain[:,0])
plt.title('Train (' +str(len(xTrain))+' data points)')
plt.show()
plt.figure(figsize=(10,3))
plt.plot(xTest[:,0])
plt.title('Test (' +str(len(xTest))+' data points)')
plt.show()
#placeholders
tf.reset_default_graph()
batchX_placeholder = tf.placeholder(dtype=tf.float32,shape=[None,truncated_backprop_length,num_features],name='data_ph')
batchY_placeholder = tf.placeholder(dtype=tf.float32,shape=[None,truncated_backprop_length,num_classes],name='target_ph')
#Weights and biases
#Because is a 3 layer net:
#Input
#Hidden Recurrent layer
#Output
#We need 2 pairs of W and b
W2 = tf.Variable(initial_value=np.random.rand(state_size,num_classes),dtype=tf.float32)
b2 = tf.Variable(initial_value=np.random.rand(1,num_classes),dtype=tf.float32)
#unpack
labels_series = tf.unstack(batchY_placeholder, axis=1)
cell = tf.contrib.rnn.BasicLSTMCell(num_units=state_size)
states_series, current_state = tf.nn.dynamic_rnn(cell=cell,inputs=batchX_placeholder,dtype=tf.float32)
states_series = tf.transpose(states_series,[1,0,2])
last_state = tf.gather(params=states_series,indices=states_series.get_shape()[0]-1)
last_label = tf.gather(params=labels_series,indices=len(labels_series)-1)
#Backward pass - Output
weight = tf.Variable(tf.truncated_normal([state_size,num_classes]))
bias = tf.Variable(tf.constant(0.1,shape=[num_classes]))
prediction = tf.matmul(last_state,weight) + bias
prediction
loss = tf.reduce_mean(tf.squared_difference(last_label,prediction))
train_step = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
loss_list = []
test_pred_list = []
with tf.Session() as sess:
tf.global_variables_initializer().run()
for epoch_idx in range(num_epochs):
print('Epoch %d' %epoch_idx)
for batch_idx in range(num_batches):
start_idx = batch_idx * truncated_backprop_length
end_idx = start_idx + truncated_backprop_length * batch_size
batchX = xTrain[start_idx:end_idx,:].reshape(batch_size,truncated_backprop_length,num_features)
batchY = yTrain[start_idx:end_idx].reshape(batch_size,truncated_backprop_length,1)
#print('IDXs',start_idx,end_idx)
#print('X',batchX.shape,batchX)
#print('Y',batchX.shape,batchY)
feed = {batchX_placeholder : batchX, batchY_placeholder : batchY}
#TRAIN!
_loss,_train_step,_pred,_last_label,_prediction = sess.run(
fetches=[loss,train_step,prediction,last_label,prediction],
feed_dict = feed
)
loss_list.append(_loss)
if(batch_idx % 50 == 0):
print('Step %d - Loss: %.6f' %(batch_idx,_loss))
#TEST
for test_idx in range(len(xTest) - truncated_backprop_length):
testBatchX = xTest[test_idx:test_idx+truncated_backprop_length,:].reshape((1,truncated_backprop_length,num_features))
testBatchY = yTest[test_idx:test_idx+truncated_backprop_length].reshape((1,truncated_backprop_length,1))
#_current_state = np.zeros((batch_size,state_size))
feed = {batchX_placeholder : testBatchX,
batchY_placeholder : testBatchY}
#Test_pred contains 'window_size' predictions, we want the last one
_last_state,_last_label,test_pred = sess.run([last_state,last_label,prediction],feed_dict=feed)
test_pred_list.append(test_pred[-1][0]) #The last one