Skip to content

Bodhi8/commodity-predictor

Repository files navigation

πŸ“ˆ Commodity Price Prediction System

A production-ready deep learning system for forecasting US commodity prices using LSTM, BiLSTM, and Transformer architectures.

Python PyTorch License

Live Demo β€’ Documentation β€’ API Access


🎯 Overview

This project implements state-of-the-art time series forecasting models to predict prices for major US commodities including:

  • Energy: Crude Oil (WTI), Natural Gas
  • Precious Metals: Gold, Silver
  • Base Metals: Copper
  • Agriculture: Corn, Wheat, Soybeans

Key Features

  • 🧠 Multiple Model Architectures: LSTM, BiLSTM, CNN-LSTM, Transformer, and hybrid models
  • πŸ“Š 100+ Technical Features: Moving averages, RSI, MACD, Bollinger Bands, volatility measures
  • πŸ“ˆ Interactive Dashboard: Real-time visualization with Chart.js
  • ⚑ Automated Pipeline: Daily predictions via GitHub Actions
  • 🎯 Multi-Horizon Forecasts: 1-day, 5-day, and 10-day predictions

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      DATA INGESTION                             β”‚
β”‚  Yahoo Finance API β†’ Raw OHLCV Data β†’ Data Validation           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   FEATURE ENGINEERING                           β”‚
β”‚  Price Lags β”‚ Returns β”‚ Moving Averages β”‚ Volatility β”‚ RSI     β”‚
β”‚  MACD β”‚ Bollinger Bands β”‚ Cross-commodity β”‚ Calendar Features   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    MODEL ENSEMBLE                               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚  LSTM   β”‚  β”‚ BiLSTM  β”‚  β”‚ Transformer β”‚  β”‚ LSTM+Transf. β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β”‚                           β–Ό                                     β”‚
β”‚                  Weighted Ensemble                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      PREDICTIONS                                β”‚
β”‚  1-Day Forecast β”‚ 5-Day Forecast β”‚ 10-Day Forecast              β”‚
β”‚  Confidence Intervals β”‚ Direction Probability                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“Š Model Performance

Backtested on 2 years of out-of-sample data (2023-2025):

Model RMSE MAE MAPE Directional Accuracy
LSTM 1.847 1.423 2.31% 54.2%
BiLSTM 1.756 1.352 2.18% 55.8%
Transformer 1.892 1.478 2.45% 53.1%
Hybrid (LSTM+Transformer) 1.698 1.287 2.04% 57.3%

Results on Crude Oil WTI daily price prediction


πŸš€ Quick Start

Installation

git clone https://github.com/YOUR_USERNAME/commodity-predictor.git
cd commodity-predictor
pip install -r requirements.txt

Run Demo

# Train models and generate predictions with sample data
python demo.py

Train on Real Data

# Full training pipeline
python train.py --commodity "Crude Oil WTI" --epochs 100

πŸ“ Project Structure

commodity-predictor/
β”œβ”€β”€ models.py                 # Neural network architectures
β”œβ”€β”€ feature_engineering.py    # Technical indicator generation
β”œβ”€β”€ data_loader.py           # Data fetching and preprocessing
β”œβ”€β”€ train.py                 # Training pipeline
β”œβ”€β”€ demo.py                  # Demo with sample data
β”œβ”€β”€ dashboard.html           # Interactive web dashboard
β”œβ”€β”€ sample_data.py           # Synthetic data generator
└── requirements.txt         # Dependencies

🧠 Model Details

BiLSTM Architecture

class BiLSTMModel(nn.Module):
    """
    Bidirectional LSTM for capturing both forward 
    and backward temporal patterns.
    
    Architecture:
    - Input projection layer
    - 2-layer Bidirectional LSTM (hidden_size=64)
    - Dropout regularization (p=0.2)
    - Fully connected output layer
    """

Feature Engineering

The system generates 100+ features including:

Category Features
Lag Features 1, 2, 3, 5, 7, 14, 21, 30, 60, 90 day lags
Returns 1, 5, 10, 21, 63 day returns
Moving Averages SMA & EMA (5, 10, 20, 50, 100, 200)
Volatility Rolling std dev (5, 10, 21, 63 day)
Momentum RSI, MACD, Rate of Change
Bands Bollinger Band position & width
Cross-Commodity Gold/Silver ratio, WTI/Brent spread
Calendar Day of week, month, seasonality encoding

πŸ“ˆ Dashboard

The interactive dashboard provides:

  • Real-time price cards with prediction badges
  • Historical charts with forecast overlay
  • Multi-horizon forecasts (1, 5, 10 days)
  • Model performance metrics

Dashboard Preview


πŸ”Œ API Access

Predictions are available via JSON endpoint:

fetch('https://YOUR_USERNAME.github.io/commodity-predictor/predictions/sample_predictions.json')
  .then(response => response.json())
  .then(data => {
    console.log(data.predictions['Crude Oil WTI'].forecasts['1d']);
    // { price: 69.15, change_pct: 0.63 }
  });

Premium API with real-time predictions available β€” Contact for access


πŸ“š Documentation


πŸ› οΈ Tech Stack

  • Deep Learning: PyTorch
  • Data Processing: Pandas, NumPy
  • Data Source: Yahoo Finance, FRED
  • Visualization: Chart.js, Matplotlib
  • Deployment: GitHub Actions, GitHub Pages

πŸ“„ License

MIT License β€” see LICENSE for details.


πŸ“¬ Contact

Brian Curry β€” brian at vector1.ai


πŸ™ Acknowledgments

  • Yahoo Finance for market data
  • World Bank for historical commodity data
  • Research papers on LSTM and Transformer architectures for financial forecasting

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •