Pairs & statistics
Find two instruments that move together, and trade the gap when it stretches
The statistics a pair or basket trade is built on, ready for any strategy: how closely two instruments move, how much of one hedges the other, how stretched their spread is and how fast it closes, and the formal tests - ADF, Engle-Granger and Johansen - with the same statistics, p-values and critical values as statsmodels. Scan every F&O pair for cointegration, size each leg in whole lots, and fit only on the days before you trade, so nothing looks ahead.
TestsADF, Engle-Granger, Johansen
Hedge ratioOLS, orthogonal, Kalman
Accuracymatches statsmodels exactly
What you can use
17 · tap one to copy an example requestMeasurea number over the window you give itTesta p-value - below 0.05 is significantToolbuilds or sizes the trade
Do they move together?how closely two instruments track
CorrelationMeasureHow closely two series move, from -1 to +1 - of prices or of returns.
Rolling correlationMeasureThe same over the last N days, every day - to see a relationship weaken.
ReturnsMeasureDay-to-day returns, log or simple - what correlation should usually be measured on.
How much of each?the hedge ratio between the legs
Hedge ratioToolBeta of one leg on the other by least squares - the cointegration choice.
Orthogonal hedge ratioToolThe same line fitted both ways at once - the same answer whichever leg is first.
Rolling hedge ratioToolBeta over the last N days, re-fitted every day.
Kalman hedge ratioToolA hedge ratio that adapts a little every day, with its own z-score.
Lots per legToolWhole lots for each leg that best match the hedge ratio.
Is the gap tradable?the spread and how it behaves
SpreadMeasureOne leg minus beta times the other - the gap that is traded.
Z-scoreMeasureHow many standard deviations the spread is from its mean - the usual entry signal.
Half-lifeMeasureHow many days the spread takes to close half its gap - how long a trade should last.
Hurst exponentMeasureBelow 0.5 the spread reverts, above 0.5 it trends.
Statistical testsis the relationship real?
ADF testTestAugmented Dickey-Fuller: does one series revert to its mean?
Engle-Granger cointegrationTestDo two (or more) prices share a long-run line their gap keeps returning to?
Johansen testTestCointegration for baskets of 3 to 12 instruments, with the basket weights.
Across the marketfind the pairs, without looking ahead
Pair scanToolEvery pair of F&O stocks ranked by cointegration - the strongest first.
Prices before a dayToolSeveral symbols' closes aligned on common days, strictly before the trading day.
Strategies to try
tap a request to copy it, then paste it into the chatTwo indices.
Same-sector stocks.
A stock against its index.
An adaptive hedge.
Good to know
- ADF, Engle-Granger and Johansen give the same statistics, p-values and critical values as statsmodels.
- Fit on the days before you trade and re-fit on a schedule (monthly is common) - never on the days being traded.
- Testing many pairs makes some pass by luck: prefer same-sector pairs with a half-life of days, not months.
- Stock prices are not yet adjusted for splits and bonuses - a corporate action inside the window breaks a stock pair. Index pairs are unaffected.
- Work on log prices of roll-adjusted futures, so expiry rolls do not look like price jumps.