-
Notifications
You must be signed in to change notification settings - Fork 0
/
predicting_stock_price_app.py
66 lines (49 loc) · 1.68 KB
/
predicting_stock_price_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
# -*- coding: utf-8 -*-
"""Predicting Stock Price App
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1onQCQLeeIMkGtAbxmRgcr9wlE2_axlEX
"""
import streamlit as st
import datetime as dt
import yfinance as yf
from fbprophet import Prophet
from fbprophet.plot import plot_plotly
from plotly import graph_objs as go
start = dt.datetime(2015, 1, 1)
end = dt.datetime.now()
st.title('Stock Prediction App')
stocks = ('TSLA', 'GOOG', 'AAPL', 'MSFT')
selected_stocks = st.selectbox('Select Dataset for Predictions', stocks)
n_years = st.slider('Years of Prediction: ', 1, 4)
period = n_years * 365
def load_data(ticker):
data = yf.download(ticker, start, end)
data.reset_index(inplace=True)
return data
data_load_state = st.text("Load Data...")
data = load_data(selected_stocks)
data_load_state.text('Loading data...Done!')
st.subheader('Raw Data')
st.write(data.tail())
def plot_raw_data():
fig = go.Figure()
fig.add_trace(go.Scatter(x=data['Date'], y=data['Open'], name='Stock_Open'))
fig.add_trace(go.Scatter(x=data['Date'], y=data['Close'], name='Stock_Close'))
fig.layout.update(title_text='Time Series Data', xaxis_rangeslinder_visible=True)
st.plotly_chart(fig)
#Forecasting
df_train = data[['Date', 'Close']]
df_train = df_train.rename(columns={'Date':'ds', 'Close':'y'})
m = Prophet()
m.fit(df_train)
future = m.make_future_dataframe(periods = period)
forecast = m.predict(future)
st.subheader('Forecast Data')
st.write(forecast.tail())
st.write('Forecast Data')
fig1 = plot_plotly(m, forecast)
st.plotly_chart(fig1)
st.write('Forecast Components')
fig2 = m.plot_components(forecast)
st.write(fig2)