-
Notifications
You must be signed in to change notification settings - Fork 214
/
main.py
511 lines (425 loc) · 20.8 KB
/
main.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 27 14:36:49 2019
@author: Kaushik
"""
#**************** IMPORT PACKAGES ********************
from flask import Flask, render_template, request, flash, redirect, url_for
from alpha_vantage.timeseries import TimeSeries
import pandas as pd
import numpy as np
from statsmodels.tsa.arima.model import ARIMA
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import math, random
from datetime import datetime
import datetime as dt
import yfinance as yf
import tweepy
import preprocessor as p
import re
from sklearn.linear_model import LinearRegression
from textblob import TextBlob
import constants as ct
from Tweet import Tweet
import nltk
nltk.download('punkt')
# Ignore Warnings
import warnings
warnings.filterwarnings("ignore")
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
#***************** FLASK *****************************
app = Flask(__name__)
#To control caching so as to save and retrieve plot figs on client side
@app.after_request
def add_header(response):
response.headers['Pragma'] = 'no-cache'
response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
response.headers['Expires'] = '0'
return response
@app.route('/')
def index():
return render_template('index.html')
@app.route('/insertintotable',methods = ['POST'])
def insertintotable():
nm = request.form['nm']
#**************** FUNCTIONS TO FETCH DATA ***************************
def get_historical(quote):
end = datetime.now()
start = datetime(end.year-2,end.month,end.day)
data = yf.download(quote, start=start, end=end)
df = pd.DataFrame(data=data)
df.to_csv(''+quote+'.csv')
if(df.empty):
ts = TimeSeries(key='N6A6QT6IBFJOPJ70',output_format='pandas')
data, meta_data = ts.get_daily_adjusted(symbol='NSE:'+quote, outputsize='full')
#Format df
#Last 2 yrs rows => 502, in ascending order => ::-1
data=data.head(503).iloc[::-1]
data=data.reset_index()
#Keep Required cols only
df=pd.DataFrame()
df['Date']=data['date']
df['Open']=data['1. open']
df['High']=data['2. high']
df['Low']=data['3. low']
df['Close']=data['4. close']
df['Adj Close']=data['5. adjusted close']
df['Volume']=data['6. volume']
df.to_csv(''+quote+'.csv',index=False)
return
#******************** ARIMA SECTION ********************
def ARIMA_ALGO(df):
uniqueVals = df["Code"].unique()
len(uniqueVals)
df=df.set_index("Code")
#for daily basis
def parser(x):
return datetime.strptime(x, '%Y-%m-%d')
def arima_model(train, test):
history = [x for x in train]
predictions = list()
for t in range(len(test)):
model = ARIMA(history, order=(6,1 ,0))
model_fit = model.fit()
output = model_fit.forecast()
yhat = output[0]
predictions.append(yhat)
obs = test[t]
history.append(obs)
return predictions
for company in uniqueVals[:10]:
data=(df.loc[company,:]).reset_index()
data['Price'] = data['Close']
Quantity_date = data[['Price','Date']]
Quantity_date.index = Quantity_date['Date'].map(lambda x: parser(x))
Quantity_date['Price'] = Quantity_date['Price'].map(lambda x: float(x))
Quantity_date = Quantity_date.fillna(Quantity_date.bfill())
Quantity_date = Quantity_date.drop(['Date'],axis =1)
fig = plt.figure(figsize=(7.2,4.8),dpi=65)
plt.plot(Quantity_date)
plt.savefig('static/Trends.png')
plt.close(fig)
quantity = Quantity_date.values
size = int(len(quantity) * 0.80)
train, test = quantity[0:size], quantity[size:len(quantity)]
#fit in model
predictions = arima_model(train, test)
#plot graph
fig = plt.figure(figsize=(7.2,4.8),dpi=65)
plt.plot(test,label='Actual Price')
plt.plot(predictions,label='Predicted Price')
plt.legend(loc=4)
plt.savefig('static/ARIMA.png')
plt.close(fig)
print()
print("##############################################################################")
arima_pred=predictions[-2]
print("Tomorrow's",quote," Closing Price Prediction by ARIMA:",arima_pred)
#rmse calculation
error_arima = math.sqrt(mean_squared_error(test, predictions))
print("ARIMA RMSE:",error_arima)
print("##############################################################################")
return arima_pred, error_arima
#************* LSTM SECTION **********************
def LSTM_ALGO(df):
#Split data into training set and test set
dataset_train=df.iloc[0:int(0.8*len(df)),:]
dataset_test=df.iloc[int(0.8*len(df)):,:]
############# NOTE #################
#TO PREDICT STOCK PRICES OF NEXT N DAYS, STORE PREVIOUS N DAYS IN MEMORY WHILE TRAINING
# HERE N=7
###dataset_train=pd.read_csv('Google_Stock_Price_Train.csv')
training_set=df.iloc[:,4:5].values# 1:2, to store as numpy array else Series obj will be stored
#select cols using above manner to select as float64 type, view in var explorer
#Feature Scaling
from sklearn.preprocessing import MinMaxScaler
sc=MinMaxScaler(feature_range=(0,1))#Scaled values btween 0,1
training_set_scaled=sc.fit_transform(training_set)
#In scaling, fit_transform for training, transform for test
#Creating data stucture with 7 timesteps and 1 output.
#7 timesteps meaning storing trends from 7 days before current day to predict 1 next output
X_train=[]#memory with 7 days from day i
y_train=[]#day i
for i in range(7,len(training_set_scaled)):
X_train.append(training_set_scaled[i-7:i,0])
y_train.append(training_set_scaled[i,0])
#Convert list to numpy arrays
X_train=np.array(X_train)
y_train=np.array(y_train)
X_forecast=np.array(X_train[-1,1:])
X_forecast=np.append(X_forecast,y_train[-1])
#Reshaping: Adding 3rd dimension
X_train=np.reshape(X_train, (X_train.shape[0],X_train.shape[1],1))#.shape 0=row,1=col
X_forecast=np.reshape(X_forecast, (1,X_forecast.shape[0],1))
#For X_train=np.reshape(no. of rows/samples, timesteps, no. of cols/features)
#Building RNN
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
#Initialise RNN
regressor=Sequential()
#Add first LSTM layer
regressor.add(LSTM(units=50,return_sequences=True,input_shape=(X_train.shape[1],1)))
#units=no. of neurons in layer
#input_shape=(timesteps,no. of cols/features)
#return_seq=True for sending recc memory. For last layer, retrun_seq=False since end of the line
regressor.add(Dropout(0.1))
#Add 2nd LSTM layer
regressor.add(LSTM(units=50,return_sequences=True))
regressor.add(Dropout(0.1))
#Add 3rd LSTM layer
regressor.add(LSTM(units=50,return_sequences=True))
regressor.add(Dropout(0.1))
#Add 4th LSTM layer
regressor.add(LSTM(units=50))
regressor.add(Dropout(0.1))
#Add o/p layer
regressor.add(Dense(units=1))
#Compile
regressor.compile(optimizer='adam',loss='mean_squared_error')
#Training
regressor.fit(X_train,y_train,epochs=25,batch_size=32 )
#For lstm, batch_size=power of 2
#Testing
###dataset_test=pd.read_csv('Google_Stock_Price_Test.csv')
real_stock_price=dataset_test.iloc[:,4:5].values
#To predict, we need stock prices of 7 days before the test set
#So combine train and test set to get the entire data set
dataset_total=pd.concat((dataset_train['Close'],dataset_test['Close']),axis=0)
testing_set=dataset_total[ len(dataset_total) -len(dataset_test) -7: ].values
testing_set=testing_set.reshape(-1,1)
#-1=till last row, (-1,1)=>(80,1). otherwise only (80,0)
#Feature scaling
testing_set=sc.transform(testing_set)
#Create data structure
X_test=[]
for i in range(7,len(testing_set)):
X_test.append(testing_set[i-7:i,0])
#Convert list to numpy arrays
X_test=np.array(X_test)
#Reshaping: Adding 3rd dimension
X_test=np.reshape(X_test, (X_test.shape[0],X_test.shape[1],1))
#Testing Prediction
predicted_stock_price=regressor.predict(X_test)
#Getting original prices back from scaled values
predicted_stock_price=sc.inverse_transform(predicted_stock_price)
fig = plt.figure(figsize=(7.2,4.8),dpi=65)
plt.plot(real_stock_price,label='Actual Price')
plt.plot(predicted_stock_price,label='Predicted Price')
plt.legend(loc=4)
plt.savefig('static/LSTM.png')
plt.close(fig)
error_lstm = math.sqrt(mean_squared_error(real_stock_price, predicted_stock_price))
#Forecasting Prediction
forecasted_stock_price=regressor.predict(X_forecast)
#Getting original prices back from scaled values
forecasted_stock_price=sc.inverse_transform(forecasted_stock_price)
lstm_pred=forecasted_stock_price[0,0]
print()
print("##############################################################################")
print("Tomorrow's ",quote," Closing Price Prediction by LSTM: ",lstm_pred)
print("LSTM RMSE:",error_lstm)
print("##############################################################################")
return lstm_pred,error_lstm
#***************** LINEAR REGRESSION SECTION ******************
def LIN_REG_ALGO(df):
#No of days to be forcasted in future
forecast_out = int(7)
#Price after n days
df['Close after n days'] = df['Close'].shift(-forecast_out)
#New df with only relevant data
df_new=df[['Close','Close after n days']]
#Structure data for train, test & forecast
#lables of known data, discard last 35 rows
y =np.array(df_new.iloc[:-forecast_out,-1])
y=np.reshape(y, (-1,1))
#all cols of known data except lables, discard last 35 rows
X=np.array(df_new.iloc[:-forecast_out,0:-1])
#Unknown, X to be forecasted
X_to_be_forecasted=np.array(df_new.iloc[-forecast_out:,0:-1])
#Traning, testing to plot graphs, check accuracy
X_train=X[0:int(0.8*len(df)),:]
X_test=X[int(0.8*len(df)):,:]
y_train=y[0:int(0.8*len(df)),:]
y_test=y[int(0.8*len(df)):,:]
# Feature Scaling===Normalization
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
X_to_be_forecasted=sc.transform(X_to_be_forecasted)
#Training
clf = LinearRegression(n_jobs=-1)
clf.fit(X_train, y_train)
#Testing
y_test_pred=clf.predict(X_test)
y_test_pred=y_test_pred*(1.04)
import matplotlib.pyplot as plt2
fig = plt2.figure(figsize=(7.2,4.8),dpi=65)
plt2.plot(y_test,label='Actual Price' )
plt2.plot(y_test_pred,label='Predicted Price')
plt2.legend(loc=4)
plt2.savefig('static/LR.png')
plt2.close(fig)
error_lr = math.sqrt(mean_squared_error(y_test, y_test_pred))
#Forecasting
forecast_set = clf.predict(X_to_be_forecasted)
forecast_set=forecast_set*(1.04)
mean=forecast_set.mean()
lr_pred=forecast_set[0,0]
print()
print("##############################################################################")
print("Tomorrow's ",quote," Closing Price Prediction by Linear Regression: ",lr_pred)
print("Linear Regression RMSE:",error_lr)
print("##############################################################################")
return df, lr_pred, forecast_set, mean, error_lr
#**************** SENTIMENT ANALYSIS **************************
def retrieving_tweets_polarity(symbol):
stock_ticker_map = pd.read_csv('Yahoo-Finance-Ticker-Symbols.csv')
stock_full_form = stock_ticker_map[stock_ticker_map['Ticker']==symbol]
symbol = stock_full_form['Name'].to_list()[0][0:12]
auth = tweepy.OAuthHandler(ct.consumer_key, ct.consumer_secret)
auth.set_access_token(ct.access_token, ct.access_token_secret)
user = tweepy.API(auth)
tweets = tweepy.Cursor(user.search_tweets, q=symbol, tweet_mode='extended', lang='en',exclude_replies=True).items(ct.num_of_tweets)
tweet_list = [] #List of tweets alongside polarity
global_polarity = 0 #Polarity of all tweets === Sum of polarities of individual tweets
tw_list=[] #List of tweets only => to be displayed on web page
#Count Positive, Negative to plot pie chart
pos=0 #Num of pos tweets
neg=1 #Num of negative tweets
for tweet in tweets:
count=20 #Num of tweets to be displayed on web page
#Convert to Textblob format for assigning polarity
tw2 = tweet.full_text
tw = tweet.full_text
#Clean
tw=p.clean(tw)
#print("-------------------------------CLEANED TWEET-----------------------------")
#print(tw)
#Replace & by &
tw=re.sub('&','&',tw)
#Remove :
tw=re.sub(':','',tw)
#print("-------------------------------TWEET AFTER REGEX MATCHING-----------------------------")
#print(tw)
#Remove Emojis and Hindi Characters
tw=tw.encode('ascii', 'ignore').decode('ascii')
#print("-------------------------------TWEET AFTER REMOVING NON ASCII CHARS-----------------------------")
#print(tw)
blob = TextBlob(tw)
polarity = 0 #Polarity of single individual tweet
for sentence in blob.sentences:
polarity += sentence.sentiment.polarity
if polarity>0:
pos=pos+1
if polarity<0:
neg=neg+1
global_polarity += sentence.sentiment.polarity
if count > 0:
tw_list.append(tw2)
tweet_list.append(Tweet(tw, polarity))
count=count-1
if len(tweet_list) != 0:
global_polarity = global_polarity / len(tweet_list)
else:
global_polarity = global_polarity
neutral=ct.num_of_tweets-pos-neg
if neutral<0:
neg=neg+neutral
neutral=20
print()
print("##############################################################################")
print("Positive Tweets :",pos,"Negative Tweets :",neg,"Neutral Tweets :",neutral)
print("##############################################################################")
labels=['Positive','Negative','Neutral']
sizes = [pos,neg,neutral]
explode = (0, 0, 0)
fig = plt.figure(figsize=(7.2,4.8),dpi=65)
fig1, ax1 = plt.subplots(figsize=(7.2,4.8),dpi=65)
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', startangle=90)
# Equal aspect ratio ensures that pie is drawn as a circle
ax1.axis('equal')
plt.tight_layout()
plt.savefig('static/SA.png')
plt.close(fig)
#plt.show()
if global_polarity>0:
print()
print("##############################################################################")
print("Tweets Polarity: Overall Positive")
print("##############################################################################")
tw_pol="Overall Positive"
else:
print()
print("##############################################################################")
print("Tweets Polarity: Overall Negative")
print("##############################################################################")
tw_pol="Overall Negative"
return global_polarity,tw_list,tw_pol,pos,neg,neutral
def recommending(df, global_polarity,today_stock,mean):
if today_stock.iloc[-1]['Close'] < mean:
if global_polarity > 0:
idea="RISE"
decision="BUY"
print()
print("##############################################################################")
print("According to the ML Predictions and Sentiment Analysis of Tweets, a",idea,"in",quote,"stock is expected => ",decision)
elif global_polarity <= 0:
idea="FALL"
decision="SELL"
print()
print("##############################################################################")
print("According to the ML Predictions and Sentiment Analysis of Tweets, a",idea,"in",quote,"stock is expected => ",decision)
else:
idea="FALL"
decision="SELL"
print()
print("##############################################################################")
print("According to the ML Predictions and Sentiment Analysis of Tweets, a",idea,"in",quote,"stock is expected => ",decision)
return idea, decision
#**************GET DATA ***************************************
quote=nm
#Try-except to check if valid stock symbol
try:
get_historical(quote)
except:
return render_template('index.html',not_found=True)
else:
#************** PREPROCESSUNG ***********************
df = pd.read_csv(''+quote+'.csv')
print("##############################################################################")
print("Today's",quote,"Stock Data: ")
today_stock=df.iloc[-1:]
print(today_stock)
print("##############################################################################")
df = df.dropna()
code_list=[]
for i in range(0,len(df)):
code_list.append(quote)
df2=pd.DataFrame(code_list,columns=['Code'])
df2 = pd.concat([df2, df], axis=1)
df=df2
arima_pred, error_arima=ARIMA_ALGO(df)
lstm_pred, error_lstm=LSTM_ALGO(df)
df, lr_pred, forecast_set,mean,error_lr=LIN_REG_ALGO(df)
# Twitter Lookup is no longer free in Twitter's v2 API
# polarity,tw_list,tw_pol,pos,neg,neutral = retrieving_tweets_polarity(quote)
polarity, tw_list, tw_pol, pos, neg, neutral = 0, [], "Can't fetch tweets, Twitter Lookup is no longer free in API v2.", 0, 0, 0
idea, decision=recommending(df, polarity,today_stock,mean)
print()
print("Forecasted Prices for Next 7 days:")
print(forecast_set)
today_stock=today_stock.round(2)
return render_template('results.html',quote=quote,arima_pred=round(arima_pred,2),lstm_pred=round(lstm_pred,2),
lr_pred=round(lr_pred,2),open_s=today_stock['Open'].to_string(index=False),
close_s=today_stock['Close'].to_string(index=False),adj_close=today_stock['Adj Close'].to_string(index=False),
tw_list=tw_list,tw_pol=tw_pol,idea=idea,decision=decision,high_s=today_stock['High'].to_string(index=False),
low_s=today_stock['Low'].to_string(index=False),vol=today_stock['Volume'].to_string(index=False),
forecast_set=forecast_set,error_lr=round(error_lr,2),error_lstm=round(error_lstm,2),error_arima=round(error_arima,2))
if __name__ == '__main__':
app.run()