Pair Trading Strategy
Purpose
Select two highly correlated instruments (such as stocks from the same industry or BTC/ETH), monitor how far their price ratio (or spread) deviates from the mean, and trade against extreme deviations while waiting for mean reversion.
Signal Logic
- Compute the price ratio:
ratio = close_A / close_B - Rolling mean and standard deviation:
mean = ratio.rolling(lookback).mean(),std = ratio.rolling(lookback).std() - Z-score:
z = (ratio - mean) / std - Signal generation:
- Z < -entry_z → long A, short B (ratio is too low, expected to revert)
- Z > +entry_z → short A, long B (ratio is too high, expected to revert)
- |Z| < exit_z → close the position (reverted back near the mean)
Implementation Notes
- Pair trading requires exactly two instruments (
codesarray length = 2) - The first instrument is A (
leg1), and the second is B (leg2) - Signals for A and B are opposite: when A is long, B is short, and vice versa
- Equal-weight allocation only: A and B each take 50% of capital, with no precise hedge-ratio calculation
Parameters
| Parameter | Default | Description | |------|--------|------|…