-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOA_Stock.py
436 lines (204 loc) · 5.82 KB
/
BOA_Stock.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
#!/usr/bin/env python
# coding: utf-8
# # Imports
# In[17]:
import math
import pandas_datareader as web
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, LSTM
from keras.layers import Dropout
import matplotlib.pyplot as plt
# # Importing Stock data from yahoo
# In[3]:
# get the stock quote
df=web.DataReader('BAC',data_source='yahoo',start='2010-01-01', end='2020-5-12')
# # Data Filter
# In[4]:
df=df[['Open','Volume']]
print(df)
# In[177]:
# Visualising our data
plt.figure(figsize=[14,8])
plt.title("Close price History")
plt.plot(df['Open'])
plt.xlabel('Date',fontsize=16)
plt.ylabel('Stock Price',fontsize=16)
plt.show()
# In[14]:
# seperating training and testing data
# We are going to test 60 days of latest stock price
# The rest data before the latest 60 days will be trained
Training_data=df[:2547].values
Test_data=df[2547:].values
# In[15]:
print("Shape of Test_data ",Test_data.shape)
print(" Shape of Training data ", Training_data.shape)
# # Feature Scaling
# In[18]:
#Scaling the data between one and 0
sc=MinMaxScaler(feature_range=(0,1))
Training_data_scaled=sc.fit_transform(Training_data)
# # Creating Time Steps
# In[19]:
x_train=[]
y_train=[]
#Creating a 60 day timesteps
for i in range(60,Training_data.shape[0]):
#Takes data form 0 to 59. The process continues
x_train.append(Training_data_scaled[i-60:i])
# takes the data of 60th position
y_train.append(Training_data_scaled[i,0])
# In[20]:
# converting x_train and y_train to array
x_train=np.array(x_train)
y_train=np.array(y_train)
# In[22]:
print("The shape of x_train is:",x_train.shape)
print("The shape of y_train is:",y_train.shape)
# # Initialising the RNN
# In[28]:
regressor=Sequential()
# In[30]:
# First layer of LSTM and Dropout
#Dropout is important feature of LSTM wher it drops some portion of the data
regressor.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1],2)))
# In[32]:
# adding dropout of 20%
regressor.add(Dropout(0.2))
# In[33]:
# Adding the second layer of the LSTM
regressor.add(LSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.2))
# In[34]:
# Adding the Third layer of the LSTM
regressor.add(LSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.2))
# In[35]:
#Adding fourth Layer of LSTM
# we remove return sequence as we are not returning any sequence
regressor.add(LSTM(units=50))
regressor.add(Dropout(0.2))
# # Output layer
# In[36]:
regressor.add(Dense(units=1))
# # Adding Optimizer
# In[37]:
# for the optimer we are using adam which is a socratic gradient descent
regressor.compile(optimizer='adam', loss='mean_squared_error')
# # Training our RNN
# In[38]:
# Batch size of 32 is a standard batch size most of the people use
regressor.fit(x_train,y_train,epochs=100,batch_size=32)
# # Making Prediction
# In[122]:
# We are going to concat both training and testing dataset
Train_60_days=df[:2547].tail(60)
Test_60_days=df[2547:]
Train_60_days.head(5)
# In[123]:
new_df=Train_60_days.append(Test_60_days, ignore_index=True)
new_df.head()
# In[124]:
new_df.shape
# In[125]:
inputs=sc.transform(new_df)
inputs.shape[0]
# In[126]:
new_x_test=[]
new_y_test=[]
for i in range(60,inputs.shape[0]):
new_x_test.append(inputs[i-60:i])
new_y_test.append(inputs[i,0])
# In[127]:
new_x_test=np.array(new_x_test)
new_y_test=np.array(new_y_test)
new_x_test.shape
# In[128]:
final_prediction=regressor.predict(new_x_test)
# In[129]:
sc.scale_
# In[130]:
scaler=(1/3.26904210e-02)
scaler
# In[131]:
final_prediction=final_prediction*scaler
final_prediction=(final_prediction+5.11)
new_y_test=new_y_test*scaler
new_y_test=(new_y_test+5.11)
# In[132]:
final_prediction
# In[ ]:
# In[133]:
plt.figure(figsize=(16,8))
plt.plot(new_y_test, color='red', label="Real BOA Stock")
plt.plot(final_prediction, color='blue', label="Predicted BOA Stock")
plt.title("BOA stock prediction")
plt.xlabel("Time")
plt.ylabel('Stock Price')
plt.legend()
plt.show()
# # Table of Actual price vs Predicted price
# In[134]:
Visualise_Test_data=df[2547:]
Visualise_Test_data['Open_before_pred']=new_y_test
Visualise_Test_data['Actual_Prediction']=final_prediction
Visualise_Test_data
# # Predicting single day
# In[155]:
# Getting updated data for today
new_df=web.DataReader('BAC',data_source='yahoo',start='2020-01-01', end='2020-5-13')
# In[156]:
new_df=new_df[['Open','Volume']]
# In[165]:
Last_30_days=new_df.tail(60)
# In[166]:
print(Last_30_days)
# In[167]:
inputs_single=sc.transform(Last_30_days)
# In[168]:
Single_data=[]
for i in range(60,61):
Single_data.append(inputs_single[i-60:i])
# In[169]:
Single_data=np.array(Single_data)
# In[170]:
Single_data
# In[171]:
Single_data.shape
# In[172]:
Single_final_prediction=regressor.predict(Single_data)
# In[173]:
Single_final_prediction=Single_final_prediction*scaler
Single_final_prediction=(Single_final_prediction+5.11)
# In[174]:
print(" The Stock price for next Day is", Single_final_prediction)
# # Finding the trend
# In[179]:
Visualise_Test_data[:5]
# In[180]:
final_prediction[0]
# In[193]:
Predict_trend=[]
Actual_trend=[]
for i in range(59):
Predict_trend.append(final_prediction[i]-final_prediction[i+1])
Actual_trend.append(new_y_test[i]-new_y_test[i+1])
# In[194]:
Trend_df=pd.DataFrame({'Actual_Trend':Actual_trend,'Predict_trend':Predict_trend})
# In[204]:
def color_negative_red(val):
"""
Takes a scalar and returns a string with
the css property `'color: red'` for negative
strings, black otherwise.
"""
color = 'green' if val < 0 else 'red'
return 'color: %s' % color
# In[205]:
s = Trend_df.style.applymap(color_negative_red)
s
# In[ ]:
# In[ ]: