End-to-end Machine Learning project for predicting the next-hour direction of XAU/USD (Gold vs US Dollar). The API accepts raw OHLC market candles, performs feature engineering internally, and serves predictions through a FastAPI inference service.
![]()
![]()
![]()
![]()
Financial markets are highly dynamic and difficult to predict. This project develops an end-to-end machine learning pipeline that predicts the next-hour movement of XAU/USD using historical price action and engineered technical features.
The project covers the complete machine learning workflow:
- Data preprocessing
- Exploratory Data Analysis (EDA)
- Feature Engineering
- Model Training
- Model Evaluation
- Model Analysis
- FastAPI Deployment
- Automated API Testing
Rather than focusing only on model accuracy, the project emphasizes building a production-style machine learning workflow that is modular, reproducible, and deployable.
Unlike many ML demonstrations that require clients to compute engineered features before inference, this project exposes a production-oriented API.
Clients provide historical OHLC market candles, while the API performs feature engineering internally before generating predictions. This keeps the client interface simple, reduces implementation errors, and mirrors real-world machine learning deployment practices.
Historical XAU/USD Data
│
▼
Data Cleaning & Processing
│
▼
Feature Engineering
(26 Technical Features)
│
▼
Chronological Train/Test Split
│
▼
Model Training
(Logistic Regression & Gradient Boosting)
│
▼
Model Evaluation
│
▼
FastAPI Deployment
│
▼
Raw OHLC Candles
│
▼
API Feature Engineering
│
▼
Prediction
- Asset: XAU/USD (Gold vs US Dollar)
- Frequency: 1 Hour
- Historical Market Data
- Time-series forecasting setup
- Chronological train-test split to prevent data leakage
The final model uses 26 engineered features, including:
The FastAPI service computes these features internally from raw OHLC market candles, allowing API clients to send only historical market data rather than pre-engineered features.
- return_1h
- return_3h
- return_6h
- return_12h
- return_24h
- momentum_3h
- momentum_6h
- momentum_12h
- momentum_24h
- body_size
- range_size
- upper_wick_ratio
- lower_wick_ratio
- up_candle_ratio_12h
- RSI (14)
- ATR Ratio
- SMA 6 Ratio
- SMA 12 Ratio
- SMA 24 Ratio
- volatility_6h
- volatility_12h
- volatility_24h
- trend_strength_24h
- hour
- day_of_week
- close_position
The following models were evaluated:
- Logistic Regression
- Gradient Boosting Classifier
The final deployed model is:
Logistic Regression
Selected due to stable performance, simplicity, interpretability, and consistent generalization on the chronological test set.
| Metric | Score |
|---|---|
| Accuracy | 0.5079 |
| Precision | 0.5251 |
| Recall | 0.4392 |
| F1 Score | 0.4784 |
- Performance is slightly above random classification.
- Financial market direction remains inherently difficult to predict.
- Prediction confidence is generally concentrated near the decision threshold.
- The model demonstrates stable behaviour across the chronological test period.
The trained model is deployed as a REST API using FastAPI.
GET /
Returns API status.
GET /health
Returns:
- API status
- Model loading status
- Number of engineered features
POST /predict
Accepts approximately 30 historical OHLC candles.
The API performs feature engineering internally and predicts the direction of the next hourly candle.
Example response:
{
"predicted_class": 1,
"predicted_direction": "UP",
"confidence": 0.5626
}Interactive Swagger documentation is automatically available at:
http://127.0.0.1:8000/docs
Automated tests verify:
- Root endpoint
- Health endpoint
- Prediction endpoint using raw OHLC candles
- Feature engineering output
- Feature completeness
All API tests pass successfully.
📦 xauusd-direction-classifier
│
├── 📂 api
│ ├── __init__.py
│ └── main.py
│
├── 📂 data
│ ├── raw
│ └── processed
├── 📂 images
├── 📂 models
│
├── 📂 notebooks
│
├── 📂 src
│
├── 📂 tests
│
├── README.md
├── requirements.txt
└── .gitignore
Clone the repository
git clone https://github.com/yourusername/xauusd-direction-classifier.git
cd xauusd-direction-classifierCreate virtual environment
python -m venv .venvActivate environment
Windows
.venv\Scripts\activateInstall dependencies
pip install -r requirements.txtpython -m uvicorn api.main:app --reloadOpen
http://127.0.0.1:8000/docs
python -m pytest| Category | Technologies |
|---|---|
| Language | Python |
| ML | Scikit-Learn |
| API | FastAPI |
| Model Serving | Uvicorn |
| Testing | Pytest |
| Visualization | Matplotlib |
| Data | Pandas, NumPy |
- Hyperparameter Optimization
- XGBoost & LightGBM
- LSTM-based Forecasting
- Transformer Models
- Live Market Prediction
- Docker Deployment
- CI/CD Pipeline
- Cloud Deployment (AWS)
- Real-time Dashboard
- Probability Calibration
- Feature Importance Analysis
- Financial markets exhibit significant randomness and non-stationarity.
- The model should not be interpreted as a trading strategy.
- Prediction performance remains only slightly above random classification.
- Additional market indicators and macroeconomic features could improve performance.
This project is provided for educational and research purposes.




