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 Directional Persistence Regime Switch is an adaptive Expert Advisor (EA) for MetaTrader 5 built around a single, unusual measurement: the lag-1 sign autocorrelation of close-to-close returns. Instead of relying on a moving average, RSI, or a chart pattern, it reduces the price tape to a stream of directional decisions — up, down, or flat — and asks one statistical question: does one push tend to be followed by another push, or by a reversal? That question is answered by a "regime coordinate" the strategy calls rho (ρ), a scale-free value between -1 and +1. An Average True Range (ATR) indicator supplies the volatility scale used for stops, targets, and a noise filter.
The design is meant for markets that alternate between two behaviours. In some periods, direction has positive memory — trends persist, and continuation is more likely than reversal. In other periods, direction has negative memory — moves are answered by snapbacks, and mean reversion dominates. A fixed always-trend or always-revert rule tends to work in one environment and struggle in the other. This EA tries to measure which memory is currently active and switch its trading mode accordingly, rather than betting on a single fixed behaviour.
As a learning tool, this strategy suits traders who want to study regime detection, statistical thinking about price, and adaptive risk design. Because it is an original, first-principles system rather than a repackaged classic indicator, it is a useful way to explore how directional memory can be quantified. It is not a shortcut to results, and like every mechanical system it can and will experience losing streaks.
How It Works
The EA processes one newly-closed bar at a time on your chosen timeframe. On each new bar it recomputes the regime and decides whether any of its conditions are met.
- Building the sign process: For each of the last
Lookbackbars, the strategy computes the return (this close minus the previous close) and reduces it to a sign: +1 for an up bar, -1 for a down bar, 0 for an unchanged bar. Flat bars do not break the chain; they are simply skipped as links. - Measuring the regime (rho): It multiplies each sign by the previous non-zero sign and averages those products across the window. The result, rho, equals the probability of continuation minus the probability of reversal. A high positive rho means pushes tend to continue (persistence); a strongly negative rho means pushes tend to reverse (anti-persistence). At least five valid sign-pairs are required, otherwise the reading is treated as untrustworthy and no trade is considered.
- The regime switch: If rho is greater than or equal to
RegimeThreshold, the market is classified as a trend/persistence regime. If rho is less than or equal to negativeRegimeThreshold, it is classified as a reversion regime. If rho sits between the two thresholds, the tape is considered memoryless — no structural edge — and the strategy stands aside. - Noise gate: Even inside a valid regime, the most recent closed bar must be decisive. The strategy signals only if the latest bar's move is at least
TriggerAtrFractionof the current ATR. Small, indecisive bars are filtered out. - Direction (entry logic):
- In a persistence regime, the EA trades with the latest bar — buying after an up bar, selling after a down bar (tags
persist-long/persist-short). - In a reversion regime, it trades against the latest bar — buying after a down bar, selling after an up bar (tags
revert-long/revert-short). - Stop-loss logic: The stop is always ATR-based, placed
StopAtrMult× ATR away from entry. This scales the risk distance to current volatility rather than using a fixed pip value. - Take-profit logic (adaptive reward): The target adapts to the regime. Trend trades aim for a wider
RewardTrend× ATR target because persistence can extend a move. Reversion trades aim for a tighterRewardReversion× ATR target because snapbacks tend to be quick. - Position management: Only one position per magic number is held at a time. The EA waits for the open trade to close (at its stop or target) before evaluating a new entry.

Strategy Parameters
| Parameter | Default | Min | Max | Description | ||
|---|---|---|---|---|---|---|
| Lookback | 40 | 15 | 120 | Window, in returns, used to estimate the sign-autocorrelation regime. Larger values smooth the reading; smaller values react faster. | ||
| RegimeThreshold | 0.20 | 0.05 | 0.60 | The \ | rho\ | gate — how decisive the directional memory must be before the strategy acts. Higher values demand stronger structure. |
| TriggerAtrFraction | 0.50 | 0.10 | 1.50 | The latest bar must move at least this fraction of ATR to trigger, keeping the EA off small, noisy bars. | ||
| AtrPeriod | 14 | 7 | 40 | Averaging period for the ATR used in the noise gate, stop, and targets. | ||
| StopAtrMult | 1.50 | 0.50 | 4.00 | Stop-loss distance as a multiple of ATR. | ||
| RewardTrend | 3.00 | 1.00 | 6.00 | Take-profit multiple (× ATR) in the trend/persistence regime — lets a move run. | ||
| RewardReversion | 1.50 | 0.50 | 3.00 | Take-profit multiple (× ATR) in the reversion regime — banks a quicker snapback. | ||
| Lots | 0.10 | 0.01 | 1.00 | Fixed order volume in lots. |
There is also a Magic number (default 7731) used to identify and manage the EA's own positions.

Recommended Chart Settings
This strategy operates on a single timeframe — every calculation uses the chart's own period, so the timeframe you attach it to is the strategy's timeframe. A common starting point for study is a liquid major forex pair such as EUR/USD on the H1 (1-hour) chart, where close-to-close returns are reasonably clean and ATR behaves predictably. Higher timeframes (H1 and above) generally produce fewer, more considered signals, while lower timeframes generate more signals with more noise.
Because the regime measurement is scale-free and unit-free, it can be applied to other symbols and timeframes, but results will vary considerably across instruments and market conditions. Always test any symbol and timeframe combination on historical data and a demo account before drawing conclusions.
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
What to Consider Before Using This EA
The most interesting strength of this approach is that it does not assume a single market personality. By measuring directional memory in real time, it attempts to trade with trends when continuation is statistically favoured and against moves when reversals dominate. The adaptive reward — wider targets in trends, tighter targets on fades — reflects the different payoff geometry of the two regimes, and the ATR-based stops keep risk proportional to current volatility rather than a fixed distance.
There are important limitations to understand. The sign autocorrelation is a backward-looking statistic: it describes the memory of the past Lookback bars, and a regime can flip immediately after the strategy has classified it. Choppy, transitional markets can produce whipsaws where the EA enters just as the regime changes. The RegimeThreshold gate reduces this by standing aside when rho is weak, but no filter eliminates false signals. The strategy also acts only once per bar and holds a single position, so it can miss opportunities while a trade is open or during fast intrabar moves.
This EA may historically underperform in markets that are pure noise (rho hovers near zero and few trades trigger) or during sharp news-driven spikes that blow through ATR-based stops. It is best treated as a framework for studying regime switching — not as a finished, hands-off system. Parameter choices such as Lookback, RegimeThreshold, and the reward multiples materially change its behaviour and should be explored deliberately.
Risk Management Tips
Sound risk management matters more than any single entry rule. Consider these general principles as you study this strategy:
- Position sizing: Keep the
Lotsvalue small relative to your account. Many educational sources suggest risking no more than 1–2% of account equity on any single trade, sized so that a full stop-loss stays within that limit. - Use a demo account first: Run the EA on a demo or backtest for an extended period before considering any live capital, so you can observe how it behaves across different regimes.
- Understand drawdown: Every mechanical system experiences losing streaks. Know the historical maximum drawdown of your settings and ask whether you could tolerate it emotionally and financially.
- Diversify and limit exposure: Avoid concentrating risk in a single symbol or correlated instruments running the same logic simultaneously.
- Review regularly: Markets evolve. Parameters that suited one period may need re-evaluation as conditions change.
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: DirectionalPersistenceRegimeSwitch.ex5 (0 downloads)
- Source Code: DirectionalPersistenceRegimeSwitch.mq5 (0 downloads)
- Documentation: DirectionalPersistenceRegimeSwitch.pdf (0 downloads)