How to use the pairs trading calculator
The Pair analysis tool does the full Engle-Granger workflow from raw prices:
- Paste the prices of A and B, oldest first, for the same dates. A is the stock you regress, B the hedge. The default data are 250 simulated daily closes of two fictional stocks, generated from a cointegrated model so the test has something to find. Replace them with your own.
- Choose log or raw prices. Log prices are the usual choice for stocks; raw prices give a hedge ratio in shares.
- Choose the ADF lag. One lagged difference absorbs short-term autocorrelation in the spread; no lag is the plain Dickey-Fuller regression.
- Set the z-score lookback, the window for the rolling mean and standard deviation of the spread, and the data frequency.
- Set the entry and exit thresholds. The calculator applies them to the whole sample and lists every signal.
The Z-score & position size tool skips the estimation: enter today’s prices, a hedge ratio and the spread’s mean and standard deviation, and it returns the z-score, the price of A that would trigger an entry, and the number of shares in each leg. The Half-life tool converts a coefficient from your own regression into a half-life and an expected path back to the mean.
The formulas
ADF statistic = γ̂ ÷ SE(γ̂)Step 2: an augmented Dickey-Fuller regression on the spread, with a constant and zero or one lagged difference (drop the δ term for zero lags). A strongly negative statistic means the spread pulls back toward its mean.
A worked example
The regression of log A on log B over the 250 simulated closes gives a hedge ratio of 1.1166: for every $1 of A, a trader would hold about $1.12 of B on the other side. The regression explains 96.8% of the variation in log A, which on its own proves nothing; trending prices produce a high R² even when unrelated.
The cointegration test is what counts. With one lagged difference, the ADF statistic is -4.51, beyond the 1% critical value of -3.90, so the residual spread behaves like a mean-reverting series. Without the lag the statistic is -4.02, still past the 1% line.
The AR(1) regression gives λ = -0.1238, a half-life of 5.2 days. Over the last 20 periods the spread has a standard deviation of 1.02%, and today it sits at a z-score of 2.17: above the 2 entry line, so the rules sell A and buy B on the last day. Before that, they completed 9 round trips that captured 8.20% of spread in total, before costs.
Reading the cointegration test
The ADF statistic is compared with critical values for residuals of an estimated regression, not with the standard Dickey-Fuller table. Because the hedge ratio is chosen to make the spread look as stationary as possible, the bar is set lower. For two series with a constant in the cointegrating regression, MacKinnon’s (2010) asymptotic values are:
| Significance | Critical value | This sample size |
|---|---|---|
| 1% | -3.90 | — |
| 5% | -3.34 | -3.36 |
| 10% | -3.04 | — |
The ordinary Dickey-Fuller 5% value with a constant is about −2.86, so using it here would declare far too many pairs cointegrated. The calculator also shows the 5% value adjusted for your sample size with MacKinnon’s response-surface formula; with a few hundred observations it differs from the asymptotic figure only in the second decimal.
This is a simplified implementation. It fixes the lag at zero or one instead of choosing it by an information criterion, keeps a constant in the test regression (some software omits it because the residuals already average zero), and reports critical values rather than a p-value. Use it to screen pairs and understand the mechanics, and confirm anything you intend to trade with a full statistics package.
Thresholds, lookback and half-life
The entry threshold sets how often you trade, and the lookback sets what “normal” means. On the simulated pair:
| Setting | Completed trades | Spread captured | z-score today |
|---|---|---|---|
| Entry 1.5 | 16 | 14.18% | 2.17 |
| Entry 2.0 (default) | 9 | 8.20% | 2.17 |
| Entry 2.5 | 2 | 4.65% | 2.17 |
| Lookback 10 | 12 | 9.92% | 1.72 |
| Lookback 60 | 2 | 4.66% | 1.16 |
The same day can read as a signal or as noise depending on these choices: today’s z-score is 1.72 with a 10-day window and 1.16 with a 60-day window. That is a warning about tuning, not an invitation to it. Choosing the combination that looks best on one sample is the fastest way to build a strategy that fails out of sample.
The half-life gives a less arbitrary anchor. A lookback of a few half-lives lets the rolling mean settle without chasing every move; a lookback shorter than one half-life makes the mean follow the spread so closely that z-scores understate real dislocations. The half-life also sets expectations for holding time: if a trade is still open after several half-lives, the relationship may have changed. A coefficient of φ = 0.8000 implies a half-life of 3.11 days, while the half-life tool’s default of φ = 0.9300 gives 9.55 days.
Common mistakes
- Look-ahead bias. The hedge ratio here uses the whole sample, including days after each signal. A real backtest re-estimates it on past data only, with a rolling or expanding window.
- Ignoring costs. Every round trip pays commissions and spreads on four fills. Several trades in the example captured less than half a percent of spread, which costs can erase.
- Trusting one test. Cointegration that appears in one direction only, or in one sub-period only, is weak evidence. Mergers, index changes and new business lines break relationships without warning.
- No stop. A spread that keeps widening may be telling you the relationship has ended. Many traders close a position at a wider z-score or after a fixed number of half-lives.
- Unequal dates. Missing a day in one series shifts every later price by one row and destroys the relationship. Align the dates before pasting.
What this calculator does not include
- Rolling hedge ratios or Kalman-filter estimates that let β change over time.
- The Johansen test, which handles more than two series and does not depend on which one you regress.
- P-values, automatic lag selection or trend terms in the ADF regression.
- Costs, borrow fees and dividends. Spread captured is the gross change in the spread, in log points or dollars per share of A.
- Market data. You paste the prices; nothing is downloaded, and the default series are simulated.