← All projects

Gold (XAUUSD) Trend-Prediction Models

Machine-learning system that predicts 4-hour gold direction from price, cross-asset and positioning data, validated walk-forward and served through a FastAPI signal API.

Client
Gold-trading client
Year
2025
Category
Quant & ML
Stack
  • Python
  • XGBoost
  • LightGBM
  • scikit-learn
  • PyTorch
  • FastAPI
  • Parquet
  • pytest

The problem

The client wanted a directional model for gold on the 4-hour chart that bots could query. The first model looked fine on average but failed in a strongly trending test period. Its down-calls were unreliable, because it was trained on balanced classes and tested on a one-sided market.

What I built

  • Data pipeline: gold bars plus correlated assets in three correlation tiers, and CFTC positioning data. Daily data is shifted one day and COT data four days, so no feature uses information that was not public yet.
  • Features: about 50 engineered features, covering technical indicators, multi-factor regime detection (momentum, trend, volatility), sentiment and calendar effects.
  • Model stack: a weighted tree ensemble (Random Forest, XGBoost, LightGBM), optional LSTM and Transformer models, and a stacking layer on top.
  • Diagnosis and fix: I tested eight combinations of fixes. Walk-forward retraining combined with asymmetric, regime-aware sample weights was the pair that worked together.
  • Threshold tuning: separate confidence thresholds for up and down calls, so the model can still act on the weaker side.
  • Serving: a FastAPI server with health, status, live prediction, historical point-in-time prediction for backtests, and a lightweight signal endpoint for bots.
  • Engineering: YAML config, a CLI for collect, train, walk-forward, predict and backtest, and a pytest unit suite.

Details

Validation uses temporal train/validation/test splits and rolling walk-forward windows, never random cross-validation. The model was delivered as a packaged prediction server with a retraining guide.