-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.py
372 lines (292 loc) · 11.2 KB
/
app.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
# import necessary libraries
import os
import numpy as np
import datetime
import psycopg2
from sqlalchemy import cast, Date
import csv
from flask import (
Flask,
render_template,
jsonify,
request,
redirect)
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import func
from sqlalchemy.orm import aliased
# Postgres database user and password import
from db_key import user, password
#################################################
# Flask Setup
#################################################
app = Flask(__name__)
# create route that renders index.html template
@app.route("/")
def home():
return render_template("index.html");
# end home() route
#################################################
# add the routes for the dashboard
@app.route("/dashboard")
def dashboard():
return render_template("dashboard.html");
## end dashboard() route
#################################################
# add the routes for the NLP
@app.route("/nlp")
def NLP():
return render_template("nlp.html");
## end NLP() route
#################################################
# add the routes for the Forecast
@app.route("/forecast")
def Forecast():
return render_template("forecast.html");
## end Forecast() route
#################################################
# add the routes for the Design
@app.route("/design")
def Design():
return render_template("design.html");
## end Design() route
# add the routes for the Design
@app.route("/test")
def test():
return render_template("index2.html");
## end Design() route
#################################################
# the api retrieves the companies list
@app.route("/api/GetCompanyList")
def GetCompanyList():
with open('Resources/input/companylist.csv') as csv_file:
data = csv.reader(csv_file, delimiter=',');
first_line = True
company_data = [];
for row in data:
if not first_line:
dict = {
"Ticker": row[0],
"Company": row[1]
}
company_data.append (dict)
else:
first_line = False
return jsonify(company_data)
# end company_data() route
#################################################
# the api retrieves the LSTM data from the csv sends the json values
@app.route("/api/GetLSTMData/<ticker>")
def GetLSTMData(ticker):
with open('Resources/output/LSTMFinal.csv') as csv_file:
data = csv.reader(csv_file, delimiter=',');
first_line = True
lstm_data = [];
results = filter(lambda row: row[0] == ticker, data)
for row in results:
if not first_line:
dict = {
"Ticker": row[0],
"Date": row[1],
"Actual": row[2],
"Pred": row[3],
"PredLag" : row[4]
}
lstm_data.append (dict)
else:
first_line = False
return jsonify(lstm_data)
# end lstm_data() route
#################################################
# the api retrieves the LSTM data from the csv sends the json values
@app.route("/api/GetGRUData/<ticker>")
def GetGRUData(ticker):
with open('Resources/output/GRUFinal.csv') as csv_file:
data = csv.reader(csv_file, delimiter=',');
first_line = True
lstm_data = [];
results = filter(lambda row: row[0] == ticker, data)
for row in results:
if not first_line:
dict = {
"Ticker": row[0],
"Date": row[1],
"Actual": row[2],
"Pred": row[3],
"PredLag" : row[4]
}
lstm_data.append (dict)
else:
first_line = False
return jsonify(lstm_data)
# end GRU_data() route
#################################################
# the api retrieves the data and calculates the averages for stock
@app.route("/api/GetRollingAverages/<ticker>")
def GetRollingAverages(ticker):
import pandas as pd;
# For time stamps
from datetime import datetime, timedelta;
print(ticker);
import math;
with open('Resources/combined_top_25.csv') as csv_file:
data = csv.reader(csv_file, delimiter=',');
lstm_data = [];
results = filter(lambda row: (row[1] == ticker) , data);
df = pd.DataFrame(results);
exp1 = df[3].ewm(span=20, adjust=False).mean();
rolling_mean = df[3].rolling(window=20).mean();
df[2] = df[2].astype(float)
changepercent = df[2].pct_change();
df['rolling_mean'] = rolling_mean;
df['exp1'] = exp1;
df['EReturn'] = changepercent;
df['mean'] = changepercent.mean();
df['std'] = changepercent.std();
df = df.dropna();
for i, row in df.iterrows():
dict = {
"Ticker": row[1],
"Date": row[0],
"Close": row[3],
"MA": row['rolling_mean'],
"EMA" : row['exp1'] ,
"ER" : row['EReturn'],
"mean" : row['mean'],
"std" : row ['std']
}
lstm_data.append (dict)
return jsonify(lstm_data)
# end GRU_data() route
#################################################
# the api retrieves the data and calculates the MSE,RMSE,SI for stock
@app.route("/api/GetMetrics/<ticker>")
def GetMetrics(ticker):
import pandas as pd;
# For time stamps
from datetime import datetime, timedelta;
print(ticker);
import math;
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
import sklearn.metrics as metrics
# Importing the statistics module
import statistics
#define function to calculate cv
cv = lambda x: np.std(x, ddof=1) / np.mean(x) * 100;
# calculate performance statistics for GRU
with open('Resources/output/GRUFinal.csv') as csv_file:
data = csv.reader(csv_file, delimiter=',');
first_line = True
metric_data = [];
results = filter(lambda row: row[0] == ticker, data)
df = pd.DataFrame(results);
#print(df)
df[2] = df[2].astype(float)
df[4] = df[4].replace(r'^\s*$', 0, regex=True)
df[4] = df[4].astype(float)
df[3] = df[3].replace(r'^\s*$', 0, regex=True)
df[3] = df[3].astype(float);
gruMSE = mean_squared_error(df[2], df[3]);
gruRMSE = math.sqrt(gruMSE);
gruNRMSE = gruRMSE /(statistics.mean(df[2]));
gruMAE = mean_absolute_error(df[2], df[3]);
gruR2 = metrics.r2_score(df[2],df[3]);
gruMSELag = mean_squared_error(df[2], df[4]);
gruRMSELag = math.sqrt(gruMSELag);
gruNRMSELag = gruRMSELag /(statistics.mean(df[2]));
gruMAELag = mean_absolute_error(df[2], df[4]);
gruR2Lag = metrics.r2_score(df[2],df[4]);
# calculate performance statistics for LSTM
with open('Resources/output/LSTMFinal.csv') as csv_file:
data = csv.reader(csv_file, delimiter=',');
first_line = True
metric_data = [];
results = filter(lambda row: row[0] == ticker, data)
df = pd.DataFrame(results);
#print(df)
df[2] = df[2].astype(float)
df[4] = df[4].replace(r'^\s*$', 0, regex=True)
df[4] = df[4].astype(float)
df[3] = df[3].replace(r'^\s*$', 0, regex=True)
df[3] = df[3].astype(float);
lstmMSE = mean_squared_error(df[2], df[3]);
lstmRMSE = math.sqrt(lstmMSE);
lstmNRMSE = lstmRMSE /(statistics.mean(df[2]));
lstmMAE = mean_absolute_error(df[2], df[3]);
lstmR2 = metrics.r2_score(df[2],df[3]);
lstmMSELag = mean_squared_error(df[2], df[4]);
lstmRMSELag = math.sqrt(lstmMSELag);
lstmNRMSELag = lstmRMSELag /(statistics.mean(df[2]));
lstmMAELag = mean_absolute_error(df[2], df[4]);
lstmR2Lag = metrics.r2_score(df[2],df[4]);
#build data model
statDict = {
'lstmMSE' : str("{0:.4f}".format(lstmMSE)),
'lstmRMSE' : str("{0:.4f}".format(lstmRMSE)),
'lstmNRMSE' : str("{0:.4f}".format(lstmNRMSE)),
'lstmMAE' : str("{0:.4f}".format(lstmMAE)),
'lstmR2' : str("{0:.4f}".format(lstmR2)),
'lstmMSELag' : str("{0:.4f}".format(lstmMSELag)),
'lstmRMSELag' : str("{0:.4f}".format(lstmRMSELag)),
'lstmNRMSELag' : str("{0:.4f}".format(lstmNRMSELag)),
'lstmMAELag' : str("{0:.4f}".format(lstmMAELag)),
'lstmR2Lag' : str("{0:.4f}".format(lstmR2Lag)),
'gruMSE' : str("{0:.4f}".format(gruMSE)),
'gruRMSE' : str("{0:.4f}".format(gruRMSE)),
'gruNRMSE' : str("{0:.4f}".format(gruNRMSE)),
'gruMAE' : str("{0:.4f}".format(gruMAE)),
'gruR2' : str("{0:.4f}".format(gruR2)),
'gruMSELag' : str("{0:.4f}".format(gruMSELag)),
'gruRMSELag' : str("{0:.4f}".format(gruRMSELag)),
'gruNRMSELag' : str("{0:.4f}".format(gruNRMSELag)),
'gruMAELag' : str("{0:.4f}".format(gruMAELag)),
'gruR2Lag' : str("{0:.4f}".format(gruR2Lag))
}
return jsonify(statDict)
return jsonify(metric_data)
# end GetMetrics() route
#################################################
# the api retrieves the stock data from the web and stores them in csv
@app.route("/api/GetStockData")
def GetStockData():
import pandas as pd;
from pprint import pprint;
with open('Resources/today.csv') as csv_file:
data = csv.reader(csv_file, delimiter=',');
print('first')
first_line = True
stock_overview_data = [];
#results = filter(lambda row: row[1] == ticker, data)
for row in data:
if not first_line:
print('inside')
df = pd.DataFrame({
"ticker" : row[1],
"52 Week Range" : row[6],
"Market Cap" : row[7],
"PE Ratio" : row[8],
"EPS" : row[9],
"Earnings Date": row[10] ,
"Dividend Rate" : row[11]
}, index=[0]);
metrics_fact_table = df.to_html( classes="table table-striped")
dict = {
"Ticker": row[1],
"Name": row[2],
"Open": row[3],
"Close": row[4],
"Volume" : row[5],
"Metrics" : metrics_fact_table,
"ShortTerm" :row[12],
"MiddleTerm" :row[13],
"LongTerm" : row[14]
}
stock_overview_data.append (dict)
else:
first_line = False
return jsonify(stock_overview_data)
# end stock_overview_data() route
#################################################
if __name__ == "__main__":
app.run(debug=True)
#################################################