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 Range Filter Trend Shift is a trend-following expert advisor built around a Range Filter, a volatility-adaptive "ratcheting" baseline that is mechanically different from the more familiar ATR channels, moving-average rails, Chandelier stops, or SuperTrend lines. Rather than gauging volatility from each candle's high-to-low range, the Range Filter measures how far price actually travels from one close to the next, and then only allows its baseline to advance once price has pushed beyond that typical travelled distance. The result is a line that absorbs small back-and-forth noise but gets dragged along by genuine directional thrust.
Because the filter line can only move in the direction of the prevailing push — it "ratchets" and never retraces against itself — its slope becomes a clean, low-noise definition of the current market regime. When the line is rising and price sits above it, the strategy treats the market as being in an uptrend; when the line is falling and price sits below it, it treats the market as being in a downtrend. Every trade is a reaction to that regime flipping from one state to the other, which is why the strategy carries the name "Trend Shift."
This EA is best understood as a learning tool for traders who want to study adaptive baselines, regime detection, and stop-and-reverse logic. It is designed for markets that produce sustained directional moves — trending forex majors or metals such as EURUSD or XAUUSD on intraday-to-swing timeframes — and it is deliberately transparent so you can inspect exactly how each signal is generated. It is not a shortcut and it is not a promise of any particular outcome; it is a framework for understanding how a travelled-distance filter behaves in different market conditions.
How It Works
The strategy processes only closed bars on the primary timeframe and rebuilds its Range Filter state machine each time a new bar closes. Here is what happens under the hood and how signals are produced:
- Per-bar travel: For each closed bar, the strategy computes the absolute change in close (
|close − previous close|). This is the raw distance price travelled on that bar. - Average travel: It smooths that travel with an exponential moving average (EMA) over
RangePeriodbars to get a representative "average range." - Smoothed, scaled range: That average is smoothed again by a longer EMA (length
2 × RangePeriod − 1) and multiplied byRangeMult. This produces the final band width — how far price must clear before the baseline is allowed to step. - The ratcheting filter line: If the close is above the previous filter value, the line may only rise (it is set to the higher of its previous value or
close − range). If the close is below, it may only fall. The line never moves against its own direction — it steps forward, then holds. - Regime definition: When the filter line is rising and price is above it, the regime is flagged UP. When the line is falling and price is below it, the regime is flagged DOWN. The regime persists until it genuinely flips.
Entry — on a fresh regime flip (stop-and-reverse):
- The strategy signals a long when the regime turns UP after not being up. Any open short is closed first, then a buy is opened.
- The strategy signals a short when the regime turns DOWN after not being down. Any open long is closed first, then a sell is opened.
- Only one position per magic number is held at a time. An opposite flip reverses the position, so the EA is always aligned with the direction of the filter line.
Stop-loss and take-profit — ATR-based:
- Risk is scaled by the Average True Range (ATR), an indicator that measures recent volatility, so the distances adapt automatically to each symbol and timeframe.
- The stop-loss is placed
StopAtrMult × ATRaway from the entry price. - The take-profit is placed
TargetAtrMult × ATRaway — a deliberately wider reward multiple that is intended to let a running trend ride while the volatility-scaled stop cuts the whipsaws a stepped filter can produce inside a range.
Because the filter is recomputed over a fixed window of already-closed bars and the flip is detected by comparing the last closed bar's regime with the bar before it, the signal is non-repainting — it does not change after the fact.

Strategy Parameters
| Parameter | Default | Min | Max | Description |
|---|---|---|---|---|
| RangePeriod | 14 | 5 | 60 | Sampling length for the average close-to-close travel — the adaptive range. Shorter reacts faster; longer is smoother. |
| RangeMult | 2.5 | 1.0 | 6.0 | Scales the smoothed range. Larger values create a wider filter with fewer, slower regime flips. |
| AtrPeriod | 14 | 5 | 50 | ATR lookback used to size the volatility-scaled stop and target. |
| StopAtrMult | 1.5 | 0.5 | 5.0 | Stop-loss distance as a multiple of ATR. |
| TargetAtrMult | 3.0 | 0.5 | 8.0 | Take-profit distance as a multiple of ATR — a wide reward that aims to let trend winners run. |
| Lots | 0.10 | 0.01 | 1.00 | Fixed order volume (position size) in lots. |
| Magic | 7311 | 0 | 9,999,999 | Unique identifier so the EA only manages its own trades. |

Recommended Chart Settings
The Range Filter Trend Shift was designed with trending FX majors or metals in mind — EURUSD or XAUUSD are the reference instruments — on H1 or H4 timeframes, where directional moves tend to persist long enough for the ratcheting line to develop a clear slope. That said, the logic is timeframe-agnostic and runs on whatever timeframe you attach it to at backtest or live time.
Keep in mind that a stepped, regime-based filter behaves very differently across market conditions. The same settings that produce clean signals in a trending environment can generate repeated flips in a choppy, range-bound market. Always study how the parameters behave on your chosen symbol and timeframe before drawing any conclusions, and expect results to vary as conditions change.
How to Install on MetaTrader 5
- Download the
.ex5file 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.
What to Consider Before Using This EA
Strengths of the approach. The Range Filter's travelled-distance construction gives it a genuinely different noise profile from range-based tools. By absorbing small close-to-close wobble and only stepping on real thrust, it can stay flat through minor chop and then lock onto sustained moves. The ratcheting, non-retracing line makes the regime definition unambiguous, and the stop-and-reverse design keeps the position continuously aligned with that regime rather than sitting flat and missing continuation.
Known limitations. Every stepped filter faces the same core trade-off: the very smoothing that suppresses noise also introduces lag. In a fast reversal, the line may flip late, giving back part of a move before the regime updates. And like all trend-following logic, it is structurally vulnerable to range-bound markets, where the price oscillates around the baseline and produces a series of flips that are stopped out — the classic "whipsaw" pattern. The wide ATR-multiple target helps the winners compensate, but there is no configuration that removes this behaviour entirely.
Where it may underperform. Quiet, sideways consolidations, low-volatility sessions, and news-driven spikes that immediately reverse are the conditions most likely to challenge this design. It historically favours instruments and timeframes that produce clean, extended directional legs. Treat the defaults as a starting point for study, not as an optimised or recommended configuration, and observe its behaviour across many different market phases.
Risk Management Tips
Sound risk management matters more than any single indicator setting. As general educational principles:
- Risk a small, fixed fraction per trade. Many educational sources suggest limiting risk to roughly 1–2% of account equity on any single position, and sizing your lots to respect that limit rather than using a fixed volume blindly.
- Test on a demo account first. Run the EA on a demo or simulated account long enough to understand how it behaves through both trending and ranging conditions before considering any live capital.
- Understand drawdown. Even a well-behaved trend system endures losing streaks during choppy markets. Study the depth and duration of drawdown you would need to tolerate, and be honest about whether that matches your risk tolerance.
- Account for costs. Spreads, commissions, and slippage all affect a stop-and-reverse system that trades on regime flips. Include realistic costs in any evaluation.
- Never over-leverage. Larger position sizes magnify both gains and losses. Conservative sizing is what keeps you in the game long enough to learn from the strategy.
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: RangeFilterTrendShift.ex5 (25 downloads)
- Source Code: RangeFilterTrendShift.mq5 (20 downloads)
- Documentation: RangeFilterTrendShift.pdf (36 downloads)