Skip to content

Commit

Permalink
added stock prediction web app
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickloeber committed Feb 7, 2021
1 parent c07e6ee commit 77c9039
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions stockprediction/main.py
@@ -0,0 +1,65 @@
# pip install streamlit fbprophet yfinance plotly
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

START = "2015-01-01"
TODAY = date.today().strftime("%Y-%m-%d")

st.title('Stock Forecast App')

stocks = ('GOOG', 'AAPL', 'MSFT', 'GME')
selected_stock = st.selectbox('Select dataset for prediction', stocks)

n_years = st.slider('Years of prediction:', 1, 4)
period = n_years * 365


@st.cache
def load_data(ticker):
data = yf.download(ticker, START, TODAY)
data.reset_index(inplace=True)
return data


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())

# Plot raw data
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 with Rangeslider', xaxis_rangeslider_visible=True)
st.plotly_chart(fig)

plot_raw_data()

# Predict 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)

# Show and plot forecast
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)

st.write("Forecast components")
fig2 = m.plot_components(forecast)
st.write(fig2)

3 comments on commit 77c9039

@hari6gokulam
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

facing issues with fbprophet

@mubboans
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any lead??

@soumya-99
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just do pip install prophet
And it will work fine.
But I got a different error.

ValueError: Column ds has timezone specified, which is not supported. Remove timezone.

So, If anyone can solve this, plesase comment here.

Please sign in to comment.