1. Calculators
  2. Advanced Risk & Institutional

Pairs Trading Calculator

Paste the prices of two related stocks to estimate the hedge ratio, measure how stretched their spread is today, test whether the spread is cointegrated and see how fast it has reverted. Separate tools size the two legs and turn a regression coefficient into a half-life.

Updated Oct 16, 2026Free · no sign-upFormulas tested

Your inputs

Paste two price series for the same dates, oldest first. The example is simulated: 250 daily closes of two fictional stocks built to be cointegrated.

Closing prices, one per line or separated by commas. A spreadsheet column pastes directly.
Same dates and the same number of prices as A.
Regress
ADF test
periods
Window for the rolling mean and standard deviation of the spread.
Open a trade when |z| reaches this level.
Close when z comes back inside ±this level. 0 waits for the mean.

Results

Current z-score2.17At or above +2: A looks rich against B (short-spread zone)
Position under these rules
Short spread, opened on the last price
9 completed trades before this one
Hedge ratio (β)
1.1166
About $1.12 of B against each $1 of A
ADF statistic on the spread
-4.51
Cointegrated at the 1% level. 5% critical value -3.34 (asymptotic), -3.36 for this sample
Half-life of mean reversion (days)
5.2
The spread closes half of any gap in about 5.2 days
Spread standard deviation (last 20)
1.02%
In log points (≈ percent of A)

The spread is 2.17 standard deviations above its 20-day mean. The ADF test supports cointegration, and the estimated half-life of 5.2 days says how quickly gaps like this have shrunk in the sample.

Rolling 20-day z-score of the spread

-4-20241336597129161193225+2−2

Spread = ln A − 0.0133 − 1.1166 × ln B. Above the upper line the rules sell the spread; below the lower line they buy it.

Signals in the sample

Entries at |z| ≥ 2, exits once z is back inside ±0.5. The last column is the change in the spread captured, in log points (roughly the return on the A leg), before costs.

DaySignalZ-scorePrice APrice BSpread captured
28Buy A, sell B (long spread)-2.49$77.93$49.62
40Close long after 12 days0.30$81.12$51.04+0.86%
58Buy A, sell B (long spread)-2.09$74.91$48.16
71Close long after 13 days-0.20$82.26$52.07+0.64%
75Sell A, buy B (short spread)2.31$82.94$51.59
81Close short after 6 days0.08$83.30$52.46+1.43%
88Sell A, buy B (short spread)2.10$79.68$49.27
92Close short after 4 days0.42$78.99$49.37+1.10%
95Sell A, buy B (short spread)2.38$80.06$48.69
109Close short after 14 days0.04$70.67$43.69+0.38%

The hedge ratio is estimated on the whole sample, so these signals use information that was not available at the time. Treat them as a description, not a backtest.

Results are estimates from the numbers you enter. They are not financial, tax or legal advice.

How to use the pairs trading calculator

The Pair analysis tool does the full Engle-Granger workflow from raw prices:

  1. 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.
  2. Choose log or raw prices. Log prices are the usual choice for stocks; raw prices give a hedge ratio in shares.
  3. Choose the ADF lag. One lagged difference absorbs short-term autocorrelation in the spread; no lag is the plain Dickey-Fuller regression.
  4. Set the z-score lookback, the window for the rolling mean and standard deviation of the spread, and the data frequency.
  5. 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

ln At = α + β ln Bt + stStep 1 of Engle-Granger: an ordinary least squares regression of A on B over the whole sample. β is the hedge ratio and the residual st is the spread. With raw prices, A and B replace their logs and β is measured in shares.
zt = (st − meanL) ÷ sdLmeanL and sdL are the average and sample standard deviation of the last L spread values, including today's.
Δst = c + γ st−1 + δ Δst−1 + εt
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.
Half-life = −ln 2 ÷ ln(1 + λ)λ is the slope from regressing Δst on c and st−1 with no lagged differences. 1 + λ is the AR(1) coefficient φ of the spread.

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.

Frequently asked questions

What is a good z-score for entering a pairs trade?
Two standard deviations is the conventional starting point, with an exit somewhere between zero and one. There is no correct value: a lower threshold trades more often for a smaller move each time, a higher one waits for rarer, larger dislocations. In the simulated example, an entry at 1.5 produces 16 completed trades and one at 2.5 only 2.
What is the difference between correlation and cointegration?
Correlation measures whether two assets' returns move together from day to day. Cointegration asks whether a combination of their price levels stays anchored to a stable mean. Two stocks can be highly correlated while their prices drift apart for years, and that drift is what ruins a pairs trade. Cointegration is the property a spread strategy actually relies on.
How do you calculate the half-life of mean reversion?
Regress the daily change in the spread on the previous day's spread level. With slope λ, the half-life is −ln(2) ÷ ln(1 + λ). A slope of −0.07 gives a half-life of about 9.6 days. If λ is zero or positive, the spread shows no tendency to revert and the half-life is undefined.
Should I regress A on B or B on A?
It matters more than people expect. The Engle-Granger test is not symmetric: the two orderings give different hedge ratios and different test statistics, and occasionally one passes while the other fails. Run both and be wary of a pair that is cointegrated in only one direction. The Johansen test avoids the choice by treating both series symmetrically.
Why use log prices instead of raw prices?
Log prices make the hedge ratio a dollar ratio and the spread roughly a percentage, so a z-score means the same thing whether the stocks trade at $10 or $500. Raw prices give a hedge ratio in shares, which is easier to trade but drifts as price levels change. Both are common; the example gives a hedge ratio of 1.12 in logs and 1.76 shares in raw prices.

Related calculators

All calculators →

This calculator is for education and planning. It uses the formulas described above and the inputs you provide; real results depend on fees, taxes, timing and other details that a general tool cannot know. PromtFinance does not give personal financial advice. Check important decisions with your provider or a qualified professional.