Pairs Trading Z Score Optimized
- Indicatori
- Ledi Haryadi
- Versione: 8.0
- Aggiornato: 30 aprile 2025
- Attivazioni: 5
Pairs Trading Z-Score Optimized is a custom indicator for the MetaTrader 4 (MT4) platform, designed to support pairs trading by analyzing the spread between two currency pairs using Z-Score calculations and stationarity testing. It operates on a single pair (the chart’s symbol and a user-defined second symbol) and displays Z-Score values, threshold bands, and trading signals in a subwindow, with optional arrows on the main chart. The indicator incorporates the Augmented Dickey-Fuller (ADF) test for spread stationarity and provides customizable settings for signal generation and visualization.
Key Features
- Z-Score Analysis:
- Calculates the Z-Score of the spread between two currency pairs (Pair1: chart symbol, Pair2: user-defined, default: GBPUSD) to identify potential trading signals.
- Uses a configurable lookback period (default: 100 bars) and Z-Score threshold (default: 2.0) to determine signal triggers.
- Displays Z-Score as a line in a subwindow, with upper and lower threshold bands (default: ±2.0).
- Stationarity Testing:
- Supports the Augmented Dickey-Fuller (ADF) test to assess spread stationarity, with options for no transformation, first differencing, or logarithmic transformation (default: none).
- Configurable ADF test parameters include lags (default: 1), critical value (default: -2.86 for 5% significance), and test interval (default: 60 seconds).
- Allows users to require stationary spreads for signal generation (default: enabled).
- Signal Generation and Visualization:
- Generates "LONG" (buy Pair1, sell Pair2) or "SHORT" (sell Pair1, buy Pair2) signals when the Z-Score exceeds the threshold (above +2.0 for SHORT, below -2.0 for LONG).
- Displays signals as arrows in the subwindow, main chart, or both (configurable via ArrowDisplayMode).
- Differentiates between stationary (lime arrows, default Wingdings codes: 233/234) and non-stationary (orange arrows, default Wingdings codes: 225/226) signals, with options to enable/disable each (ShowStationaryArrows, ShowNonStationaryArrows).
- Hedge Ratio Calculation:
- Computes the hedge ratio between pairs using linearSy linear regression to determine the spread (Pair1 - hedgeRatio * Pair2).
- Displays the hedge ratio in the subwindow for transparency.
- Alerts:
- Provides optional alerts for new stationary or non-stationary signals (default: disabled), with messages indicating the signal type and Z-Score (e.g., "[Stationary Signal] SHORT on EURUSD, LONG on GBPUSD (Z-Score=2.50)").
- Prevents repeated alerts by tracking the last signal state.
- Data Handling:
- Implements robust price data retrieval with retry attempts (default: 3) and timeouts (default: 500ms for data, 1000ms for ADF test) to handle connectivity issues.
- Ensures data integrity for accurate Z-Score and ADF calculations.
- User Interface:
- Displays key information in the subwindow, including pair names, ADF test status (stationary/non-stationary), and hedge ratio, with customizable label positions (default: X=10, Y=30 pixels).
- Supports customizable arrow positions on the main chart (default: X=10, Y=10 pixels from the selected corner) and arrow codes for visual distinction.
- Customization:
- Offers extensive input parameters for adjusting lookback period, Z-Score threshold, ADF test settings, stationarity method, and visualization options (arrow display, colors, positions).
- Allows users to enable/disable features like ADF testing, stationary signal requirements, and alerts.
Usage
- Initialization: Validates both currency pairs in Market Watch, sets up buffers, and performs an initial ADF test if enabled. Creates subwindow labels for pair, ADF status, and hedge ratio.
- Operation: Calculates Z-Scores and signals for each bar, updating threshold bands and arrows based on stationarity and Z-Score conditions. Periodically runs ADF tests (every 60 seconds) for the latest bar.
- Visualization: Plots Z-Score and bands in the subwindow, places arrows on the subwindow or main chart, and updates labels with real-time data.
- Deinitialization: Removes all labels and arrow objects, resetting signal states.
Notes
- Requires sufficient historical data (minimum: lookback period + 10 bars)
- When using Pair Trading Strategy Based on Spearman , make sure to limit the bars in the Max bars in chart option to below 2000, for example 500.
- Optimized for performance with configurable timeouts and retry mechanisms.
- The indicator focuses solely on Z-Score-based signals, without additional technical filters like MA or RSI, unlike related indicators.
