Z-Score Math for Mean Reversion Expert Advisors
The Logic of the Bell Curve: What is Z-Score?
In quantitative finance, the Z-Score is a statistical measure that tells you how many Standard Deviations the current price is away from its average (mean). It assumes that price movements follow a 'Normal Distribution'. If the price moves too far away from the mean, it is statistically 'Extreme' and is likely to return to the average.
This is the foundation of Statistical Mean Reversion. While a simple RSI indicator tells you if a market is 'Overbought', a Z-Score tells you exactly how rare the current price level is from a mathematical perspective. A Z-Score of +3.0 means the price is higher than 99.7% of all previous price points, suggesting a massive sell-off is imminent.
The Formula: (Price - Mean) / Standard Deviation
To calculate the Z-Score in MQL4/MQL5, the EA needs two inputs: 1. Price (Close). 2. Simple Moving Average (SMA) of the last N periods (e.g., 20). 3. Standard Deviation (StdDev) of those same 20 periods.
The formula is: Z = (Close - SMA) / StdDev. This converts the price into a 'Standardized' oscillator that fluctuates around zero. Zero represents the mean. A positive Z-Score means the price is above the mean, and a negative score means it is below. This allows the bot to 'Quantify' the strength of a price spike relative to historical volatility.
Z-Score Execution Rules
Primary: Calculate Z-Score using a 20-period lookback
Threshold: Sell at Z > +2.5 / Buy at Z < -2.5
Constraint: Never trade during high ADX trend readings (> 30)
Market State: Mean reversion works best in the Asian session
Verified: Target the 'Zero Line' (Mean) as your first Take Profit
Safety: Use a hard Stop-Loss to avoid 'Parabolic' trend failures
Strategy: Trading the 'Statistical Reversion'
The Entry Signal: The EA looks for Z-Score extremes. A common institutional strategy is to Sell when the Z-Score hits +2.5 and Buy when it hits -2.5. These are the points of maximum statistical exhaustion. The Probability: Mathematically, the price should stay within 2.0 standard deviations 95% of the time. This means that a Z-Score entry has a 'High Probability' of reversing soon. By combining this with a volume-spike filter, you can build a highly accurate mean-reversion algorithm.
Context: Mean Reversion vs. Trending Markets
The Z-Score's greatest weakness is a Strong Trend. During a massive momentum move (like a parabolic crypto pump or an NFP spike), the price can stay at a Z-Score of +3.0 or +4.0 for a long time as the 'Mean' itself moves upward. This is known as 'Riding the bands'. A professional Z-Score EA should always include a Trend Filter (like the ADX or a higher-timeframe EMA) to avoid 'Fading' a market that is fundamentally breaking out.