Pairs Trading Strategy Analysis with Python
Objective This project implements a classic statistical arbitrage strategy, pairs trading, from the ground up. The goal was to identify a cointegrated relationship between two synthetic financial assets, generate trading signals based on mean reversion of their price spread, and evaluate the strategy's performance through a simplified backtest.
Methodology The strategy is built on several core econometric and statistical concepts:
- Linear Regression: An Ordinary Least Squares (OLS) regression was used to determine the hedge ratio (β) between the two assets, modelling the relationship
Stock_A = α + β * Stock_B. - Spread Calculation: The spread was calculated as the residual of the regression (
spread = Stock_A - β * Stock_B). This time series represents the deviation from the long-term equilibrium. - Mean Reversion Test: A simplified test was conducted to confirm that the spread is stationary (i.e., it tends to revert to its mean over time), which is the fundamental requirement for a pairs trading strategy.
- Z-Score for Signals: Trading signals were generated by calculating the rolling Z-score of the spread. Entry and exit points were defined by setting thresholds (e.g., enter at +/- 1.5 standard deviations, exit when the Z-score crosses 0).
Key Results -Hedge Ratio (β): -0.239 -Model Fit (R-Squared): 0.23% -Mean Reversion Rate: 64.23% -Backtest Win Rate: 77.14% -Total P&L: 6.10
After running the backtest on the synthetic data, the strategy yielded the following results:
Total Net P&L: 4.35 Total Trades: 35 Win Rate: 74.29% Sharpe Ratio: 0.37