Playground
Pair Trading Cointegration Tester
Engle-Granger cointegration test: OLS hedge ratio, ADF on residuals, OU half-life, rolling z-score with entry/exit bands.
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.
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.
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.
- 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.
- 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.
Articles
- 11 min read Execution Simulation: Slippage and Impact
The math of market impact — why it scales as the square root of trade size, when linear impact dominates, and the fix that keeps backtests honest.
- 11 min read Multi-Timeframe Signal Integration With LLMs
LLMs belong on weekly fundamentals, not intraday microstructure. A two-layer architecture: weekly LLM thesis plus rule-based intraday invalidation gates.
- 10 min read Retail PnL vs Backtest
The eleven-point gap between a 14% backtest and 3.2% live PnL, decomposed across eight mechanisms with dollar examples — slippage, latency, fees, fills.
Workflows that use this tool
- Workflow Explore market structure
Inspect order books, options payoffs, and cross-venue arb in a sandbox before going live.
Use it from code
The same calculation as a JavaScript module you can import. It runs where you import it, with no request, key or rate limit.
import { compute } from "https://aifinhub.io/engines/pair-trading-tester.js";