RSI Divergence EA
RSI divergence is one of the most commonly taught oscillator divergence setups: when price pushes to a new extreme but RSI fails to confirm it, momentum is fading even though price is still moving, and that disagreement is read as an early warning of a reversal. This EA implements it mechanically, sharing its swing-detection logic with the MACD divergence reference EA elsewhere in this catalogue, with RSI substituted for the oscillator comparison. A swing low only counts once InpPivotBars bars have closed on both sides of it with none of them lower - the same requirement that makes the pivot real is what stops the EA from ever repainting one. The unavoidable consequence is that every signal is InpPivotBars bars late: divergence trading always pays for a confirmed pivot with a delayed entry, and no amount of clever coding removes that trade-off, it just makes the lag explicit instead of hidden. The EA searches back from the newest possible pivot position for the two most recent confirmed swings and compares both the price extreme and the RSI reading at each. It only opens a trade on the bar where a fresh pivot is first confirmed, so a divergence that stays true for several bars in a row cannot fire the same signal repeatedly. The stop is sized from ATR rather than a fixed pip count, because how far price can reasonably travel before a divergence trade is proven wrong scales with recent volatility, not with a constant. Position sizing can run as a fixed lot or, with InpUseRiskSizing on, as a percentage of equity risked to that ATR-derived stop distance, using MODE_TICKVALUE and MODE_TICKSIZE rather than a hardcoded contract size so it prices correctly on gold, indices and JPY crosses as well as plain FX pairs. Notes and limitations - Only regular divergence is detected (price extreme vs RSI extreme disagreeing). Hidden divergence, used by some traders as a continuation signal, is a different pattern and is not covered here. - Every signal is InpPivotBars bars late by construction, because that delay is what makes the pivot confirmed rather than a guess. This is not a lookahead-free shortcut around that cost - there is no such shortcut. - One position at a time, tracked by InpMagicNumber. While a position is open the EA does not evaluate new divergence signals; it waits for the stop or target to close the trade first. - Divergence trading has a well-documented failure mode: momentum can diverge and price can simply keep extending anyway, especially in a strong trend where RSI can sit at an extreme for a long stretch. The ATR stop limits the damage; it does not prevent the loss.
Expert advisor · MT5 · v1.0 · Free
About RSI Divergence EA
RSI Divergence EA is an expert advisor (trading robot) for MetaTrader 5 based on RSI and ATR. It increases the lot size after a losing trade (martingale) and can size trades from your balance and risk percent. It has 13 settings, such as Rsi period, Pivot bars and Lookback bars. MQL source code included.
Files in the download: RsiDivergenceEa.ex5, RsiDivergenceEa.mq5, RsiDivergenceEa.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Rsi period | 14 | RSI period |
| Pivot bars | 3 | Bars required on each side to confirm a swing point |
| Lookback bars | 60 | How far back to search for the second swing point |
| Atr period | 14 | ATR period used to size the stop |
| Atr stop multiplier | 2.0 | Stop distance, in ATR multiples |
| Take profit pips | 60 | Take profit distance, in pips (0 = none) |
| Use risk sizing | false | Size by percent risk instead of a fixed lot |
| Risk percent | 1.0 | Percent of equity risked per trade, when risk sizing is on |
| Fixed lots | 0.10 | Lot size when risk sizing is off, or as a fallback |
| Magic number | 20260606 | Magic number for orders opened by this EA |
| Slippage pips | 3 | Slippage tolerance, in pips |
| Max retries | 5 | Attempts per trade action before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: MACD · RSI · Divergence · Momentum · Oscillator · Martingale · Risk & Lot Size · ATR · MT5 Expert Advisor · MQL5 Source Code