top of page
DALL·E 2023-06-16 02.43.56 - Shiba in a tesla on the moon.png

Stock Prediction App

Python, Machine Learning, pandas, CSS

DESCRIPTION

  1. 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`.

  2. 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".

  3. 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.

  4. 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".

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. It defines a function called `plot_raw_data` that plots the "Open" and "Close" prices of the stock over time using `plotly`.

  10. The code calls the `plot_raw_data` function to display the raw data plot.

  11.  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.

  12. 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.

  13. The code creates a Prophet model object (`m`) and fits it on the training data.

  14. It generates a dataframe (`future`) with future dates for which predictions need to be made.

  15. The code uses the trained model (`m`) to predict the future stock prices by calling the `predict` method on the `future` dataframe.

  16. The forecasted data is displayed by showing the last few rows of the DataFrame.

  17. The code plots the forecasted data using `plot_plotly` function from Prophet and `plotly` library, displaying the forecasted stock prices.

  18. Additionally, it plots the components of the forecast such as trend and seasonality.

IMAGES

bottom of page