
DESCRIPTION
-
The code imports the necessary libraries: `streamlit`, `date` from `datetime`, `pandas`, `yfinance`, `Prophet` from `prophet`, `plot_plotly` from `prophet.plot`, and `graph_objs` from `plotly`.
-
The `START` variable is set to the date "2010-01-01" and the `TODAY` variable is set to the current date in the format "%Y-%m-%d".
-
The code includes a slider where the user can select the number of years for which they want to predict the stock's future data.
-
It imports and uses a custom option menu component called `option_menu` to allow the user to switch between viewing "Raw Data" and "Forecast Data".
-
The `load_data` function is defined, which takes a stock ticker as an input and retrieves the historical stock data using the `yfinance` library. It also displays the company name as a subheading and returns the downloaded data.
-
The `load_data` function is decorated with `@st.cache_data`, which caches the function's output based on its input. This improves performance by avoiding redundant data loading.
-
The code calls the `load_data` function with the selected stock ticker to retrieve the stock's historical data and assigns it to the `data` variable.
-
If the user selects "Raw Data" in the option menu and the `data` variable is not empty, it displays the raw data by showing the last few rows of the DataFrame.
-
It defines a function called `plot_raw_data` that plots the "Open" and "Close" prices of the stock over time using `plotly`.
-
The code calls the `plot_raw_data` function to display the raw data plot.
-
If the user selects "Forecast Data" in the option menu and the `data` variable is not empty, it prepares the data for forecasting using Facebook's Prophet library.
-
It extracts the "Date" and "Close" columns from the historical data and renames them as "ds" and "y", respectively, to fit the required format for Prophet.
-
The code creates a Prophet model object (`m`) and fits it on the training data.
-
It generates a dataframe (`future`) with future dates for which predictions need to be made.
-
The code uses the trained model (`m`) to predict the future stock prices by calling the `predict` method on the `future` dataframe.
-
The forecasted data is displayed by showing the last few rows of the DataFrame.
-
The code plots the forecasted data using `plot_plotly` function from Prophet and `plotly` library, displaying the forecasted stock prices.
-
Additionally, it plots the components of the forecast such as trend and seasonality.