Stock Prediction Web App

## Project Overview

This project implements an interactive web application with Streamlit to forecast stock prices. It allows the user to select a stock ticker, adjust the prediction timeframe, view historical data, and see future price forecasts generated by Facebook Prophet.

## Data Loading

The yfinance library is used to download historical OHLC price data for the chosen stock. This time series data is then fed into the Prophet forecasting model. Streamlit components like progress indicators and text are used to display load status.

## Data Visualization

Raw price data is visualized with Plotly graphs showing Open and Close prices over time. This allows exploratory analysis of the historical data.

## Forecasting

Facebook Prophet is used to conduct time series forecasting on the stock price data. The predict method generates a future dataframe containing a date column and predicted stock price values for the requested timeframe.

## Visualizations

The Plotly and Prophet charting tools are used to visualize the forecasts. The app shows the raw numerical forecasts, as well as graphs of the predicted forecast trends and decomposition of trend vs seasonal components.

## Interactivity

Streamlit components like dropdown menus, sliders and buttons allow the user to customize the stock and timeframe parameters to suit their analysis needs. This interactivity makes for an engaging, customizable forecasting app.

##Conclusion

This stock prediction web application serves as an excellent demonstration of how to combine multiple Python libraries to build an interactive, useful data science application.

By leveraging Streamlit for the web framework, yfinance for data retrieval, Prophet for forecasting, and Plotly for visualization, the project delivers a robust tool for time series prediction. Users can customize the stock and timeframe, visualize historical data, and interact with the automatically generated forecasts.

The modular structure splits logical components like data loading, transformation modeling, and visualization into separate functions. This makes the code maintainable and extensible. Additional stocks could be added, different models tested, and new visualizations incorporated over time.

---

##Preview

Source Code:

import streamlit as st
import datetime

import yfinance as yf
from prophet import Prophet
from prophet.plot import plot_plotly
from plotly import graph_objs as go

START = "2015-01-01"
TODAY = datetime.datetime.today().strftime("%Y-%m-%d")

st.title("Stock Prediction App")

stocks = ("AAPL", "GOOG", "MSFT", "GME", "TEF")
selected_stock = st.selectbox("Select dataset for prediction", stocks)

n_years = st.slider("Years of Prediction:", 1 , 4)
period = n_years * 365

def load_data(ticker):
    data = yf.download(ticker, START, TODAY)
    data.reset_index(inplace=True)
    return data

data_load_state = st.text("Load data...")
data = load_data(selected_stock)
data_load_state.text("Loading data... done!")

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

def plot_raw_data():
    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="Time Series Data", xaxis_rangeslider_visible=True)
    st.plotly_chart(fig)

plot_raw_data()

# Forecasting
df_train = data[["Date", "Close"]]
df_train = df_train.rename(columns={"Date": "ds", "Close": "y"}) #Rename for fbprophet

m = Prophet()
m.fit(df_train)
future = m.make_future_dataframe(periods=period)
forecast = m.predict(future)

st.subheader("Forecast")
st.write("Forecast Data")
st.write(forecast.tail())

st.write("Forecast Graphic")
fig1 = plot_plotly(m, forecast)
st.plotly_chart(fig1)

st.write("Forecast Components")
fig2 = m.plot_components(forecast)
st.write(fig2)

Yahoo Finance: https://finance.yahoo.com/

You can run this locally, running the code on a virtual environment (most IDE's work).

Thanks for reading!

Gerard Puche