TrendCandleEA: A Multi-Filter Trend Continuation Expert Advisor for XAUUSD
A self-contained MQL5 Expert Advisor — no external indicator required. Two calibrated configurations (Balanced and Selective) validated on 9 months of 100 % real-tick backtest data.
Table of Contents
- Introduction
- Strategy Philosophy — Trend Continuation Candles
- The Filter Stack
- Two Configurations: Balanced vs Selective
- Backtest Environment
- Config 1.2 — Selective: Results in Depth
- Config 1.1 — Balanced: Results in Depth
- Head-to-Head Comparison
- Risk Management & Position Sizing
- How to Use the EA
- Conclusion
- Useful Links
1. Introduction
XauTrendCandleEA is an MQL5 Expert Advisor built specifically for XAUUSD on the M30 timeframe. It is a trend continuation system: it looks for a dominant directional bias, then enters on the first sign of momentum resuming after a pullback, and exits either on a fixed 1:1 reward-to-risk target or a trailing stop.
The distinctive feature of this EA is that it is completely self-contained. Every filter — Bollinger Bands, ZigZag, Squeeze Momentum, MACD, ATR, and the Virgin Price detection — is computed internally from the raw price series. There is no iCustom call, no external .ex5 dependency, no chart indicator required. The EA will compile and run on any MetaTrader 5 terminal, on any broker, without any additional download.
The strategy combines 11 independent filters, each of which can be enabled or disabled via inputs. This modular design allows the trader to build a filter stack ranging from very permissive (many trades, lower selectivity) to very strict (few trades, high conviction).
Two production-ready configurations are shipped and fully documented in this article:
- Config 1.1 — Balanced ( InpVirginCloseBars = 1000 ) — more trades, moderate win rate, higher drawdown.
- Config 1.2 — Selective ( InpVirginCloseBars = 10000 ) — fewer trades, high win rate, low drawdown, higher Profit Factor.
The only difference between the two configurations is a single input: the depth of the Virgin Price lookback. Everything else is identical.
2. Strategy Philosophy — Trend Continuation Candles
The base idea is deliberately simple: a strong trending market rarely reverses on the first pullback. When price is above a rising reference (trend filter), a bullish continuation candle — one that closes near its high with a solid body — is statistically more likely to be followed by another bullish candle than by an immediate reversal.
The EA encodes this logic into a minimal rule set:
- Bullish context + a closed green candle with a body and a close near its high → BUY
- Bearish context + a closed red candle with a body and a close near its low → SELL
- Stop Loss = low of the signal candle (minus a configurable buffer) for BUY; high of the signal candle (plus the same buffer) for SELL.
- Take Profit = same distance as SL (1:1 R:R by default), or a fixed distance, or a trailing stop.
On its own, this rule set is too permissive — it would fire on every green candle in an uptrend. The entire value of the EA lies in the filter stack that decides which continuation candles are actually worth trading.
3. The Filter Stack
Each filter below can be toggled independently. When disabled, it has strictly zero effect on the strategy (the corresponding handle is not even created). The default configuration for XAUUSD M30 uses the following active filters:
3.1 ADX Filter (Trend Strength)
A minimum ADX value ensures the market is trending, not ranging. The default is ADX(7) ≥ 25 . This is the primary gate that prevents the EA from trading during consolidation phases.
3.2 RSI Filter (Directional Confirmation)
RSI(14) must be above 65 for a BUY and below 35 for a SELL. This forces entries to occur only when momentum is already established in the direction of the trade.
3.3 MACD Filter (Momentum)
MACD(12, 26, 9) in Momentum mode: the MACD line must be above its signal line for a BUY, below for a SELL. Reads are performed on closed candles only — no repainting.
3.4 Bollinger Bands — Modality A (Median Line)
On the H3 timeframe, price must be on the correct side of the 200-period median line, and the median line itself must be sloping in the direction of the trade. This confirms that the higher timeframe structure supports the entry.
3.5 Bollinger Bands — Modality B (Outer Band Touch)
On the M30 timeframe, the signal candle's wick must have touched or exceeded the outer band of a very tight BB(200, 0.5). This confirms that the entry occurs at a local extreme — the point of maximum tension before the continuation resumes.
3.6 ZigZag Filter (Trend Leg Tracking)
An internal ZigZag (Depth 24, Deviation 5, Backstep 3) determines the direction of the current market leg. The EA only trades in the direction of that leg. This filter alone was responsible for the 100 % long bias observed in the backtest below — every bearish leg filtered out every SELL signal.
3.7 Virgin Price Filter (The Key Differentiator)
This filter checks that the close price of the signal candle has not been touched by any of the N previous candles. If any prior candle — wick or body, depending on configuration — has already crossed that price level, the trade is rejected.
— N = 1 000 candles (Config 1.1 — Balanced): about 21 days of M30 history.
— N = 10 000 candles (Config 1.2 — Selective): about 7 months of M30 history.
The rationale is that a price level that has never been visited before acts as a liquidity vacuum — there is no resting order flow to reject the move, so the continuation is more likely to run cleanly. The longer the lookback, the more stringent the requirement, and the fewer signals pass.
3.8 Additional Filters (Available, Off by Default)
The EA also includes a Matrend MA filter, a Higher-Timeframe MA confirmation, a candle quality filter, a session filter, a directional RSI filter, a Stochastic filter, an IBS filter, a Squeeze Momentum filter, a Fibonacci retracement filter, a Smart Money Concept (SMC) filter, a second ZigZag on a user-chosen timeframe, a volume filter, a Volumes Indicator filter, and an ATR-based body filter. Each is fully configurable and documented inside the source code.
4. Two Configurations: Balanced vs Selective
| Parameter | Config 1.1 — Balanced | Config 1.2 — Selective |
|---|---|---|
| Symbol | XAUUSD | XAUUSD |
| Timeframe | M30 | M30 |
| InpVirginCloseBars | 1 000 | 10 000 |
| Virgin lookback duration | ~21 days | ~7 months |
| All other inputs | Identical | Identical |
| Trades (9 months) | 98 | 37 |
| Win rate | 72,45 % | 83,78 % |
| Profit Factor | 2,65 | 5,60 |
| Balance Drawdown Max | 6,55 % | 4,25 % |
| Net Profit | +151,3 % | +79,7 % |
Config 1.1 — Balanced is designed for traders who want a higher trade frequency and accept a slightly higher drawdown in exchange for more total profit. Config 1.2 — Selective is designed for traders who prioritize win rate, low drawdown and a smooth equity curve, and are comfortable waiting for a smaller number of higher-conviction setups.
5. Backtest Environment
| Item | Value |
|---|---|
| Broker | Tickmill-Demo |
| Platform | MetaTrader 5 (Build 6182) |
| Symbol | XAUUSD |
| Timeframe | M30 |
| Period | 2026.01.01 → 2026.09.09 (9 months) |
| Modelling | Every tick based on real ticks |
| History quality | 100 % real ticks |
| Initial deposit | 1 000 USD |
| Leverage | 1:500 |
| Bars | 8 107 |
| Ticks | 58 258 660 |
Both configurations were tested on the exact same historical data, with the exact same EA build and the exact same input values, except for the single InpVirginCloseBars parameter. This makes the comparison rigorously controlled.
6. Config 1.2 — Selective: Results in Depth
| 37 Trades | 83,78 % Win Rate | 5,60 Profit Factor | 4,25 % DD Balance Max | 31,21 Sharpe Ratio | 9,16 Recovery Factor |
6.1 Global Statistics
| Metric | Value |
|---|---|
| Total Net Profit | 796,86 USD |
| Gross Profit | 969,98 USD |
| Gross Loss | −173,12 USD |
| Profit Factor | 5,60 |
| Expected Payoff | 21,54 USD / trade |
| Sharpe Ratio | 31,21 |
| Recovery Factor | 9,16 |
| Balance Drawdown Maximal | 56,68 USD (4,25 %) |
| Equity Drawdown Maximal | 87,04 USD (5,06 %) |
| Total Trades | 37 |
| Profit Trades | 31 (83,78 %) |
| Loss Trades | 6 (16,22 %) |
| Average Profit Trade | 31,29 USD |
| Average Loss Trade | −28,85 USD |
| Largest Profit Trade | 82,96 USD |
| Largest Loss Trade | −31,80 USD |
| Max Consecutive Wins | 12 (333,27 USD) |
| Max Consecutive Losses | 2 (−56,68 USD) |
| Average Holding Time | 3 h 16 min |
6.2 Directional Breakdown
All 37 trades were BUY. Zero SELL signals passed the filter stack during the test period. This is not a bug — it reflects a strong bull trend in XAUUSD over the test window combined with the ZigZag leg-direction filter, which blocked every SELL attempt during pullbacks.
6.3 Key Observations
- Exceptional risk-adjusted return: Sharpe of 31 is extremely high and reflects a very smooth equity curve.
- Low drawdown: 4,25 % maximum balance drawdown for a +79,7 % net return is a very favourable ratio.
- High win rate: 83,78 % wins is sustainable because the loss/win average is nearly symmetric (≈ −29 vs +31 USD), so the win rate alone drives profitability.
- Sparse trading: 37 trades over 9 months is about 4 per month. Patience is required.
7. Config 1.1 — Balanced: Results in Depth
| 98 Trades | 72,45 % Win Rate | 2,65 Profit Factor | 6,55 % DD Balance Max | 19,74 Sharpe Ratio | 9,16 Recovery Factor |
7.1 Global Statistics
| Metric | Value |
|---|---|
| Total Net Profit | 1 512,70 USD |
| Gross Profit | 2 427,05 USD |
| Gross Loss | −914,35 USD |
| Profit Factor | 2,65 |
| Expected Payoff | 15,44 USD / trade |
| Sharpe Ratio | 19,74 |
| Recovery Factor | 9,16 |
| Balance Drawdown Maximal | 148,20 USD (6,55 %) |
| Equity Drawdown Maximal | 165,16 USD (7,30 %) |
| Total Trades | 98 |
| Profit Trades | 71 (72,45 %) |
| Loss Trades | 27 (27,55 %) |
| Average Profit Trade | 34,18 USD |
| Average Loss Trade | −33,86 USD |
| Largest Profit Trade | 82,96 USD |
| Largest Loss Trade | −96,46 USD |
| Max Consecutive Wins | 12 (333,27 USD) |
| Max Consecutive Losses | 3 (−145,62 USD) |
| Average Holding Time | 3 h 43 min |
7.2 Directional Breakdown
62 long trades (77,42 % win rate) and 36 short trades (63,89 % win rate). The shorter lookback of the Virgin Price filter let more SELL signals through, giving the strategy a more balanced long/short exposure.
7.3 Key Observations
- Higher absolute return: +151,3 % vs +79,7 %. The EA trades roughly 2,6× more often and accumulates more total profit.
- Higher drawdown: 6,55 % vs 4,25 %. Still very contained for a 9-month period on gold.
- Lower win rate: 72,45 % vs 83,78 %. The extra trades include lower-quality setups.
- Larger loss tail: the largest loss trade reaches −96,46 USD vs −31,80 USD in the Selective config.
8. Head-to-Head Comparison
| Metric | Config 1.1 — Balanced | Config 1.2 — Selective | Winner |
|---|---|---|---|
| Trades | 98 | 37 | Balanced (frequency) |
| Net Profit | 1 512,70 USD | 796,86 USD | Balanced |
| Net Profit % | +151,3 % | +79,7 % | Balanced |
| Profit Factor | 2,65 | 5,60 | Selective |
| Win Rate | 72,45 % | 83,78 % | Selective |
| Balance DD Max | 6,55 % | 4,25 % | Selective |
| Equity DD Max | 7,30 % | 5,06 % | Selective |
| Sharpe Ratio | 19,74 | 31,21 | Selective |
| Expected Payoff | 15,44 USD | 21,54 USD | Selective |
| Largest Loss Trade | −96,46 USD | −31,80 USD | Selective |
| Long/Short Balance | 62 / 36 | 37 / 0 | Balanced |
| Recovery Factor | 9,16 | 9,16 | Tie |
— Choose Config 1.1 — Balanced if you want more trading activity, more total profit, and are comfortable with a slightly higher drawdown and a less favourable win rate.
— Choose Config 1.2 — Selective if you prefer a higher win rate, a smoother equity curve, a lower drawdown, and are patient enough to wait for the best setups.
9. Risk Management & Position Sizing
The EA supports two lot-sizing modes:
- Fixed lot ( LOT_FIXED ) — the same volume is used for every trade. Simplest, but does not adapt to account growth or SL distance.
- Risk-based lot ( LOT_RISK_PCT ) — the lot is computed so that the distance between entry and SL corresponds to a fixed percentage of account equity. Recommended for live trading.
9.1 Exit Modes
Exactly one exit mode must be enabled at a time:
- Fixed TP in points — a hard take-profit at a user-defined distance.
- Fixed TP = SL distance (1:1) — the default used in both backtests.
- Trailing SL, fixed step — the SL follows price by a constant number of points.
- Trailing SL, step = SL distance — the step adapts to the trade's initial risk.
- Trailing SL, step = SL × multiplier — the step doubles (or grows by a custom multiplier) at each move.
In both configurations documented here, Mode 2 (1:1 R:R) was used with a InpSlBufferPoints = 1000 buffer — i.e. the SL is placed 1 000 points (10 USD) below the low of the signal candle for BUY, and 1 000 points above the high for SELL.
9.2 Capital Protection
The EA includes a InpMaxDailyLossPct input that stops all trading for the rest of the day once the realised loss for that day exceeds a defined percentage of capital. This is disabled by default ( 0.0 ) but is strongly recommended when running live.
10. How to Use the EA
10.1 Installation
- Open MetaTrader 5.
- In the Navigator, right-click Expert Advisors → Open Folder.
- Copy XauTrendCandleEA.mq5 into that folder.
- In MetaEditor, press F7 to compile. No dependency is required.
- Drag the EA from the Navigator onto an XAUUSD M30 chart.
- In the input window, paste the parameters of the chosen configuration.
10.2 Recommended Initial Settings
For Config 1.2 — Selective:
InpMagic = 1003 InpTimeframe = PERIOD_CURRENT (M30) InpSlippagePoints = 30 InpMaxSpreadPoints = 60 InpUseAdxFilter = true InpAdxPeriod = 7 InpAdxMin = 25.0 InpUseRsiFilter = true InpRsiPeriod = 14 InpRsiOverbought = 65.0 InpRsiOversold = 35.0 InpUseMacdFilter = true InpUseBollingerMiddleFilter = true InpUseBollingerOuterTouchFilter = true InpUseZigZagFilter = true InpUseVirginCloseFilter = true InpVirginCloseBars = 10000 <-- Selective InpSlBufferPoints = 1000 InpMode_TP_EgalSL = true InpLotMode = LOT_FIXED InpFixedLot = 0.01
For Config 1.1 — Balanced, change only:
InpVirginCloseBars = 1000 <-- Balanced
10.3 Operational Notes
- The EA trades only on closed candles. Signals are evaluated once per M30 bar close.
- The trailing stop is managed on every tick, independently of new bar detection.
- Trailing state is persisted in global variables, so the EA recovers cleanly after a terminal restart, a disconnection, or a power outage.
- ZigZag and Volumes Indicator chart objects are automatically created and removed by the EA.
11. Conclusion
XauTrendCandleEA is a fully self-contained trend continuation system tailored to XAUUSD M30. Its 11-filter stack — with the Virgin Price filter playing the central role — allows the trader to tune the EA's behaviour from a moderately active profile to a highly selective one, without touching anything except a single input.
Over 9 months of 100 % real-tick backtest on Tickmill-Demo:
- Config 1.1 — Balanced produced 98 trades, a 72,45 % win rate, a Profit Factor of 2,65, a 6,55 % maximum drawdown, and +151,3 % net profit.
- Config 1.2 — Selective produced 37 trades, an 83,78 % win rate, a Profit Factor of 5,60, a 4,25 % maximum drawdown, and +79,7 % net profit.
Both configurations deliver positive expectancy, controlled drawdown, and robustness. The choice between them is a matter of trading style, not of quality.
Thank you for reading. Feedback, bug reports and suggestions are welcome in the comments section.


