shaheerawan3 commited on
Commit
b7d2c24
·
verified ·
1 Parent(s): 4655c77

Create app.py

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