ThomasDo commited on
Commit
2bb47bc
1 Parent(s): 046c84b

add the application file

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # pip install streamlit fbprophet yfinance plotly
2
+ import streamlit as st
3
+ from datetime import date, datetime, timedelta
4
+
5
+ import yfinance as yf
6
+ from prophet import Prophet
7
+ from prophet.plot import plot_plotly
8
+ from plotly import graph_objs as go
9
+ import pandas as pd
10
+
11
+
12
+ # TODAY = date.today().strftime("%Y-%m-%d")
13
+ TODAY = datetime.today()
14
+
15
+ st.title('Stock Forecast')
16
+
17
+ st.markdown('This app is built to predict the stock market performance')
18
+
19
+ stocks = ('TSLA', 'FB', 'NVDA', 'BABA', 'GOOG', 'AAPL', 'MSFT', 'GME', 'AMZN', 'XIACF')
20
+ selected_stock = st.selectbox('Select dataset for prediction', stocks)
21
+
22
+ n_years = st.slider('Years of prediction:', 1, 4)
23
+ period = n_years * 365
24
+
25
+ new_resolution = st.radio(
26
+ "Do you want to get the higher resolution or short time interval, please choose one:",
27
+ ('In 1 day', 'In 1 hour', 'In 5 minutes'))
28
+
29
+ if new_resolution == 'In 5 minutes':
30
+ new_interval = "5m"
31
+ START = TODAY - timedelta(days=30)
32
+ elif new_resolution == 'In 1 hour':
33
+ new_interval = "1h"
34
+ START = TODAY - timedelta(days=365)
35
+ else:
36
+ new_interval = "1d"
37
+ START = "2018-01-01"
38
+
39
+ @st.cache
40
+ def load_data(ticker):
41
+ data = yf.download(ticker, START, TODAY, interval = new_interval)
42
+ data.reset_index(inplace=True)
43
+ return data
44
+
45
+
46
+ data_load_state = st.text('Loading data...')
47
+ data = load_data(selected_stock)
48
+ data_load_state.text('... Data loaded, well done!')
49
+
50
+ @st.cache
51
+ def convert_df(df):
52
+ # IMPORTANT: Cache the conversion to prevent computation on every rerun
53
+ return df.to_csv().encode('utf-8')
54
+
55
+ csv = convert_df(data)
56
+
57
+ st.download_button(
58
+ label="Download data as CSV",
59
+ data=csv,
60
+ file_name='stock_data.csv',
61
+ mime='text/csv',
62
+ )
63
+
64
+
65
+ st.subheader('Raw data')
66
+ st.write(data.tail())
67
+
68
+ # Plot raw data
69
+ def plot_raw_data():
70
+ fig = go.Figure()
71
+ fig.add_trace(go.Scatter(x=data['Date'], y=data['Open'], name="stock_open"))
72
+ fig.add_trace(go.Scatter(x=data['Date'], y=data['Close'], name="stock_close"))
73
+ fig.layout.update(title_text='Time Series data with Rangeslider', xaxis_rangeslider_visible=True)
74
+ st.plotly_chart(fig)
75
+
76
+ plot_raw_data()
77
+
78
+ # Predict forecast with Prophet.
79
+ df_train = data[['Date','Close']]
80
+ df_train = df_train.rename(columns={"Date": "ds", "Close": "y"})
81
+
82
+ m = Prophet()
83
+ m.fit(df_train)
84
+ future = m.make_future_dataframe(periods=period)
85
+ forecast = m.predict(future)
86
+
87
+ # Show and plot forecast
88
+ st.subheader('Forecast data')
89
+ st.write(forecast.tail())
90
+
91
+ st.write(f'Forecast plot for {n_years} years')
92
+ fig1 = plot_plotly(m, forecast)
93
+ st.plotly_chart(fig1)
94
+
95
+ st.write("Forecast components")
96
+ fig2 = m.plot_components(forecast)
97
+ st.write(fig2)
98
+