Spaces:
Runtime error
Runtime error
Commit
•
8b70a40
1
Parent(s):
e1ed3f8
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# pip install streamlit fbprophet yfinance plotly
|
2 |
+
import streamlit as st
|
3 |
+
from datetime import date
|
4 |
+
|
5 |
+
import yfinance as yf
|
6 |
+
from fbprophet import Prophet
|
7 |
+
from fbprophet.plot import plot_plotly
|
8 |
+
from plotly import graph_objs as go
|
9 |
+
|
10 |
+
START = "2015-01-01"
|
11 |
+
TODAY = date.today().strftime("%Y-%m-%d")
|
12 |
+
|
13 |
+
st.title('Stock Forecast App')
|
14 |
+
|
15 |
+
stocks = ('GOOG', 'AAPL', 'MSFT', 'GME')
|
16 |
+
selected_stock = st.selectbox('Select dataset for prediction', stocks)
|
17 |
+
|
18 |
+
n_years = st.slider('Years of prediction:', 1, 4)
|
19 |
+
period = n_years * 365
|
20 |
+
|
21 |
+
|
22 |
+
@st.cache
|
23 |
+
def load_data(ticker):
|
24 |
+
data = yf.download(ticker, START, TODAY)
|
25 |
+
data.reset_index(inplace=True)
|
26 |
+
return data
|
27 |
+
|
28 |
+
|
29 |
+
data_load_state = st.text('Loading data...')
|
30 |
+
data = load_data(selected_stock)
|
31 |
+
data_load_state.text('Loading data... done!')
|
32 |
+
|
33 |
+
st.subheader('Raw data')
|
34 |
+
st.write(data.tail())
|
35 |
+
|
36 |
+
# Plot raw data
|
37 |
+
def plot_raw_data():
|
38 |
+
fig = go.Figure()
|
39 |
+
fig.add_trace(go.Scatter(x=data['Date'], y=data['Open'], name="stock_open"))
|
40 |
+
fig.add_trace(go.Scatter(x=data['Date'], y=data['Close'], name="stock_close"))
|
41 |
+
fig.layout.update(title_text='Time Series data with Rangeslider', xaxis_rangeslider_visible=True)
|
42 |
+
st.plotly_chart(fig)
|
43 |
+
|
44 |
+
plot_raw_data()
|
45 |
+
|
46 |
+
# Predict forecast with Prophet.
|
47 |
+
df_train = data[['Date','Close']]
|
48 |
+
df_train = df_train.rename(columns={"Date": "ds", "Close": "y"})
|
49 |
+
|
50 |
+
m = Prophet()
|
51 |
+
m.fit(df_train)
|
52 |
+
future = m.make_future_dataframe(periods=period)
|
53 |
+
forecast = m.predict(future)
|
54 |
+
|
55 |
+
# Show and plot forecast
|
56 |
+
st.subheader('Forecast data')
|
57 |
+
st.write(forecast.tail())
|
58 |
+
|
59 |
+
st.write(f'Forecast plot for {n_years} years')
|
60 |
+
fig1 = plot_plotly(m, forecast)
|
61 |
+
st.plotly_chart(fig1)
|
62 |
+
|
63 |
+
st.write("Forecast components")
|
64 |
+
fig2 = m.plot_components(forecast)
|
65 |
+
st.write(fig2)
|