Pairs Trading and Cointegration Explained
Weekly z score of the KO/PEP spread, hedge ratio fitted on 2023 onlyseries ·
2026-08-13 · 105×2
Where the KO/PEP spread sat twenty sessions later, by starting z score (2019-2025)ranking ·
2026-08-13 · 6×4
Daily-return correlation vs price-level correlation, five familiar pairs (2024-2025)ranking ·
2026-08-13 · 5×3
Hedge ratio refitted each calendar year, two sector pairsranking ·
2026-08-13 · 7×3
Weekly z score of the KO/PEP spread, hedge ratio fitted on 2023 only
Weekly z score of the KO/PEP spread, hedge ratio fitted on 2023 only
| week | z_score |
|---|---|
| 2024-01-01 | -1.53 |
| 2024-01-08 | -2.4 |
| 2024-01-15 | -2.08 |
| 2024-01-22 | -0.76 |
| 2024-01-29 | -0.76 |
| 2024-02-05 | -0.82 |
| 2024-02-12 | -1.08 |
| 2024-02-19 | -1.64 |
| 2024-02-26 | -1.39 |
| 2024-03-04 | -1.6 |
| 2024-03-11 | -1.09 |
| 2024-03-18 | 0.68 |
| 2024-03-25 | 1.07 |
| 2024-04-01 | 0.89 |
| 2024-04-08 | 1.63 |
| 2024-04-15 | 1.43 |
| 2024-04-22 | 0.32 |
| 2024-04-29 | 0.1 |
| 2024-05-06 | 0.27 |
| 2024-05-13 | 1.17 |
| 2024-05-20 | 0.67 |
| 2024-05-27 | -1.75 |
| 2024-06-03 | -2.71 |
| 2024-06-10 | -2.42 |
| 2024-06-17 | -1.59 |
| 2024-06-24 | -1.92 |
| 2024-07-01 | -1.71 |
| 2024-07-08 | -1.17 |
| 2024-07-15 | -1.2 |
| 2024-07-22 | -1.24 |
| 2024-07-29 | -1.25 |
| 2024-08-05 | -1.48 |
| 2024-08-12 | -1.49 |
| 2024-08-19 | -1.29 |
| 2024-08-26 | -2.81 |
| 2024-09-02 | -1.17 |
| 2024-09-09 | -1.11 |
| 2024-09-16 | -2.1 |
| 2024-09-23 | -1.93 |
| 2024-09-30 | -1.21 |
| 2024-10-07 | 0.3 |
| 2024-10-14 | 0.02 |
| 2024-10-21 | 1.58 |
| 2024-10-28 | 1.52 |
| 2024-11-04 | 1.8 |
| 2024-11-11 | 1.46 |
| 2024-11-18 | 0.87 |
| 2024-11-25 | 0.93 |
| 2024-12-02 | 0.63 |
| 2024-12-09 | 0.29 |
| 2024-12-16 | -0.38 |
| 2024-12-23 | -0.49 |
| 2024-12-30 | -1.04 |
| 2025-01-06 | -2.39 |
| 2025-01-13 | -1.79 |
| 2025-01-20 | -1 |
| 2025-01-27 | -1.44 |
| 2025-02-03 | -2.31 |
| 2025-02-10 | -3.1 |
| 2025-02-17 | -2.14 |
| 2025-02-24 | -1.69 |
| 2025-03-03 | -1.42 |
| 2025-03-10 | -1.13 |
| 2025-03-17 | -1.1 |
| 2025-03-24 | -1.05 |
| 2025-03-31 | -0.98 |
| 2025-04-07 | -1.45 |
| 2025-04-14 | -1.81 |
| 2025-04-21 | -2.08 |
| 2025-04-28 | -1.77 |
| 2025-05-05 | -1.53 |
| 2025-05-12 | -1.62 |
| 2025-05-19 | -1.53 |
| 2025-05-26 | -1.26 |
| 2025-06-02 | -1.04 |
| 2025-06-09 | -0.69 |
| 2025-06-16 | -0.06 |
| 2025-06-23 | -0.21 |
| 2025-06-30 | 0.12 |
| 2025-07-07 | 1.41 |
| 2025-07-14 | 3.18 |
| 2025-07-21 | 2.42 |
| 2025-07-28 | 1.43 |
| 2025-08-04 | 1.4 |
| 2025-08-11 | 1.88 |
| 2025-08-18 | 1.41 |
| 2025-08-25 | 1.42 |
| 2025-09-01 | 1.33 |
| 2025-09-08 | 1.23 |
| 2025-09-15 | 1.09 |
| 2025-09-22 | 1.12 |
| 2025-09-29 | 0.83 |
| 2025-10-06 | 2.28 |
| 2025-10-13 | 1.84 |
| 2025-10-20 | 0.37 |
| 2025-10-27 | -0.5 |
| 2025-11-03 | -2.94 |
| 2025-11-10 | -1.93 |
| 2025-11-17 | -2.58 |
| 2025-11-24 | -1.77 |
| 2025-12-01 | -0.7 |
| 2025-12-08 | 0.04 |
| 2025-12-15 | -0.11 |
| 2025-12-22 | -0.68 |
| 2025-12-29 | -0.68 |
the exact SQL behind every number
WITH
daily AS (
SELECT
date,
anyIf(toFloat64(close), ticker = 'PEP') AS pep,
anyIf(toFloat64(close), ticker = 'KO') AS ko
FROM global_markets.stocks_daily_aggs
WHERE ticker IN ('KO', 'PEP')
AND date BETWEEN '2023-01-01' AND '2025-12-31'
GROUP BY date
HAVING pep > 0 AND ko > 0
),
fitted AS (
SELECT covarSamp(pep, ko) / varSamp(ko) AS beta
FROM daily
WHERE date < '2024-01-01'
),
spread AS (
SELECT
daily.date AS date,
daily.pep - fitted.beta * daily.ko AS spread_usd
FROM daily
CROSS JOIN fitted
),
scored AS (
SELECT
date,
(spread_usd - avg(spread_usd) OVER (
ORDER BY date ROWS BETWEEN 62 PRECEDING AND CURRENT ROW))
/ stddevSampStable(spread_usd) OVER (
ORDER BY date ROWS BETWEEN 62 PRECEDING AND CURRENT ROW) AS z
FROM spread
)
SELECT
toString(toMonday(date)) AS week,
round(argMax(z, date), 2) AS z_score
FROM scored
WHERE date >= '2024-01-01'
AND isFinite(z)
GROUP BY week
ORDER BY week
More from this analysisPairs Trading and Cointegration Explained
Hedge ratio refitted each calendar year, two sector pairs
ranking 7×3
→
Where the KO/PEP spread sat twenty sessions later, by starting z score (2019-2025)
ranking 6×4
→
Daily-return correlation vs price-level correlation, five familiar pairs (2024-2025)
ranking 5×3
→
One position, one year at a time: SPY annualized Sharpe by calendar year
table 14×5
→
See all 2,173 queries →