Anithprakash commited on
Commit
0d4d5de
·
verified ·
1 Parent(s): 7cdb793

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -79
app.py CHANGED
@@ -1,80 +1,80 @@
1
- #importing libraries
2
- import streamlit as st
3
- import yfinance as yf
4
- from datetime import date
5
- from prophet import Prophet
6
- from prophet.plot import plot_plotly
7
- from plotly import graph_objs as go
8
-
9
-
10
- #main function
11
- def main():
12
- START="2017-01-01"
13
- TODAY=date.today().strftime("%Y-%m-%d")
14
-
15
- st.title("Stock Forecast App")
16
- st.write("**Disclaimer:** The stock price predictions generated by this app should not be considered financial advice. Always consult with a qualified financial advisor before making investment decisions.")
17
-
18
- stocks= ("MSFT","AAPL","GOOG")
19
-
20
- st.write("""
21
- ***Stock Tickers:***
22
- - AAPL : Apple Inc.
23
- - MSFT : Microsoft Corporation.
24
- - GOOG : Alphabet Inc.(Google)
25
- """)
26
-
27
- selected_stocks=st.selectbox('select dataset for prediction',stocks)
28
- n_year=st.slider("**Select Year of prediction**",1,4)
29
- period=n_year * 365
30
-
31
- #download Dataset
32
- @st.cache_data
33
- def load_data(ticker):
34
- data=yf.download(ticker,START,TODAY)
35
- data.reset_index(inplace=True)
36
- return data
37
-
38
- data_load_state=st.text('Loading data..')
39
- data=load_data(selected_stocks)
40
- data_load_state.text("Done!!")
41
-
42
- st.subheader("Raw data")
43
- st.write(data.tail())
44
-
45
- #plot the Raw Data
46
- def plot_rawdata():
47
- fig=go.Figure()
48
- fig.add_trace(go.Scatter(x=data['Date'], y=data['Open'],name="stock_open"))
49
- fig.add_trace(go.Scatter(x=data['Date'], y=data['Close'],name="stock_close"))
50
- fig.layout.update(title_text="Forecast Data")
51
- fig.update_xaxes(rangeslider_visible=True)
52
- st.plotly_chart(fig)
53
-
54
- plot_rawdata()
55
-
56
- #Forecasting
57
-
58
- df_train=data[["Date",'Close']]
59
- df_train=df_train.rename(columns={'Date':'ds','Close':'y'})
60
-
61
- model=Prophet()
62
- model.fit(df_train)
63
-
64
- future=model.make_future_dataframe(periods=period)
65
- forecast=model.predict(future)
66
-
67
- #show and plot the feature
68
- st.subheader("Forecasted dataset")
69
- st.write(forecast.tail(5))
70
-
71
- st.write("Forecast plot")
72
- fig1=plot_plotly(model,forecast)
73
- st.plotly_chart(fig1)
74
-
75
- st.write("Forecast components")
76
- fig2=model.plot_components(forecast)
77
- st.write(fig2)
78
-
79
- if __name__=="__main__":
80
  main()
 
1
+ #importing libraries
2
+ import streamlit as st
3
+ import yfinance as yf
4
+ from datetime import date
5
+ from prophet import Prophet
6
+ from prophet.plot import plot_plotly
7
+ from plotly import graph_objs as go
8
+
9
+
10
+ #main function
11
+ def main():
12
+ START="2017-01-01"
13
+ TODAY=date.today().strftime("%Y-%m-%d")
14
+
15
+ st.title("Stock Forecast App")
16
+ st.write("**Disclaimer:** The stock price predictions generated by this app should not be considered financial advice. Always consult with a qualified financial advisor before making investment decisions.")
17
+
18
+ stocks= ("MSFT","AAPL","GOOG")
19
+
20
+ st.write("""
21
+ ***Stock Tickers:***
22
+ - AAPL : Apple Inc.
23
+ - MSFT : Microsoft Corporation.
24
+ - GOOG : Alphabet Inc.(Google)
25
+ """)
26
+
27
+ selected_stocks=st.selectbox('select dataset for prediction',stocks)
28
+ n_year=st.slider("**Select Year of prediction**",1,4)
29
+ period=n_year * 365
30
+
31
+ #download Dataset
32
+ @st.cache_data
33
+ def load_data(ticker):
34
+ data=yf.download(ticker,START,TODAY)
35
+ data.reset_index(inplace=True)
36
+ return data
37
+
38
+ data_load_state=st.text('Loading data..')
39
+ data=load_data(selected_stocks)
40
+ data_load_state.text("Done!!")
41
+
42
+ st.subheader("Raw data")
43
+ st.write(data.tail())
44
+
45
+ #plot the Raw Data
46
+ def plot_rawdata():
47
+ fig=go.Figure()
48
+ fig.add_trace(go.Scatter(x=data['Date'], y=data['Open'],name="stock_open"))
49
+ fig.add_trace(go.Scatter(x=data['Date'], y=data['Close'],name="stock_close"))
50
+ fig.layout.update(title_text="Forecast Data")
51
+ fig.update_xaxes(rangeslider_visible=True)
52
+ st.plotly_chart(fig)
53
+
54
+ plot_rawdata()
55
+
56
+ #Forecasting
57
+
58
+ df_train=data[["Date",'Close']]
59
+ df_train=df_train.rename(columns={'Date':'ds','Close':'y'})
60
+
61
+ model=Prophet()
62
+ model.fit(df_train)
63
+
64
+ future=model.make_future_dataframe(periods=period)
65
+ forecast=model.predict(future)
66
+
67
+ #show and plot the feature
68
+ st.subheader("Forecasted dataset")
69
+ st.write(forecast.tail(5))
70
+
71
+ st.write("Forecast plot")
72
+ fig1=plot_plotly(model,forecast)
73
+ st.plotly_chart(fig1)
74
+
75
+ st.write("Forecast components")
76
+ fig2=model.plot_components(forecast)
77
+ st.write(fig2)
78
+
79
+ if __name__=="__main__":
80
  main()