File size: 3,317 Bytes
2dd599b b6d1b85 2dd599b b6d1b85 2dd599b 4c63c89 f11d375 2dd599b a874ac5 2dd599b 37f6248 2dd599b d8f0bd9 2dd599b 7a02452 2dd599b 7a02452 b48b5bf 7a02452 2dd599b b48b5bf 2dd599b b48b5bf 2dd599b 37f6248 2dd599b bd44df6 b48b5bf 36653b7 ef94ad2 |
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 |
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import pandas_datareader.data as web
import sklearn as sk
import datetime as dt
from datetime import date
import yfinance as yf
yf.pdr_override()
from keras.models import load_model
import streamlit as st
# defining the dates here
st_dt1 = dt.datetime(2010,1,1)
end_dt1 = dt.datetime(2019,12,31)
st_dt2 = dt.datetime(2021,1,1)
end_dt2 = date.today()
st.title('Stock Trend Prediction')
user_input = st.text_input('Enter Stock Tiker','AAPL')
df_stocks1 = web.get_data_yahoo( user_input, st_dt1, end_dt1 )
df_stocks2 = web.get_data_yahoo(user_input, st_dt2, end_dt2 )
df_stocks = pd.concat([df_stocks1,df_stocks2])
df = df_stocks.reset_index()
# Desribing the data
stk_des = "Data for "+str(user_input)+" from 2010 to 2023 exluding the Covid Year"
st.subheader(stk_des)
st.write(df.describe())
# visualization
st.subheader('The Closing price vs Time Chart')
fig = plt.figure(figsize=(12,6))
plt.plot(df.Close)
st.pyplot(fig)
# with Moving Averages
st.subheader('The Closing price vs Time Chart with MA')
ma100 = df.Close.rolling(100).mean()
ma200 = df.Close.rolling(200).mean()
fig = plt.figure(figsize=(12,6))
plt.plot(df.Close,'b',label='Original Price')
plt.plot(ma100,'r',label='MA100')
plt.plot(ma200,'g',label='MA200')
plt.xlabel('Time')
plt.ylabel('Price')
plt.legend()
st.pyplot(fig)
# train-test split
df_train = pd.DataFrame(df['Close'][0:int(df.shape[0]*0.7)])
df_test = pd.DataFrame(df['Close'][int(df.shape[0]*0.7):int(df.shape[0])])
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(0,1))
df_train_array = scaler.fit_transform(df_train)
# need to fix the step size of 100
x_train = []
y_train = []
for i in range(100,df_train_array.shape[0]):
x_train.append(df_train_array[i-100:i])
y_train.append(df_train_array[i,0])
x_train,y_train = np.array(x_train), np.array(y_train)
model = load_model("keras_model.h5")
past_100_days = df_train.tail(100)
df_final = pd.concat([past_100_days,df_test],ignore_index=True)
df_final = scaler.fit_transform(df_final)
x_test = []
y_test = []
for i in range(100,(int(df_final.shape[0])+1) ):
x_test.append(df_final[i-100:i])
for i in range(100,df_final.shape[0]):
# x_test.append(df_final[i-100:i])
y_test.append(df_final[i])
x_test, y_test = np.array(x_test), np.array(y_test)
y_pred = model.predict(x_test)
y_pred_tmp = y_pred
y_test_tmp = y_test
y_pred_tmp = scaler.inverse_transform(y_pred_tmp)
y_test_tmp = np.reshape(y_test_tmp,(-1,1))
y_test_tmp = scaler.inverse_transform(y_test_tmp)
scaler_val = scaler.scale_
scale_factor = 1/scaler_val
y_pred = y_pred*scale_factor
y_test = y_test*scale_factor
#plotting
st.subheader("Forecasted Trends vs Original Prie")
fig2 = plt.figure(figsize=(12,6))
plt.plot(y_test,'b',label='Original Price')
plt.plot(y_pred,'r',label='Predicted Price')
plt.xlabel('Time')
plt.ylabel('Price')
plt.legend()
st.pyplot(fig2)
# also printing the next day closing price
last_predicted_price = round(float(y_pred_tmp[-1]),2)
actual_closing_price = round(float(y_test_tmp[-1]),2)
# ls = "Predicted Upcoming Trading Day Closing Price: "
st.subheader("Predicted Upcoming Trading Day Closing Price: ")
st.write(last_predicted_price)
st.subheader("Last Closing Price: ")
st.write(actual_closing_price) |