File size: 2,552 Bytes
f77aad9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from keras.models import load_model
import matplotlib.pyplot as plt

from datetime import datetime
import yfinance as yf

st.title("Stock Price Predictor App")

stock = st.text_input("Enter the Stock ID","GOOG")
end = datetime.now()
start = datetime(end.year-20,end.month,end.day)

google_data = yf.download(stock,start,end)
model = load_model("google_stock_prediction.keras")

st.subheader("Stock Data")
st.write(google_data)
splitting_len = int(len(google_data)*0.7)
x_test = pd.DataFrame(google_data.Close[splitting_len:])

def plot_graph(figsize,values,full_data,extra_data=0, extra_dataset =None):
    fig = plt.figure(figsize = figsize)
    plt.plot(values,"Orange")
    plt.plot(full_data.Close,'b')
    if extra_data:
        plt.plot(extra_dataset)
    return fig
st.subheader("Original Close price and MA for 250 days")
google_data['MA-250'] = google_data.Close.rolling(250).mean()
st.pyplot(plot_graph((15,6),google_data['MA-250'],google_data,0))

st.subheader("Original Close price and MA for 200 days")
google_data['MA-200'] = google_data.Close.rolling(200).mean()
st.pyplot(plot_graph((15,6),google_data['MA-200'],google_data,0))

st.subheader("Original Close price and MA for 100 days")
google_data['MA-100'] = google_data.Close.rolling(100).mean()
st.pyplot(plot_graph((15,6),google_data['MA-100'],google_data,0))

st.subheader("Original Close price and MA for 250 days and 100 Days")
st.pyplot(plot_graph((15,6),google_data['MA-250'],google_data,1,google_data["MA-100"]))

scaler= MinMaxScaler(feature_range = (0,1))
scaled_data = scaler.fit_transform(x_test[['Close']])

x_data= []
y_data = []
for i in range(100,len(scaled_data)):
    x_data.append(scaled_data[i-100:i])
    y_data.append(scaled_data[i])

x_data ,y_data = np.array(x_data),np.array(y_data)
predictions = model.predict(x_data)

inv_pre = scaler.inverse_transform(predictions)
inv_y_test = scaler.inverse_transform(y_data)

plotting_data = pd.DataFrame(
    {
        'original_test_data':inv_y_test.reshape(-1),
        'Predictions':inv_pre.reshape(-1)
    },
    index = google_data.index[splitting_len+100:]
)
st.subheader("Original values vs predicted values")
st.write(plotting_data)

st.subheader("Original close price vs predicted close price")
fig = plt.figure(figsize = (15,6))
plt.plot(pd.concat([google_data.Close[:splitting_len+100],plotting_data],axis = 0))
plt.legend(["Data not used",'original test data','predicted test data'])
st.pyplot(fig)