File size: 2,259 Bytes
0d4d5de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d2ddff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#importing libraries 
import streamlit as st
import yfinance as yf
from datetime import date
from prophet import Prophet 
from prophet.plot import plot_plotly
from plotly import graph_objs as go


#main function
def main():
    START="2017-01-01"
    TODAY=date.today().strftime("%Y-%m-%d")

    st.title("Stock Forecast App")
    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.")

    stocks= ("MSFT","AAPL","GOOG")

    st.write("""
             ***Stock Tickers:***
             - AAPL : Apple Inc.
             - MSFT : Microsoft Corporation.
             - GOOG : Alphabet Inc.(Google)
             """)
    
    selected_stocks=st.selectbox('select dataset for prediction',stocks)
    n_year=st.slider("**Select Year of prediction**",1,4)
    period=n_year * 365

    #download Dataset 
    @st.cache_data
    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_stocks)
    data_load_state.text("Done!!")

    st.subheader("Raw data")
    st.write(data.tail())

    #plot the Raw Data
    def plot_rawdata():
        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="Forecast Data")
        fig.update_xaxes(rangeslider_visible=True)
        st.plotly_chart(fig)

    plot_rawdata()

    #Forecasting 

    df_train=data[["Date",'Close']]
    df_train=df_train.rename(columns={'Date':'ds','Close':'y'})

    model=Prophet()
    model.fit(df_train)

    future=model.make_future_dataframe(periods=period)
    forecast=model.predict(future)

    #show and plot the feature
    st.subheader("Forecasted dataset")
    st.write(forecast.tail(5))

    st.write("Forecast plot")
    fig1=plot_plotly(model,forecast)
    st.plotly_chart(fig1)

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

if __name__=="__main__":
    main()