-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredictFrontend.py
174 lines (134 loc) · 5.04 KB
/
predictFrontend.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
#import packages
import pandas as pd
import numpy as np
import sys
import json
import argparse
import datetime as dt
import requests
from datetime import timedelta
#importing required libraries
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential, model_from_json, load_model
from keras.layers import Dense, Dropout, LSTM
#for normalizing data
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 1))
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
#lines = sys.stdin.readlines()
#ticker = json.loads(lines[0])
# ====================== Loading Data from Alpha Vantage ==================================
lines = sys.stdin.readlines()
ticker = json.loads(lines[0])
api_key = "LBG3YII29JOX2SSU"
#ticker = "AAPL"
# JSON file with all the stock market data for AAL from the last 20 years
url_string = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=%s&outputsize=full&apikey=%s"%(ticker,api_key)
# Save data to this file
file_to_save = 'stock_market_data-%s.csv'%ticker
# If you haven't already saved data,
# Go ahead and grab the data from the url
# And store date, low, high, volume, close, open values to a Pandas DataFrame
response = requests.get(url_string)
data = json.loads(response.text)
# extract stock market data
data = data['Time Series (Daily)']
df = pd.DataFrame(columns=['Date','Low','High','Close','Open'])
for k,v in data.items():
date = dt.datetime.strptime(k, '%Y-%m-%d')
data_row = [date.date(),float(v['3. low']),float(v['2. high']),
float(v['4. close']),float(v['1. open'])]
df.loc[-1,:] = data_row
df.index = df.index + 1
# If the data is already there, just load it from the CSV
#print the head
df.head()
#setting index as date
df['Date'] = pd.to_datetime(df.Date,format='%Y-%m-%d')
df.index = df['Date']
#plot
#plt.figure(figsize=(16,8))
#plt.plot(df['Close'], label='Close Price history')
#
#LSTM Model Start
#
#creating dataframe
data = df.sort_index(ascending=True, axis=0)
new_data = pd.DataFrame(index=range(0,len(df)),columns=['Date', 'Close'])
for i in range(0,len(data)):
new_data['Date'][i] = data['Date'][i]
new_data['Close'][i] = data['Close'][i]
#setting index
new_data.index = new_data.Date
new_data.drop('Date', axis=1, inplace=True)
#creating train and test sets
dataset = new_data.values
train = dataset[len(dataset)-3000:,:]
valid = dataset[987:,:]
#converting dataset into x_train and y_train
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(dataset)
x_train, y_train = [], []
for i in range(60,len(train)):
x_train.append(scaled_data[i-60:i,0])
y_train.append(scaled_data[i,0])
x_train, y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0],x_train.shape[1],1))
file_name = 'model_%s.h5'%ticker
if(os.path.isfile(file_name) is False):
# create and fit the LSTM network
print(json.dumps("Error: Model has not been trained yet."))
else:
train = dataset[:,:]
model = load_model(file_name)
#predicting test here
"""inputs2 = new_data[len(train)-60:len(train)].values
for i in range(0,60):
inputsNorm2 = inputs2.reshape(-1,1)
inputsNorm2 = scaler.transform(inputs2)
X_test2 = []
for i in range(0,inputsNorm2.shape[0]):
X_test2.append(inputsNorm2[i,0])
X_test2 = np.array(X_test2)
X_test2 = np.reshape(X_test2, (1,60,1))
closing_price = model.predict(X_test2)
closing_price = scaler.inverse_transform(closing_price)
#add new data to the input set and remove the beginning data pt in the input set
nextDataToAdd = closing_price[0]
dataset = np.vstack([dataset, nextDataToAdd])
inputs2 = dataset[len(dataset)-60:]
print(closing_price)"""
inputs2 = new_data[len(train)-60:len(train)].values
trainInputs2 = new_data[len(train)-61:len(train)].values
predictVals = 20
for i in range(0,predictVals):
inputsNorm2 = inputs2.reshape(-1,1)
inputsNorm2 = scaler.transform(inputs2)
X_test2 = []
#use 60 to predict 1
X_test2.append(inputsNorm2[:, 0])
X_test2 = np.array(X_test2)
X_test2 = np.reshape(X_test2, (X_test2.shape[0],X_test2.shape[1],1))
closing_price = model.predict(X_test2)
closing_price = scaler.inverse_transform(closing_price)
#add new data to the input set and remove the beginning data pt in the input set
nextDataToAdd = closing_price[0]
inputs2 = inputs2[1:]
inputs2 = np.vstack([inputs2, nextDataToAdd])
trainInputs2 = trainInputs2[1:]
trainInputs2 = np.vstack([trainInputs2, nextDataToAdd])
new_data2 = pd.DataFrame(index=range(0,predictVals+1),columns=['Date', 'Close'])
j = len(train)-1
new_data2['Date'][0] = data['Date'][j]
new_data2['Close'][0] = data['Close'][j]
for i in range(1,predictVals+1):
new_data2['Date'][i] = data['Date'][j] + timedelta(days=i+1)
new_data2['Close'][i] = inputs2[i+39,0]
#setting index
new_data2.index = new_data2.Date
new_data2.drop('Date', axis=1, inplace=True)
new_data2['Date'] = new_data2.index.strftime('%Y-%m-%d')
new_data2['Close'] = new_data2['Close'].apply(lambda x: round(x, 2))
json_valid = new_data2.to_json(orient='records')
print(json_valid)