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 Median Line Fork Reversion strategy is a mean-reversion system built on Alan Andrews' median-line geometry — the technique many traders know as the "Andrews pitchfork." Instead of the flat, horizontal boundaries of a Donchian channel or the statistical fit of a linear regression channel, this approach constructs a sloped price channel directly from three recent swing pivots and then fades price whenever it stretches to one of the channel's outer rails, aiming for a return to the centre line. In plain terms, it is a counter-trend, "buy-the-dip / sell-the-rip" style tool that works within a diagonal envelope rather than a flat one.
The pivots themselves are located with a ZigZag fractal filter — a bar only qualifies as a swing high or swing low if it is the strict extreme of the small window of bars around it. Those alternating high/low pivots feed the pitchfork math. The core idea the strategy leans on is Andrews' well-known "80% rule" observation: once price reacts away from a well-formed median line, it historically tends to travel back toward that line the majority of the time. The system is therefore designed for markets that oscillate, drift, and retrace within a channel — ranging or gently trending conditions — rather than fast, one-directional breakouts.
As a learning tool, this strategy is well suited to traders who want to understand how geometric channel construction, swing-pivot detection, and volatility-based risk sizing fit together in a single automated framework. It is not a signal to chase profits; it is a structured example of how a classic charting method can be translated into precise, rule-based code. Treat it as a study in mechanical mean reversion.
How It Works
The Expert Advisor (EA) rebuilds its pitchfork continuously as new pivots confirm, then checks for fade opportunities once per completed bar. Here is the logic in plain English:
- Pivot detection (ZigZag): Each time a bar finishes, the strategy tests the bar sitting
PivotLookbackpositions back. If that bar is the strict highest high (or lowest low) of the surrounding window, it is confirmed as a swing pivot. Pivots are stored in an alternating high–low–high sequence, so the three most recent — labelled P0 (oldest), P1, and P2 (the latest reaction) — always zigzag. - Building the median line: The median line starts at P0 and is drawn through the midpoint of the P1–P2 segment. Its slope defines the tilt of the whole channel. Two parallel "tines" run through P1 and P2, forming the upper and lower rails. By construction, the median line sits exactly halfway between the two rails on every bar, which makes it a natural reversion target from either side.
- Long entry — the strategy signals a buy when: the just-completed bar sags to or below the lower tine (rejection of the rail), its close is back above the prior bar's close (a sign the sag is being rejected), and the median line sits a worthwhile distance above the current market. The take-profit is placed at the dynamic median line.
- Short entry — the strategy signals a sell when: the completed bar pushes to or above the upper tine, closes back below the prior close, and the median line sits a worthwhile distance below the market. Again, the target is the median line.
- Minimum-room filter: If the channel is thinner than half of the current ATR (Average True Range, a volatility gauge), the trade is skipped. This avoids fading channels too narrow to offer meaningful room to the target.
- Staleness guard: If the newest pivot is older than
MaxForkAgebars, the fork is considered stale and no trade is taken — the geometry may no longer describe current price behaviour. - Stop-loss logic: A protective stop is placed
AtrStopMult × ATRbeyond the entry price, so risk scales with current volatility rather than a fixed pip distance. - Take-profit logic: The target is always the live median line — the centre of the channel — reflecting the mean-reversion thesis that price tends to return there.
- One position at a time: The EA holds only a single position per magic number, so signals are ignored while a trade is already open.

Strategy Parameters
| Parameter | Default | Min | Max | Description |
|---|---|---|---|---|
| PivotLookback | 3 | 2 | 8 | Fractal half-width for swing-pivot detection — the number of bars on each side a candidate must exceed to confirm as a pivot. Larger values find fewer, more significant swings. |
| AtrPeriod | 14 | 7 | 30 | The ATR length used both for the protective stop distance and for the minimum-room filter. Longer periods smooth the volatility estimate. |
| AtrStopMult | 1.5 | 0.5 | 4.0 | Multiplier that sets the protective stop at entry ∓ (AtrStopMult × ATR). Higher values give the trade more breathing room but risk more per position. |
| MaxForkAge | 120 | 20 | 400 | The maximum age (in bars) of the newest pivot. Forks older than this are treated as stale and skipped. |
| Lots | 0.10 | 0.01 | 1.0 | The fixed order volume (position size) used for each trade. |

Recommended Chart Settings
This strategy is single-timeframe by design — every calculation uses the chart's chosen timeframe, so you decide the timeframe when you attach it. As a starting point for study, mid-range intraday timeframes such as H1 (1-hour) on liquid major FX pairs like EUR/USD give the pivot detection enough clean swings to build meaningful pitchforks without excessive noise. Higher timeframes such as H4 tend to produce fewer but more structurally significant forks.
Because the median-line method depends on well-formed swing structure, the strategy generally behaves best in markets that oscillate within a channel. Keep in mind that results will vary considerably across different symbols, timeframes, and market conditions — always test any configuration on your own data 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
Strengths of the approach. The median-line method is one of the oldest and most respected pieces of technical geometry, and turning it into a rules-based EA removes the subjectivity of hand-drawing pitchforks. The sloped channel adapts to trend direction, the ATR-based stop keeps risk proportional to volatility, and the minimum-room and staleness filters help avoid the lowest-quality setups. As a study in disciplined, symmetric (long and short) mean reversion, it is a clean example.
Known limitations. Mean-reversion systems face a structural challenge: they lean against momentum. In a strong, sustained trend, price can ride an outer tine for a long time, and fading it repeatedly may lead to a series of stop-outs before the channel finally breaks. The pitchfork is also only as good as the pivots it is built from — during choppy, structureless phases the ZigZag can produce forks that describe the past better than the future. The staleness guard mitigates this but cannot eliminate it.
Where it may underperform. Expect weaker behaviour during breakouts, news-driven volatility spikes, and low-liquidity sessions where spreads widen and swing structure becomes erratic. The strategy is designed for orderly oscillation, not for regime shifts. Treat every configuration as a hypothesis to be tested, not a finished product.
Risk Management Tips
Sound risk management matters more than any single entry rule. Consider these general principles as you study this EA:
- Position sizing: Size each trade so that a stop-out costs only a small, pre-decided fraction of your account. A common educational guideline is risking no more than 1–2% of account equity per trade.
- Respect the stop: The ATR-based stop exists to cap losses. Widening or removing stops to "give a trade room" is one of the most common ways accounts are damaged.
- Use a demo account first: Run the strategy on a demo or paper account until you fully understand its behaviour across different market conditions before considering any live capital.
- Understand drawdown: Every strategy experiences losing streaks. Know the maximum drawdown you are willing to tolerate and how it feels emotionally before it happens.
- Diversify and moderate: Avoid over-concentrating risk in a single symbol, timeframe, or strategy, and avoid over-leveraging.
Automated does not mean unattended — monitor the EA, keep records, and review its behaviour regularly.
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: MedianLineForkReversion.ex5 (28 downloads)
- Source Code: MedianLineForkReversion.mq5 (28 downloads)
- Documentation: MedianLineForkReversion.pdf (37 downloads)