Skip to content

Free MT5 Expert Advisor

Z-Score Mean Reversion EA

MT5 Expert Advisor that fades price z-score extremes once price closes back inside ±2, with an ADX trend filter and a target at the mean.

Mean reversionv1.0MetaTrader 5 (MQL5) Compiles with 0 errors Stop-loss on every trade
Strategy
Buys when the price z-score closes back above -2 after being below it, and sells when it closes back below +2 after being above it, as long as ADX is not signalling a strong trend. The target is the moving average.
Risk model
1% of balance per trade (configurable), ATR-based stop-loss on every order, one position at a time

What it does

The z-score expresses how far price is from its average in units of standard deviation. Z-Score Mean Reversion EA waits until price has been unusually far from its 20-bar mean and has started to come back, then trades toward that mean. An ADX filter keeps it out of strongly trending conditions, where stretched prices often keep stretching.

How it works

For each closed bar the EA calculates:

z = (close − SMA(20)) ÷ standard deviation of the close over 20 bars

On every new bar it compares the last two closed bars:

  • Buy: z was below −2 on the older bar and is at or above −2 on the last closed bar.
  • Sell: z was above +2 on the older bar and is at or below +2 on the last closed bar.
  • ADX filter: if ADX(14) on the signal bar is above 25, the signal is skipped and logged.
  • Stop-loss: 1.5 × ATR(14) from the entry price.
  • Take-profit: the SMA(20) value of the signal bar.
  • Reward check: if the mean is closer than 0.7 × the stop distance, the trade is skipped.

The z-score is the same quantity that Bollinger Bands display visually: a z-score of ±2 sits on 2-deviation bands. Waiting for the score to come back inside is what makes this a reversion entry rather than a fade of a running move.

How to use it

Ranging currency pairs and quiet sessions are the natural home for this logic. Start on H1 and look at how often ADX blocks signals; a lower ADX limit means fewer but more range-bound trades. Combine the idea with the lesson on support and resistance if you review the trades manually.

Settings

InputDefaultWhat it changes
Mean and standard deviation period20Lookback of the z-score
Z-score threshold2.0How extreme the move must be
ADX filter / period / maximumtrue / 14 / 25Avoids strong trends
ATR period / Stop-loss (x ATR)14 / 1.5Stop size
Min reward vs stop0.7Skips setups with a close target
Risk per trade / Fixed lots1.0% / 0Position sizing
Max spread (points)30Spread filter
Trading start / end hour7 / 20Server-time window
Break-even / Trailingon / offStop management

Limitations

Price distributions in markets have fat tails, so ±2 deviations are reached more often than a normal distribution suggests and can extend further. ADX is a lagging measure and may still read low when a new trend begins. The take-profit is fixed at the mean of the signal bar, which drifts as new bars arrive.

Before you run it live

Test it in the Strategy Tester on several symbols and on both quiet and volatile years, then on a demo account. Check that your broker's spread and stop level are small relative to the ATR stop. Use a small risk per trade. Past behavior does not guarantee future results.

Key inputs

Key inputs of Z-Score Mean Reversion EA
InputDefaultDescription
Mean and standard deviation period20Bars used for the SMA and the standard deviation.
Z-score threshold (+/-)2.0Distance from the mean, in standard deviations, that counts as extreme.
Skip trades when ADX shows a strong trendtrueTurns the ADX filter on or off.
Maximum ADX for a trade25Signals are skipped when ADX is above this value.
Stop-loss distance (x ATR)1.5Stop size as a multiple of ATR(14).
Skip if distance to the mean < this x stop0.7Minimum reward relative to risk.

Educational tools, not advice. Automated trading can lose money quickly. Indicators and Expert Advisors describe price; they do not predict it, and past behaviour does not guarantee future results. Test everything on a demo account first and never risk money you cannot afford to lose. See our disclaimer.

Keep exploring

Related mt5 expert advisors