Statistical Pair Arbitrage Using Z-Score in MetaTrader 5

 
Statistical Pair Arbitrage Using Z-Score in MetaTrader 5

Hello everyone,

I would like to share a statistical pair trading approach that I have been working on for MetaTrader 5.

Instead of trying to predict the direction of a single market, this approach analyzes the relationship between two instruments and looks for temporary deviations from their historical relationship.

The main idea is simple:

Rather than asking:

"Will EURUSD go up or down?"

the system asks:

"Has the relationship between EURUSD and GBPUSD moved unusually far from its recent statistical average?"

HOW DOES IT WORK?

The strategy monitors two related instruments simultaneously.

For example:

EURUSD
GBPUSD

A statistical spread is calculated from the price data of both instruments.

The system then analyzes the historical behavior of this spread over a defined lookback period.

To measure how unusual the current spread is, the strategy uses a Z-Score.

In simplified form:

Z-Score = (Current Spread - Mean Spread) / Standard Deviation

A Z-Score close to zero means that the relationship is relatively close to its recent average.

A large positive or negative Z-Score indicates that the relationship has moved further away from that average.

ENTRY LOGIC

For example, the strategy can use:

Entry Z-Score: +2.0 / -2.0

When the Z-Score reaches or exceeds +2.0, the system identifies a relatively high statistical deviation.

When the Z-Score reaches or falls below -2.0, it identifies a relatively low statistical deviation.

The strategy can then open opposite positions on the two instruments according to the direction of the spread.

The objective is not to predict whether both markets will rise or fall.

The objective is to trade a potential normalization of the relationship between them.

EXIT LOGIC

The positions can remain open until the relationship moves closer to its statistical mean.

For example:

Exit Z-Score: 0.50

When the absolute Z-Score returns to 0.50 or below, the system can close both legs of the pair.

WHY USE TWO INSTRUMENTS?

Traditional directional strategies depend heavily on predicting the future direction of one instrument.

Pair trading approaches the market differently.

The focus is on relative movement.

One instrument may be bought while the other is sold.

This creates a two-leg position designed around the relationship between the instruments rather than a simple directional forecast.

AUTOMATING THE STRATEGY

I implemented this concept as a multi-symbol Expert Advisor for MetaTrader 5.

The EA automatically:

- Monitors both instruments
- Calculates the statistical spread
- Calculates the Z-Score
- Detects entry conditions
- Opens the two trading legs
- Monitors convergence
- Closes the pair according to the exit condition
- Checks trading sessions
- Checks spreads
- Checks available margin
- Uses separate Magic Numbers for position management

I also added protection for an important practical problem in pair trading: incomplete execution.

If the first leg is opened but the second leg cannot be executed, the EA attempts to close the first leg instead of intentionally leaving an unpaired directional position.

NO MARTINGALE OR GRID

The strategy does not use Martingale or Grid logic.

Position size is not automatically multiplied after a loss.

The trading decision is based on the statistical relationship between the selected instruments.

IMPORTANT LIMITATION

Statistical arbitrage should not be confused with risk-free arbitrage.

A historical relationship between two markets can change.

A spread that appears unusually wide can remain wide or become even wider.

Correlation can weaken, market regimes can change, and execution conditions can affect results.

Broker spreads, commissions, swaps, slippage, liquidity and contract specifications are also important when evaluating a pair strategy.

For this reason, backtesting and testing under the conditions of your own broker are essential.

DISCUSSION

I am interested in hearing other traders' experiences with statistical pair trading in MetaTrader 5.

Which approach do you prefer for pair selection?

- Correlation
- Cointegration
- Z-Score
- Price ratio
- Regression-based spread

I am also experimenting with the same concept on other related markets, including Gold and Silver.

I would be interested to hear which pairs you think are most suitable for statistical mean-reversion research.

This is a technical discussion about algorithmic trading and is not financial or investment advice.