Playground
Pair Trading Cointegration Tester
The sample pair is borderline, fast reversion but fails stationarity test; half-life 2.2 days, ADF t = −3.27 against −3.42 at 5%.
On the documented defaults, computed by this tool's engine · the same answer as JSON
Engle-Granger cointegration test: OLS hedge ratio, ADF on residuals, OU half-life, rolling z-score with entry/exit bands.
01Playground
Runs in your browser. Nothing you enter is uploaded, and no account or API key is needed.
1. Upload two price series
Format: date,asset_a,asset_b. The first numeric column is treated as A, the second as B. Prices are log-transformed internally; the regression is log A = α + β · log B + ε. Needs ≥ 60 aligned observations.
2. Parameters
What this tool computes
Full Engle-Granger cointegration test on a price pair, plus Ornstein-Uhlenbeck mean-reversion half-life and rolling z-score of the spread for entry/exit simulation. Load the cointegrated synthetic demo or upload your own two-column price CSV.
02Receipt
What the answer above ran on, how it is computed, where the data comes from, and a link that reopens it.
Inputs
a80 valuesb80 valueslags1z_window30
The documented defaults. Change any input above and the tool recomputes.
Method
The method, step by step (below)
Engine /engines/pair-trading-tester.js, sha256 977827d4f3ff. The page, the API and MCP run the same file.
Source
Your inputs / BYO key
Page updated .
Permalink
aifinhub.io/pair-trading-tester/
The link carries the inputs you set, so it reopens this result.
03For agents
Any agent can run this calculator by URL or MCP and get the same answer as JSON: the inputs that used defaults, the result and a line to cite. The call below follows the inputs you set above. In code, import the same engine; it runs where you import it, with no request or key.
GET https://aifinhub.io/api/run/pair-trading-tester curl -s 'https://aifinhub.io/api/run/pair-trading-tester'
# Markdown instead of JSON:
curl -s 'https://aifinhub.io/api/run/pair-trading-tester?format=md' # MCP over Streamable HTTP, no sign-in: run_calculator
curl -s https://aifinhub.io/mcp \
-H 'content-type: application/json' \
-H 'accept: application/json, text/event-stream' \
-d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"run_calculator","arguments":{"calculator":"pair-trading-tester","input":{}}}}' import { compute } from "https://aifinhub.io/engines/pair-trading-tester.js";
const result = compute(input); {
"calculator": "pair-trading-tester",
"title": "Pair Trading Cointegration Tester",
"site": "AI Fin Hub",
"input": {},
"defaults_used": {
"a": [
100,
98.5874,
97.8259,
98.015,
97.6033,
97.5598,
97.4391,
95.4593,
96.8625,
97.6213,
96.884,
97.9378,
96.7864,
96.052,
96.7539,
97.0884,
96.2998,
96.8609,
96.4113,
94.7817,
94.0908,
95.2396,
94.912,
95.9782,
94.1185,
94.1316,
95.6987,
94.2103,
94.2116,
95.9806,
95.3522,
94.9435,
93.114,
94.4138,
93.5007,
92.7732,
93.5529,
92.4435,
91.7068,
91.0294,
89.8526,
90.4241,
89.7977,
88.3319,
90.5591,
91.0151,
90.6968,
90.7679,
89.7283,
90.0064,
90.209,
92.9212,
92.3561,
91.9658,
89.6928,
89.0712,
89.7689,
89.4756,
89.6975,
89.6835,
89.6376,
90.0946,
90.1572,
90.8464,
93.7497,
92.1072,
93.1928,
94.5939,
91.9662,
94.5231,
95.2861,
95.7917,
95.5093,
94.1203,
93.5651,
92.9154,
94.495,
94.4734,
94.3377,
95.4258
],
"b": [
151.141,
149.3602,
148.5811,
149.3006,
149.0083,
149.3586,
148.3213,
145.2459,
146.8073,
148.1455,
147.1322,
148.4226,
147.5568,
146.0558,
145.4098,
146.5035,
146.4691,
147.5213,
145.983,
144.2597,
142.6992,
144.0918,
143.485,
145.0805,
142.3363,
141.9733,
144.8319,
143.6638,
144.3719,
147.3897,
146.2313,
146.8656,
144.1777,
145.6877,
143.6726,
142.1663,
144.0035,
140.4674,
140.1333,
139.8269,
138.2176,
137.391,
137.1818,
134.8946,
138.3316,
138.8633,
138.7863,
139.4953,
137.8307,
138.0143,
138.1207,
141.8669,
140.5081,
139.9323,
136.2529,
135.6546,
137.3739,
137.2101,
138.5398,
139.4239,
138.4136,
138.4121,
138.646,
137.7651,
142.8305,
140.5458,
142.8138,
145.3761,
141.5616,
144.592,
146.166,
146.9859,
146.5194,
144.7299,
144.0425,
142.9379,
144.9803,
145.411,
144.2937,
146.1881
],
"lags": 1,
"z_window": 30
},
"result": {
"regression": {
"alpha": -0.7073025197522043,
"beta": 1.0571543819297125,
"residualStdev": 0.006736172926951214,
"r2": 0.9505280692548589
},
"residuals": [
0.007446657324323702,
0.005749686440291946,
0.003524403132907139,
0.00034867638482616314,
-0.001788827021623085,
-0.004716929953481497,
0.00141269714356973,
0.003035345318162186,
0.006324006254089909,
0.004534587914807098,
0.004208936951615172,
0.00579594754580981,
0.0001546540030767929,
0.0033466947594380514,
0.015313754468381013,
0.010843383008726448,
0.002935977330938755,
0.001178460577295759,
0.007607456948492519,
0.003114133961981125,
0.007295908141924379,
0.009164713234053323,
0.010180331393513598,
0.009660978507939788,
0.01028214726794019,
0.013120826399682706,
0.008557631473029126,
0.0014431777201133755,
-0.003740799470180356,
-0.0070078989737227815,
-0.005235121497054074,
-0.014106195987364067,
-0.014036591953551891,
-0.011188090667464401,
-0.006182169893898859,
-0.0028513026854533052,
-0.008056029974279788,
0.006297718905053706,
0.0008140270246794756,
-0.0042859853628947064,
-0.005060374909052534,
0.007621113050237938,
0.002280565903624243,
0.003596789788566035,
0.0019001842025891946,
0.002867379106609391,
-0.00004961538565151358,
-0.004652794861756071,
-0.003481363538841542,
-0.0017940655103041436,
-0.0003603269655201302,
0.0009714126769830855,
0.005045566456096218,
0.005151680865248309,
0.008294363449272701,
0.005992206655431431,
0.0004804624737468899,
-0.0015308979830974678,
-0.00924949884319659,
-0.016130450154329345,
-0.008954089967709855,
-0.0038572788350439424,
-0.004947652411081194,
0.009405857757831981,
0.002691840718480165,
0.002063315944491606,
-0.003142547012386032,
-0.007018861536549181,
-0.007081825437600386,
-0.0020503081227341013,
-0.005456400629014091,
-0.006077709307463763,
-0.00566962229839163,
-0.007328552657623,
-0.008211897898199538,
-0.007041840601685223,
-0.005182803758989607,
-0.008547297483170624,
-0.0018304482257125443,
-0.004151190809777994
],
"adf": {
"tStat": -3.2667924014082734,
"lags": 1,
"criticalValues": {
"1%": -4.040551637080869,
"5%": -3.415586081525312,
"10%": -3.099271433267587
},
"pValueBand": "< 10%",
"isStationary5": false
},
"halfLife": {
"halfLifeDays": 2.206887527598083,
"theta": 0.26954204104165946
},
"zscores": [
0,
-1,
-1.275882083529186,
-1.4755124290901587,
-1.4280118053721846,
-1.5274400607081426,
-0.07591781708638326,
0.31317691811025256,
1.0518898494756328,
0.5373902171883161,
0.4228737633223106,
0.8148784079542108,
-0.771047344258988,
0.16313842344376991,
2.6269526988289886,
1.454206653341733,
-0.24206515328298384,
-0.6072849602798092,
0.8061677350737454,
-0.2109694629096259,
0.7359670671253877,
1.1150036248325077,
1.2733216893463395,
1.099324350265264,
1.1806560783505113,
1.6846315928110798,
0.6717688201189896,
-0.8654360443986594,
-1.851081282452837,
-2.265074545352997,
-1.7477450384483466,
-2.794709798564448,
-2.407255024695035,
-1.8371469961638944,
-1.1311346383498533,
-0.7072314936789733,
-1.3124055403960149,
0.5100560105065273,
-0.16659547395885338,
-0.7752069889147855,
-0.8257298272313381,
0.7561626117017167,
0.07966573769459476,
0.24318997514273014,
0.09167175461099246,
0.26131188034823327,
-0.12059534702146232,
-0.712332351385419,
-0.5106256069977207,
-0.2594163708723618,
-0.030029016250859204,
0.19771162196375014,
0.8260975488493704,
0.8871515348997804,
1.3917210471584738,
1.144685255741888,
0.30415864595870906,
-0.02056824999813117,
-1.271253989535459,
-2.189869367336283,
-1.0508963688448352,
-0.3502178394525699,
-0.6139755721410725,
1.7312508843752796,
0.5349182559284822,
0.39777195230227164,
-0.5480013047816598,
-1.1585119909235164,
-1.1002370451730052,
-0.23685872198672522,
-0.8304633074227461,
-0.880675012329571,
-0.7573646265080771,
-0.9860656858301274,
-1.0699381457175225,
-0.8075000857720009,
-0.4490577942553233,
-1.0040421159000321,
0.15315687759503688,
-0.23615141184188207
],
"signals": {
"entryZ": 2,
"exitZ": 0.5,
"entries": 3,
"exits": 3,
"openAtEnd": false
},
"verdict": "borderline_reverting",
"verdictLabel": "Borderline — fast reversion but fails stationarity test"
},
"provenance": {
"run": "https://aifinhub.io/api/run/pair-trading-tester",
"page": "https://aifinhub.io/pair-trading-tester/",
"methodology": "https://aifinhub.io/pair-trading-tester/",
"contract": "https://aifinhub.io/contracts/pair-trading-tester.json",
"engine": "https://aifinhub.io/engines/pair-trading-tester.js",
"engine_sha256": "977827d4f3ff51b8207c95f664faada7d2ea467a35c1c85cbd6bf1ddc5cc6173",
"build": "1a772910d31ef1740892ed0e3a065ed71e8c7406"
},
"disclaimer": "Planning estimates only — not financial, tax, or investment advice.",
"license": "https://creativecommons.org/licenses/by/4.0/",
"cite": "AI Fin Hub, Pair Trading Cointegration Tester (engine 977827d4f3ff), https://aifinhub.io/api/run/pair-trading-tester"
} How to use it
- The page opens with a synthetic cointegrated pair. Upload your own date,asset_a,asset_b CSV with at least 60 aligned prices.
- Set the ADF lag count (0 to 10) and the rolling z-score window (default 60 observations).
- Read the cointegration result: the ADF t-statistic on the residuals of log A = α + β·log B, judged against the Engle-Granger (MacKinnon 2010) critical values.
- Check the spread's mean-reversion half-life and the verdict: tradable, borderline or not cointegrated.
- Set entry and exit z-scores and read how many entries and exits the rolling z-score triggers on the chart.
The method, step by step
Test a pair for cointegration with the Engle-Granger method: regress one price series on the other to get the hedge ratio, run an Augmented Dickey-Fuller test on the regression residual to check the spread is stationary, and estimate the Ornstein-Uhlenbeck half-life to see how fast it mean-reverts. A low ADF p-value plus a tradeable half-life is the signal; confirm it holds out of sample before trading.
-
Start from an economic reason, not a screen
Before testing, have a reason the two instruments should be linked: same sector, same supply chain, a hedging relationship, or an index-and-constituent tie. Screening thousands of pairs for cointegration guarantees false positives, because some will pass by chance just like any large multiple-comparison search. A prior reason constrains the search and makes a passing test meaningful rather than a coincidence you will trade until it breaks.
If you screened many pairs, the ones that pass are partly selection luck. Treat a data-mined cointegration result with the same suspicion as any over-searched backtest.
-
Estimate the hedge ratio
Regress one price series on the other using ordinary least squares. The slope coefficient is the hedge ratio: how many units of the second instrument offset one unit of the first to form the spread. This ratio defines the spread you will test and trade. Getting it right matters, because a mis-estimated hedge ratio produces a spread that is not actually balanced and will not mean-revert the way the test suggests.
The hedge ratio can drift over time. Re-estimate it periodically rather than fixing it once, since a stale ratio quietly unbalances the spread.
-
Run an ADF test on the residual
Take the residual of the regression, which is the spread, and run an Augmented Dickey-Fuller test on it. The ADF test checks whether the spread is stationary, meaning it reverts to a stable mean rather than wandering. A low p-value rejects the null of a non-stationary, drifting spread, which is the evidence that the pair is cointegrated. A high p-value means the spread can wander indefinitely and the pair is not safe to trade as a mean-reverting spread.
Cointegration, not correlation, is the property that matters. Two highly correlated series can still have a non-stationary spread that drifts apart and never comes back.
Cointegration Half-Life Solver -
Measure the mean-reversion half-life
A stationary spread is necessary but not sufficient; it also has to revert fast enough to trade. Fit an Ornstein-Uhlenbeck process to the spread and compute the half-life, the expected time for a deviation to decay halfway back to the mean. A half-life of a few days suits a short-horizon strategy; a half-life of many months means your capital is tied up too long for the spread to be useful. The half-life turns a yes-or-no test into a tradeability assessment.
Match the half-life to your holding horizon and capital cost. A statistically perfect spread with a six-month half-life is not tradeable for a strategy that needs to turn capital over weekly.
Cointegration Half-Life Solver -
Validate out of sample and monitor for breaks
Cointegration relationships break, often when a structural change hits one of the instruments. Confirm the relationship holds on data after the period you estimated it on, and once live, monitor the spread for signs the cointegration has broken: a spread that stops reverting and trends away is the warning. A pair that cointegrated historically but has broken is a trap that looks like an opportunity. Build the break check in from the start.
Have a rule for when a pair has broken and you exit, decided before you enter. The most dangerous pair is one that used to cointegrate and quietly stopped.
Sources: Co-Integration and Error Correction: Representation, Estimation, and Testing (Engle and Granger, Econometrica (1987)) ; Pairs Trading: Performance of a Relative-Value Arbitrage Rule (Gatev, Goetzmann, Rouwenhorst, Review of Financial Studies (2006))
Mistakes that undo the result
Trading correlation instead of cointegration
Correlated series can still have a spread that drifts apart forever. Only cointegration guarantees a mean-reverting spread, and trading correlation alone risks holding a divergence that never reverts.
Screening many pairs and trading the best
Testing thousands of pairs guarantees some pass by chance. Without an economic reason, a passing cointegration test from a large screen is largely selection luck and tends to break soon after you start trading it.
Ignoring the half-life
A stationary spread that reverts over many months ties up capital far too long to be tradeable for a short-horizon strategy. Cointegration without a workable half-life is a statistical curiosity, not a trade.
Questions people ask
What does the tester actually test?
The Engle-Granger two-step test: an OLS regression of log A on log B for the hedge ratio, then an augmented Dickey-Fuller test on the residuals judged against MacKinnon's cointegration critical values (about −3.34 at 5% for two series, stricter than the plain Dickey-Fuller −1.94). It also estimates the spread's Ornstein-Uhlenbeck half-life and counts z-score entry and exit crossings. It does not backtest P&L, Sharpe or drawdown.
Why isn't the cointegration test built in?
It is the core of the tool: every upload runs Engle-Granger first, and the verdict reads not cointegrated when the residual ADF statistic does not clear the 5% critical value. The z-score chart is shown either way so you can see what the spread did. The Cointegration Half-Life Solver covers the half-life side in more depth.
What's the difference between this and the order-book replayer?
This tool works on two price series (daily or other bars) and asks whether their spread mean-reverts. The order-book replayer displays Level-2 snapshots of one book. Neither simulates fills; for execution cost use the Execution Simulator.
How do I set the entry threshold?
Common starting points are entry at |z| = 2 and exit between 0 and 0.5. A higher entry threshold triggers fewer entries; the entry and exit counts under the chart update as you change the thresholds.
Why is pair trading notoriously hard to make work?
Three common reasons: cointegration breaks during regime changes and leaves a diverging pair, execution costs eat the edge on tight spreads, and the strategy is well known and crowded. Passing the test on one sample is not evidence the relationship will hold.
Related tools
- Calculators Portfolio Correlation Matrix
Paste a multi-asset returns CSV. See the Pearson correlation heatmap, condition number, average absolute correlation, and eigenvalue concentration — the diagnostics for detecting redundant strategies before you allocate capital.
- Playgrounds Walk-Forward Validator
Upload a returns CSV. Rolling or expanding IS/OOS windows, per-window Sharpe, walk-forward efficiency, and a concatenated OOS equity curve. Catches regime decay that PBO alone misses.
- Calculators Risk-Adjusted Returns Calculator
Paste a returns CSV. Sharpe, Sortino, Calmar, Omega, alpha, beta, tracking error, information ratio, max drawdown, and tail moments — plus a benchmark-relative block when you include one.
Articles
- 8 min read Best Sentiment & News APIs for Trading 2026
Best news and sentiment APIs for trading in 2026: Finnhub's free news at 60 calls a minute, Massive's free sentiment labels, Alpha Vantage and Benzinga.