-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproject.py
78 lines (59 loc) · 2.54 KB
/
project.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
# importing libraries and site packages
import streamlit as st
from datetime import date
import yfinance as yf
from fbprophet import Prophet
from fbprophet.plot import plot_plotly
from plotly import graph_objs as go
# Defining a start date so that we can get a range of data from that date till today
START = "2011-01-01"
TODAY = date.today().strftime("%Y-%m-%d")
# Providing a title to the project
st.title('Predict over data')
# yfinance has its abbreviations defined for the stocks, we are here picking few and using them
stocks = ('GOOG', 'AAPL', 'MSFT', 'GME')
# creating a select box using streamlit feature
selected_stock = st.selectbox('Select dataset for prediction', stocks)
# Setting a slider for the years of prediction and getting the details of prices of stock of each day
n_years = st.slider('Years of prediction:', 1, 4)
period = n_years * 365
# Definig a function to fetch the stock quotes
@st.cache
def load_data(ticker):
data = yf.download(ticker, START, TODAY)
data.reset_index(inplace=True)
return data
# Loading data from yfinance for selected stocks using load_data()
# Caching it so that if I chose AAPL first and get stock prices and then get the same for GOOG, and again switches to AAPL then data will not be fetched again rather will be saved due to earlier access
data_load_state = st.text('Loading data...')
data = load_data(selected_stock)
data_load_state.text('Loading data... done!')
st.subheader('Raw data')
st.write(data.tail())
# Plotting the raw data
def plot_raw_data():
fig = go.Figure()
# Open is the first price that someone has paid for for share of the particular stock.
# Close is the last price that someone has paid for a share of the particular stock.
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 with Rangeslider', xaxis_rangeslider_visible=True)
st.plotly_chart(fig)
plot_raw_data()
# Predicting forecast with Prophet.
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)
# Showing and plotting the forecast with the help of plotly
st.subheader('Forecast data')
st.write(forecast.tail())
st.write(f'Forecast plot for {n_years} years')
fig1 = plot_plotly(m, forecast)
st.plotly_chart(fig1)
# Forecast components
st.write("Forecast components")
fig2 = m.plot_components(forecast)
st.write(fig2)