CoolFace
Apppublic

abhishekh14/stock-market-forecasting

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
App README

๐Ÿ“ˆ Stock Market Forecasting System

![Python](https://www.python.org/) ![Streamlit](https://streamlit.io/) ![Scikit-Learn](https://scikit-learn.org/) ![TensorFlow](https://www.tensorflow.org/) ![License: MIT](https://opensource.org/licenses/MIT)

A production-ready Stock Market Forecasting System that fetches historical market data, performs Exploratory Data Analysis (EDA), trains multiple Machine Learning and Deep Learning models, compares their performance, and makes recursive price predictions for various horizons (7, 30, or 60 days).


๐ŸŒŸ Key Features

  • โ€”Real-Time Data Access: Fetches historical stock data dynamically from Yahoo Finance (yfinance).
  • โ€”Technical Feature Engineering: Constructs Simple Moving Averages (20, 50, 100 days), Daily Returns, rolling Volatility, and multi-day price lags.
  • โ€”Multiple Forecasting Engines:
  • โ€”Linear Regression: Fast baseline regression.
  • โ€”Random Forest Regressor: Handles complex non-linear relations.
  • โ€”XGBoost Regressor: High-performance gradient boosted decision trees.
  • โ€”LSTM Neural Network (TensorFlow/Keras): Captures deep sequential relationships.
  • โ€”Auto-Selection: Compares models side-by-side using RMSE, MAE, and $R^2$, and auto-selects the best model.
  • โ€”Interactive Dashboards:
  • โ€”Plotly Candlestick charts with overlayed moving averages.
  • โ€”Dynamic correlation heatmaps of indicators.
  • โ€”Comparison of actual vs. predicted validation sets.
  • โ€”Future price forecast charts with recursive day-by-day calculations.
  • โ€”Data Export: Export future forecast predictions to CSV instantly.

๐Ÿ› ๏ธ Tech Stack

  • โ€”Frontend/Dashboard: Streamlit
  • โ€”Data Preprocessing & Manipulation: Pandas, NumPy
  • โ€”Visualizations: Plotly, Matplotlib, Seaborn
  • โ€”Machine Learning: Scikit-Learn, XGBoost
  • โ€”Deep Learning: TensorFlow / Keras (LSTM)
  • โ€”Data Source: yfinance API

๐Ÿ“‚ Project Structure

text
Stock-Market-Forecasting/
โ”‚
โ”œโ”€โ”€ app.py                      # Main Streamlit Dashboard UI
โ”œโ”€โ”€ requirements.txt            # Package Dependencies
โ”œโ”€โ”€ README.md                   # Project Documentation & HF metadata
โ”œโ”€โ”€ .gitignore                  # Python Git Ignore Rules
โ”‚
โ”œโ”€โ”€ models/
โ”‚     โ”œโ”€โ”€ best_model.pkl        # Serialized best traditional ML model
โ”‚     โ”œโ”€โ”€ best_model_info.pkl   # Performance metadata and selected model name
โ”‚     โ”œโ”€โ”€ scaler_X.pkl          # Feature Min-Max Scaler
โ”‚     โ”œโ”€โ”€ scaler_y.pkl          # Target Min-Max Scaler
โ”‚     โ”œโ”€โ”€ linear_regression.pkl # Serialized Linear Regression model
โ”‚     โ”œโ”€โ”€ random_forest.pkl     # Serialized Random Forest model
โ”‚     โ”œโ”€โ”€ xgboost.pkl           # Serialized XGBoost model
โ”‚     โ””โ”€โ”€ lstm.keras            # Saved LSTM Keras neural network
โ”‚
โ”œโ”€โ”€ data/
โ”‚     โ”œโ”€โ”€ stock_data.csv        # Cached downloaded stock historical dataset
โ”‚     โ””โ”€โ”€ test_stock_data.csv   # Cache for test verification run
โ”‚
โ”œโ”€โ”€ src/
โ”‚     โ”œโ”€โ”€ data_loader.py        # Yahoo Finance downloader
โ”‚     โ”œโ”€โ”€ preprocessing.py      # Scaling, splitting, and sequencing inputs
โ”‚     โ”œโ”€โ”€ feature_engineering.py# Tech indicator builder and lagged feature generator
โ”‚     โ”œโ”€โ”€ train_model.py        # Pipelines for LR, RF, XGB, LSTM, & auto-selection
โ”‚     โ”œโ”€โ”€ evaluate.py           # Evaluation metrics: RMSE, MAE, Rยฒ
โ”‚     โ””โ”€โ”€ predict.py            # Recursive day-by-day future forecasting
โ”‚
โ”œโ”€โ”€ notebooks/
โ”‚     โ”œโ”€โ”€ 01_EDA.ipynb          # Jupyter Notebook for exploratory analysis
โ”‚     โ””โ”€โ”€ 02_Model_Building.ipynb # Jupyter Notebook for model training & evaluation
โ”‚
โ”œโ”€โ”€ images/
โ”‚     โ””โ”€โ”€ dashboard.png         # Screenshot mockup of the UI dashboard
โ”‚
โ””โ”€โ”€ utils/
      โ””โ”€โ”€ helper.py             # UI helpers, Plotly charting, and API caches

๐Ÿš€ Installation & Local Run

Follow these steps to run the application locally:

1. Clone the repository

bash
git clone https://github.com/<username>/Stock-Market-Forecasting.git
cd Stock-Market-Forecasting

2. Set up a virtual environment (Recommended)

bash
python -m venv venv
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate

3. Install dependencies

bash
pip install -r requirements.txt

4. Run the Streamlit Dashboard

bash
streamlit run app.py

Open your browser and navigate to http://localhost:8501 to view the system.


๐Ÿ“ˆ Evaluation Metrics

All models are evaluated on a 20% out-of-sample chronological test set using the following metrics:

  • โ€”Root Mean Squared Error (RMSE): Measures the average magnitude of the prediction error.
  • โ€”Mean Absolute Error (MAE): Measures average absolute prediction error.
  • โ€”Coefficient of Determination ($R^2$ Score): Measures how well unseen samples are likely to be predicted by the model.

๐Ÿ”ฎ Future Improvements

  1. 1.Additional Features: Integrate sentiment analysis from Twitter/Reddit/Financial news APIs.
  2. 2.Alternative Architectures: Add Transformer (Attention) models and Prophet for comparison.
  3. 3.Hyperparameter Tuning: Introduce Bayesian Optimization for optimizing model hyperparameters.
  4. 4.Order Simulation: Add a simple paper-trading simulator to test forecast strategies.

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file or standard MIT terms for details.