OPEN-SOURCE SCRIPT

Statistical Arbitrage Pairs Trading - Long-Side Only

This strategy implements a simplified statistical arbitrage ("stat arb") approach focused on mean reversion between two correlated instruments. It identifies opportunities where the spread between their normalized price series (Z-scores) deviates significantly from historical norms, then executes long-only trades anticipating reversion to the mean.

Key Mechanics:
1. Spread Calculation: The strategy computes Z-scores for both instruments to normalize price movements, then tracks the spread between these Z-scores.

2. Modified Z-Score: Uses a robust measure combining the median and Median Absolute Deviation (MAD) to reduce outlier sensitivity.

3. Entry Signal: A long position is triggered when the spread’s modified Z-score falls below a user-defined threshold (e.g., -1.0), indicating extreme undervaluation of the main instrument relative to its pair.

4. Exit Signal: The position closes automatically when the spread reverts to its historical mean (Z-score ≥ 0).


Risk management:
  • Trades are sized as a percentage of equity (default: 10%).
  • Includes commissions and slippage for realistic backtesting.




Penafian