Disclaimer: This article is for educational and informational purposes only. It does not constitute financial or investment advice. Trading forex and CFDs carries significant risk of loss. Past performance of any strategy – including backtests – does not guarantee future results. Never trade with money you cannot afford to lose.
What Is This Strategy?
The Inverse Fair Value Gap Retest is a price-action reversal strategy for MetaTrader 5. It is built around the Inverse Fair Value Gap (IFVG) pattern, and its only indicator is the Average True Range (ATR). A fair value gap is a three-candle imbalance: the middle candle moves so far that the wicks of the candles on either side never overlap, which leaves a "void" where price moved in one direction only. The inverse idea is that when a later candle closes completely through that gap, the gap has failed. The strategy then treats it as a zone of the opposite polarity, so failed support becomes possible resistance, and failed resistance becomes possible support.
The Expert Advisor (EA) automates the whole process. It finds gaps on closed candles, tracks up to 64 of them at once, and waits for price to return to a flipped zone for the first time. ATR plays no part in finding signals. It only adds a small buffer to the stop-loss.
The strategy is meant for markets that make sharp displacement moves and then pull back, as major forex pairs and gold often do. As a learning tool, it suits traders who want to see how Smart Money Concepts-style patterns can be written as strict, testable rules that you can backtest and check trade by trade.
How It Works
The strategy works in three stages: detect, flip and enter.
Step 1: Detect a fair value gap (checked on every closed candle)
- The EA looks at the last three closed candles: candle 1 (oldest), candle 2 (the displacement candle) and candle 3 (newest).
- A bullish gap forms when candle 1's high is below candle 3's low. The zone runs from candle 1's high up to candle 3's low.
- A bearish gap forms when candle 1's low is above candle 3's high. The zone runs from candle 3's high up to candle 1's low.
- A size filter removes hairline gaps. The gap has to be at least a set fraction of candle 2's range, which is 10% by default.
Step 2: Flip the gap when a candle closes through it
- A bullish gap becomes a sell zone when a later candle closes below its bottom edge. Wicks don't count.
- A bearish gap becomes a buy zone when a later candle closes above its top edge.
- The reasoning is that traders who entered from the original gap are now trapped. When price comes back, their exits may cause a reaction at the gap's edge.
Step 3: Enter on the first retest (checked on every tick)
- Sell zone: a market sell order goes in when the Bid price rallies back up to the bottom edge of the zone.
- Buy zone: a market buy order goes in when the Bid price falls back to the top edge of the zone. The order fills at the Ask.
- If price has already gone through the zone's far edge when the touch is detected, as can happen in a fast spike, the EA skips the trade.
Stop-loss and take-profit logic
- The stop sits past the zone's far edge: above the top for sells, below the bottom for buys. A padding of ATR × StopAtrMult is added. ATR measures the average candle range over recent bars, so the padding widens when the market is volatile.
- If the stop would be closer than the broker's minimum stop distance, the EA moves it out to that minimum. It then recalculates the target so the reward-to-risk ratio stays the same.
- The target is a fixed multiple of the risk, 2R by default. "R" is the distance from entry to stop.
- The EA has no trailing stop and no break-even move. Every trade ends at its stop-loss or its take-profit.
Rules that keep the system disciplined
- One trade per gap. The first touch of a flipped zone uses it up, whether or not an order was placed. If a closed candle shows that price reached the zone without a tick triggering an entry, the zone is retired instead of being traded late.
- Gaps expire. A gap older than MaxGapAge bars, counted from the candle that completed the pattern, is dropped.
- Position limits. The EA never holds more than MaxPositions trades at once. It never opens a trade in the opposite direction to one it already holds.

Strategy Parameters
| Parameter | Default | Min | Max | Description |
|---|---|---|---|---|
| MaxGapAge | 30 | 5 | 80 | Maximum age of a gap in bars, counted from the candle that completed the pattern. Older gaps are discarded whether or not they have flipped. |
| AtrPeriod | 14 | 5 | 50 | Number of bars used to calculate ATR. It only sizes the stop buffer and never generates entries. |
| StopAtrMult | 0.25 | 0.0 | 2.0 | Stop padding past the gap's far edge, as a multiple of ATR. 0 puts the stop exactly on the edge. |
| RiskReward | 2.0 | 1.0 | 5.0 | Take-profit distance as a multiple of the stop distance. 2.0 means a 2R target. |
| MinGapPct | 0.10 | 0.0 | 0.60 | Minimum gap size as a fraction of the middle candle's range. 0 accepts every gap. |
| MaxPositions | 1 | 1 | 5 | Maximum number of trades this EA can have open at once on the symbol. |
| Lots | 0.10 | 0.01 | 1.0 | Fixed order volume, rounded to your broker's lot step and limits. |
The EA also has a Magic input (default 7317), an ID it uses to recognise its own trades. If you run more than one copy on the same account, give each copy a different Magic number.

