Neural Network Entry Filter untuk XAUUSD — XGBoost classifier yang menyaring sinyal entry EA MetaTrader 5. Menggantikan grid/martingale dengan AI-based risk management.
SRMappingNN — Neural Network Entry Filter for XAUUSD
XGBoost classifier that filters entry signals for MetaTrader 5 EA.
Replacing grid/martingale with AI-based risk management.
Table of Contents
- About the Project
- System Architecture
- Model Results
- Repository Structure
- Roadmap & Workflow
- Installation & Usage
- Deployment Guide to MT5
- Technical Details
- Limitations & Important Notes
- License
About the Project
Background
The SRMappingFoundation v7.1 EA has strong entry signals (91.10% win rate, profit factor 2.37) but uses a 10x grid martingale for risk management, causing:
- Max Equity Drawdown: 40.69% (dangerous)
- High margin call risk in trending market conditions
Solution
Building an XGBoost classifier that learns from market patterns to filter entries:
- GOOD entry = price moves directly toward Take Profit
- BAD entry = price will hit Stop Loss
Original EA Backtest (11 months, Dec 2024 — Nov 2025)
| Metric | Value | |--------|-------| | Total Trades | 820 | | Win Rate | 91.10% | | Profit Factor | 2.37 | | Net Profit | $57,893 (from $1,000) | | Max DD | 40.69% (PROBLEM) |
System Architecture
┌─────────────────────────────────────────────────────┐
│ MARKET DATA (Live) │
│ XAUUSD M1/M30/D1 from MetaTrader 5 │
└────────────────────┬────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────┐
│ SRMappingNN_v1.mq5 (EA in MT5) │
│ │
│ 1. BuildSNR() → Fractal S/R levels │
│ 2. GetRSI_M1() → RSI(14) on M1 │
│ 3. GetATRM1Points() → ATR(14) on M1 │
│ 4. DetectDailyRange() → Daily direction (+1/0/-1) │
│ 5. Signal Check → BUY/SELL conditions │
│ │
│ ┌──────────────────────────────────────────────┐ │
│ │ NN FILTER (NEW COMPONENT) │ │
│ │ │ │
│ │ PrepareFeatures() → 26 real-time features │ │
│ │ ↓ │ │
│ │ srmappingnn.onnx → XGBoost inference │ │
│ │ ↓ │ │
│ │ confidence >= 0.51 ? │ │
│ │ YES → ExecuteBuy/Sell (with SL/TP) │ │
│ │ NO → SKIP (log reason) │ │
│ └──────────────────────────────────────────────┘ │
│ │
│ 6. ManageSmartTrailing() → Active trailing stop │
└─────────────────────────────────────────────────────┘
Model Results
Performance on Test Set (Sep — Nov 2025)
| Metric | Original EA | EA + NN Filter | |--------|-------------|----------------| | Total Trades | 606 | 29 | | Win Rate | 51.3% | 72.4% | | Precision | — | 72.4% | | Signals/day | ~14 | ~0.5 |
Charts
Feature Importance
Confusion Matrix
Precision-Recall Curve
Equity Curve Comparison
Repository Structure
SRMappingNN/
├── README.md # Main documentation (this file)
├── requirements.txt # Python dependencies
├── .gitignore # Git ignore rules
│
├── scripts/ # Python scripts
│ ├── 01downloaddata.py # Step 1: Download XAUUSD data
│ ├── 02featureengineering.py # Step 2: Compute all features
│ ├── 03_labeling.py # Step 3: Forward-looking labels
│ ├── 04trainmodel.py # Step 4: Train XGBoost
│ ├── 05exportonnx.py # Step 5: Export to ONNX
│ ├── 06generatecharts.py # Step 6: Generate visualizations
│ ├── 07equitysimulation.py # Step 7: Simulate equity curve
│ └── runfullpipeline.py # Run all steps at once
│
├── mql5/ # MetaTrader 5 files
│ ├── SRMappingNN_v1.mq5 # Main EA (ONNX-enabled)
│ └── DEPLOYMENT_GUIDE.md # MT5 deployment guide
│
├── models/ # Trained models
│ ├── srmappingnn.onnx # ONNX model for MT5
│ ├── model_xgboost.pkl # XGBoost pickle
│ └── model_xgboost.json # XGBoost JSON (backup)
│
├── configs/ # Configuration files
│ ├── feature_config.json # Features + thresholds + normalization
│ ├── pipeline_results.json # Training results
│ └── threshold_analysis.csv # Full threshold analysis
│
├── data/ # Datasets
│ ├── xauusd_h1.csv # OHLCV H1 (Oct 2024 — Nov 2025)
│ ├── xauusd_d1.csv # OHLCV D1 (Jan 2023 — Nov 2025)
│ ├── training_data.csv # Training dataset (4,040 samples)
│ ├── test_predictions.csv # Predictions on test set
│ └── equity_curves.csv # Equity curve simulation
│
├── charts/ # Visualizations
│ ├── feature_importance.png # Feature importance ranking
│ ├── confusion_matrix.png # Confusion matrix
│ ├── precisionrecallcurve.png # Precision-recall curve
│ └── equity_curve.png # Equity curve comparison
│
└── docs/ # Additional documentation
├── VALIDATION_REPORT.md # Full validation report
├── EA_LOGIC.md # EA logic documentation
├── FEATURE_DICTIONARY.md # Feature explanations
└── WORKFLOW.md # Full workflow diagram
Roadmap & Workflow
Overview: 7-Step Pipeline
STEP 1 STEP 2 STEP 3 STEP 4
Download → Feature → Labeling → Training
XAUUSD Data Engineering (TP/SL sim) XGBoost
STEP 5 STEP 6 STEP 7
ONNX → Generate EA → Validation &
Export MQL5 Code Reporting
Step-by-Step Detail
STEP 1: Download Data (scripts/01downloaddata.py)
Input: Ticker GC=F from yfinance
Output: data/xauusdh1.csv, data/xauusdd1.csv
- Download H1 OHLCV (Oct 2024 — Nov 2025): 6,711 bars
- Download D1 OHLCV (Jan 2023 — Nov 2025): 732 bars
- Validation: check missing data, weekend gaps, holidays
STEP 2: Feature Engineering (scripts/02featureengineering.py)
Input: data/xauusdh1.csv, data/xauusdd1.csv
Output: DataFrame with 26 features per bar
Features computed:
- Williams Fractals (lookback=2) → S/R levels
- RSI(14) on H1
- ATR(14) on H1
- Daily Open + Daily Range Direction
- Distance to S/R (normalized by ATR)
- Momentum (return 3/5/10/20 bars)
- Volatility regime (ATR percentile)
- Candle patterns (body, wicks, bullish/bearish)
- Time features (hour, dayofweek)
- Bars since last fractal
STEP 3: Labeling (scripts/03_labeling.py)
Input: Feature DataFrame from Step 2 Output: data/training_data.csv (4,040 samples)
For each bar where daily direction != 0:
- TP = entry ± ATR × 2.0
- SL = entry ∓ ATR × 1.6
- Scan forward 48 bars (48 hours)
- Label = 1 if TP hit first (GOOD)
- Label = 0 if SL hit first (BAD)
- Timeout: use floating P/L
Distribution: 45.2% GOOD, 54.8% BAD
STEP 4: Training (scripts/04trainmodel.py)
Input: data/training_data.csv
Output: models/modelxgboost.pkl, configs/featureconfig.json
- Time-based split: Train 70% / Val 15% / Test 15%
- Try 3 configs: balanced, conservative, aggressive
- Select best config based on Val AUC
- Threshold optimization: target precision >= 70%
- Result: AUC=0.538, Precision=72.4% at threshold=0.51
STEP 5: ONNX Export (scripts/05exportonnx.py)
Input: models/model_xgboost.pkl
Output: models/srmappingnn.onnx
- Convert XGBoost → ONNX via onnxmltools
- Verification: ONNX vs original predictions (max diff: 0.000000)
- File size: 1.6 KB (very small, fast load in MT5)
STEP 6: Generate Charts (scripts/06generatecharts.py)
Input: configs/pipelineresults.json, data/testpredictions.csv, etc.
Output: charts/*.png (4 files)
- feature_importance.png — Feature ranking bar chart
- confusion_matrix.png — Confusion matrix heatmap
- precisionrecallcurve.png — P-R curve + optimal threshold
- equity_curve.png — Original vs NN equity comparison
STEP 7: Validation (scripts/07equitysimulation.py)
Input: data/testpredictions.csv, models/modelxgboost.pkl
Output: data/equitycurves.csv, docs/VALIDATIONREPORT.md
Equity simulation:
- Original EA: $10,000 → $11,946 (19.5%, 606 trades, WR 51.3%)
- EA + NN: $10,000 → $10,504 (5.0%, 29 trades, WR 72.4%)
Installation & Usage
Prerequisites
- Python 3.10+
- MetaTrader 5 (for EA deployment)
- Git
Quick Start
# 1. Clone repository
git clone https://github.com/Mrizalfahlepi/SRMappingNN.git
cd SRMappingNN
2. Install dependencies
pip install -r requirements.txt
3. Run full pipeline
python scripts/runfullpipeline.py
Or run step by step:
python scripts/01downloaddata.py
python scripts/02featureengineering.py
python scripts/03_labeling.py
python scripts/04trainmodel.py
python scripts/05exportonnx.py
python scripts/06generatecharts.py
python scripts/07equitysimulation.py
Retrain with Your Own Data
If you have XAUUSD data from your broker (Exness, etc.):
# 1. Save M30/H1 data to data/xauusd_custom.csv
Format: datetime,open,high,low,close,volume
2. Edit scripts/01downloaddata.py:
Set DATA_SOURCE = "custom"
Set CUSTOMFILE = "data/xauusdcustom.csv"
3. Run pipeline
python scripts/runfullpipeline.py
Deployment Guide to MT5
Quick Steps
- Copy EA:
mql5/SRMappingNN_v1.mq5→MQL5/Experts/ - Copy Model:
models/srmappingnn.onnx→MQL5/Files/ - Compile in MetaEditor (F7)
- Attach to XAUUSD chart (M30 or H1)
- Set parameters (see table below)
Required Parameters
| Parameter | Value | Description | |-----------|-------|-------------| | InpUseNNFilter | true | Enable NN filter | | InpConfidenceThresh | 0.51 | Confidence threshold | | InpUseStopLoss | true | REQUIRED (grid removed) | | InpUseATR_SLTP | true | ATR-based SL/TP | | InpSLATRMult | 1.6 | SL = ATR x 1.6 | | InpTPATRMult | 2.0 | TP = ATR x 2.0 | | InpUseSmartTrailing | true | Smart trailing active | | InpMaxOpenTrades | 1 | Max 1 position |
Entry Parameters
| Parameter | Value | Description | |-----------|-------|-------------| | InpRSI_Oversold | 40 | RSI BUY threshold | | InpRSI_Overbought | 70 | RSI SELL threshold | | InpATR_MinPoints | 200 | Minimum ATR | | InpATR_MaxPoints | 2500 | Maximum ATR | | InpSNR_Tolerance | 50 | S/R tolerance (points) | | InpDailyRangeThresh | 1000 | Daily range threshold | | InpTradingHourStart | 2 | Trading start (hour) | | InpTradingHourEnd | 18 | Trading end (hour) |
Full guide: mql5/DEPLOYMENT_GUIDE.md
Technical Details
EA Entry Logic (5 Simultaneous Conditions)
ALL conditions must be TRUE:
- IsWithinTradingHours() → 02:00-18:00 broker time
- totalPositions < 1 → max 1 trade
- IsMinBarsElapsed() → min 5 hours since last trade
- IsSpreadAcceptable() → spread <= 400 points
- IsATR_InRange() → ATR within 200-2500 points
BUY if:
- Daily Direction = +1 (bullish)
- Price near Support (±50 points)
- RSI <= 40
SELL if: - Daily Direction = -1 (bearish) - Price near Resistance (±50 points) - RSI >= 70
26 Model Features
| # | Feature | Description | |---|---------|-------------| | 1 | rsi | RSI(14) | | 2 | atr | ATR(14) in $ | | 3 | daily_range | Close - Daily Open | | 4 | daily_direction | +1 / 0 / -1 | | 5 | distresnorm | Distance to Resistance / ATR | | 6 | distsupnorm | Distance to Support / ATR | | 7 | sr_position | Position within S/R range (0-1) | | 8 | hour | Hour (0-23) | | 9 | dow | Day (0=Mon, 4=Fri) | | 10 | ret_3 | Return 3 bars (%) | | 11 | ret_5 | Return 5 bars (%) | | 12 | ret_10 | Return 10 bars (%) | | 13 | ret_20 | Return 20 bars (%) | | 14 | atr_pctile | ATR percentile (rolling 168) | | 15 | atr_change | ATR change 5 bars (%) | | 16 | rsi_sma | RSI SMA(10) | | 17 | rsi_slope | RSI change 3 bars | | 18 | near_support | 1 if near support | | 19 | near_resistance | 1 if near resistance | | 20 | body_size | Candle body / ATR | | 21 | upper_wick | Upper wick / ATR | | 22 | lower_wick | Lower wick / ATR | | 23 | is_bullish | 1 if bullish candle | | 24 | barssincefrac_up | Bars since UP fractal | | 25 | barssincefrac_down | Bars since DOWN fractal | | 26 | vol_ratio | Volume / SMA(20) volume |
Williams Fractal (S/R Detection)
# Fractal UP (Resistance):
High[i] > High[i-1] AND High[i] > High[i-2]
AND High[i] > High[i+1] AND High[i] > High[i+2]
Fractal DOWN (Support):
Low[i] < Low[i-1] AND Low[i] < Low[i-2]
AND Low[i] < Low[i+1] AND Low[i] < Low[i+2]
Carry forward: if no new fractal, use last known value
Limitations & Important Notes
Data
- Data source: GC=F (Gold Futures) from yfinance, not XAUUSD spot from Exness
- Timeframe: H1 (not M1/M30 like the original EA) due to public data limitations
- Period: 14 months (Oct 2024 — Nov 2025), ideally needs 2-3 years
- Spread: Estimated (not real spread from broker)
Model
- Moderate AUC (0.538): Model is not a super-predictor, but at high thresholds produces good precision
- Low recall (6.8%): Many good signals are skipped — safety vs opportunity trade-off
- Overfitting risk: Periodic retraining with new data is required
Deployment
- MUST backtest in Strategy Tester before going live
- MUST demo trade for at least 1 month
- Retrain recommended every 1-3 months with latest data
- Feature calibration: M1 features in EA will differ from H1 training — retrain with Exness data required
Priority Recommendations
| Priority | Action | Impact | |----------|--------|--------| | P0 | Retrain with Exness M1/M30 data | Significant accuracy improvement | | P0 | Backtest + Demo 1 month | Validate before live trading | | P1 | Walk-forward cross-validation | Model stability | | P1 | Ensemble model (XGB + LightGBM + RF) | Robustness | | P2 | Feature expansion (session, news) | More market information | | P2 | LSTM/GRU as alternative | Capture temporal patterns |
License
MIT License — See LICENSE for details.
DISCLAIMER: This project is for educational and research purposes. Forex/gold trading involves high risk. There is no guarantee of profit. Always use strict risk management and never trade with money you cannot afford to lose.
Built with Python, XGBoost, ONNX, and MQL5 By Muhamad Rizal Fahlepi This project is experimental — profit is not guaranteed. Happy Trading!