RSI/MACD Divergence EA Template for MetaTrader 5
Divergence between price and a momentum indicator is one of the most reliable reversal signals in technical analysis. This free EA template automatically detects bullish and bearish divergence using either RSI or MACD, with configurable swing point detection and ATR-based risk management. Best for EURUSD, GBPUSD, and USDJPY on H4 to D1 timeframes. Build it in AlgoStudio without coding and export a production-ready MQL5 Expert Advisor in minutes.
What Is a Divergence Trading Strategy?
Divergence occurs when price action and a momentum indicator move in opposite directions. For example, if price makes a new higher high but the RSI makes a lower high, this bearish divergence suggests that the uptrend is losing momentum and a reversal may be imminent. Conversely, if price makes a lower low but the RSI makes a higher low, this bullish divergence suggests the downtrend is weakening.
Divergence works because momentum typically leads price. When an indicator stops confirming new price extremes, it signals that the underlying buying or selling pressure is fading. The move may continue for a while on pure inertia, but the probability of a reversal increases significantly.
This is a reversal strategy — it goes against the current price direction, which makes proper risk management essential. Divergence signals are not immediate: price can diverge for several bars before reversing. The template uses ATR-based stops and a fixed risk-reward ratio to manage this uncertainty, ensuring losses are controlled while allowing room for the reversal to play out.
How This EA Template Works
The EA scans the last N bars (default 20) for swing highs and swing lows in both price and the selected indicator. It then compares the two most recent swing points to determine if divergence exists. The minimum swing bars setting (default 5) ensures that the two swing points are separated by enough bars for the divergence pattern to be meaningful — very close swing points produce unreliable signals.
When using RSI, the indicator value at each price swing point is compared directly. When using MACD, the histogram values at swing points are compared. Both methods are effective, but they detect slightly different types of momentum shifts. RSI divergence is more common and easier to validate visually. MACD divergence tends to signal larger reversals but produces fewer trades.
Default Parameters
These defaults work well on major pairs on H4 and D1 timeframes. All parameters are exported as input variables so you can optimize them in the MT5 Strategy Tester.
| Parameter | Value | Type |
|---|---|---|
| Indicator | RSI (or MACD) | Selection |
| RSI Period | 14 | Indicator |
| MACD Settings | 12/26/9 | Indicator |
| Lookback Bars | 20 | Detection |
| Min Swing Bars | 5 | Detection |
| Stop Loss | 1.5x ATR(14) | ATR-based |
| Take Profit | 2:1 R:R | Risk-reward |
| Position Sizing | 1% risk per trade | Risk |
How to Build This EA Without Coding
1. Create a new project in AlgoStudio
Sign up for free (no credit card required) and click “New Project”. Name your project “Divergence Strategy” and open the visual builder canvas.
2. Add the RSI/MACD Divergence entry strategy block
Drag an RSI/MACD Divergence entry strategy block onto the canvas. Choose RSI or MACD as your indicator. Set the lookback bars to 20 and minimum swing bars to 5. If using RSI, keep the default period of 14. If using MACD, keep the standard 12/26/9 settings.
3. Configure risk management
Set the stop loss to 1.5x ATR(14), take profit to 2:1 risk-reward, and position sizing to 1% risk per trade. Since divergence is a reversal strategy, having proper risk management is critical — not every divergence signal results in a reversal, and losses must be kept small relative to your account.
4. Export, backtest, and optimize
Click Export to generate a .mq5 file. Load it into MetaTrader 5 and backtest on EURUSD H4 or D1 with at least 3 years of historical data. Divergence strategies produce fewer trades, so you need a longer backtest period for meaningful results. Optimize lookback bars (15-30) and minimum swing bars (3-8). Demo trade for 2-3 months before going live.
Optimization Tips
Use higher timeframes for more reliable signals
Divergence on H4 and D1 is far more reliable than on H1 or lower. Higher timeframes produce cleaner swing points and more meaningful divergence patterns. The trade-off is fewer signals — you might get 2-4 trades per month on D1 versus 8-12 on H4. Quality over quantity is the key principle for divergence trading.
Combine with support/resistance levels
Divergence at a key support or resistance level is significantly more powerful than divergence in the middle of a range. If you see bullish divergence right at a major support level, the probability of reversal is much higher. You can add a session filter or time filter to focus on setups that coincide with significant price levels.
Accept lower trade frequency
Divergence strategies inherently produce fewer trades than trend-following or momentum strategies. Trying to increase trade frequency by loosening the detection parameters (fewer lookback bars, less strict swing detection) usually degrades signal quality. If your backtest shows more than 1-2 trades per week on H4, your detection may be too sensitive and you're likely finding false divergence patterns.