Recommended Chart Settings
The Inverse Fair Value Gap Retest was designed for EURUSD, GBPUSD and XAUUSD (gold) on the M15 (15-minute) timeframe, but it runs on whatever timeframe the chart uses. Lower timeframes produce more gaps and more noise, and the spread takes up a bigger share of each trade's risk. Higher timeframes produce fewer, larger gaps and wider stops. Gold moves in larger ranges than the currency pairs, so check what the default lot size means in money terms before you test it there.
Results will vary across market conditions, including trends, ranges and news-driven moves. Entries are detected tick by tick, so backtest with "Every tick based on real ticks" modelling.
How to Install on MetaTrader 5
- Download the .ex5 file from the link below
- Copy it to your MT5
MQL5\Expertsfolder - Restart MetaTrader 5 or refresh the Navigator panel
- Drag the EA onto a chart matching the recommended symbol and timeframe
- Configure the input parameters and enable Algo Trading
To find the folder, go to File → Open Data Folder in MT5. The file is named InverseFairValueGapRetest.ex5. To read or change the logic, open InverseFairValueGapRetest.mq5 in MetaEditor.
What to Consider Before Using This EA
Strengths
- Objective rules. Every condition is plain candle geometry, so you can check each trade against the chart.
- Defined risk. The stop sits at a structural level past the failed zone.
- One-shot discipline. Each zone is traded at most once, so the EA doesn't keep re-entering at a level that has already failed.
- Small parameter set. One indicator and seven inputs leave less room for curve-fitting, although the danger is still there.
Limitations
- Strong trends. In an uptrend, a pullback that closes through a bullish gap turns it into a sell zone. The EA then sells against the dominant trend, and those trades may be stopped out repeatedly.
- Choppy sessions. Tight ranges produce many small gaps that flip quickly without follow-through, and a 2R target can be hard to reach.
- No context filters. The EA uses no higher-timeframe bias, session timing or news avoidance, which discretionary IFVG traders often apply.
- Costs. All entries are market orders, and buys fill at the Ask. On small gaps, spread and slippage can be a large share of the planned risk.
- Fixed lot size. Volume does not scale with stop distance, so a wide gold gap at 0.10 lots risks far more money than a narrow EURUSD gap.
Risk Management Tips
- Size from the stop. A common formula is: lot size = (balance × risk %) ÷ (stop distance × value per point per lot). This EA uses a fixed
Lotsvalue, so work out what a typical stop costs and adjust that setting to match. - Keep risk small. Many educators suggest risking no more than 1-2% of your account on any single trade.
- Start on a demo account. Forward-test for several weeks before you consider live capital, and compare the results with your backtest.
- Understand drawdown. Drawdown is the fall from an account's peak to its lowest point. At 2R, the strategy only needs to win about one trade in three to break even before costs, but losing streaks still happen. Ten losses in a row at 2% each would cut an account by roughly 18%.
- Watch correlation. EURUSD and GBPUSD often move together, so running the EA on both can double your exposure in the same direction.
- Avoid over-optimization. Test values around the defaults, and check them on data you did not optimize on.
Risk Warning
Trading foreign exchange, CFDs, and other leveraged financial instruments involves substantial risk of loss and is not suitable for all investors. The strategies and tools discussed on this page are provided for educational purposes only and do not constitute financial advice, investment recommendations, or solicitation to trade. Always consult a qualified financial adviser before making trading decisions. Past backtest performance is not indicative of future results.
Downloads
- Expert Advisor: InverseFairValueGapRetest.ex5 (4 downloads)
- Source Code: InverseFairValueGapRetest.mq5 (1 downloads)
- Documentation: InverseFairValueGapRetest.pdf (2 downloads)