Pairs trading (cointegration)
Pairs trading takes two stocks that usually move together and trades the gap between them. This version fits a hedge ratio between HDFCBANK and ICICIBANK from a year of daily closes, re-fits it every month, and only trades when a statistical test says the two prices are cointegrated. When the gap stretches far from its recent average, it buys the cheap leg and sells the rich one.How it trades
Results
PRE-BUILT · DEFAULTSWhere it made and lost money
There is no evidence to split for this strategy. On default settings it made no trades between 1 Oct 2025 and 30 Sep 2026, so there is no record of how it fared against the NIFTY market backdrop, whether by India VIX level, session type or trend. The reason is the gate before any signal: the pair is only traded while the last hedge-ratio re-fit passes the cointegration test, and HDFCBANK and ICICIBANK never passed it at a re-fit date in this year. Until the strategy produces trades, the market conditions that suit it remain untested here.
The same trades, split by the market conditions around each entry, before costs.About this strategy
- Every 21 sessions, counted from the start of the test, regress the log price of HDFCBANK futures on ICICIBANK futures over the last 252 daily closes to get the hedge ratio.
- Test the leftover gap (the residual) for stationarity with an ADF test; the pair is tradeable only if it passes at the 5% level, and stays so until the next re-fit.
- Each evening, measure the spread's z-score against the previous 30 closes. Below -1.5 means HDFCBANK is cheap; above +1.5 means it is rich.
- At 09:16 the next session, buy 1 lot of the cheap leg's future and sell the other; ICICIBANK lots are set by the hedge ratio so both sides carry similar rupee value. Both legs sit in one position.
- Close the pair at 09:16 after the z-score crosses back to 0, if it stretches to 3.5 (stop), or after 30 sessions.
- On monthly expiry both legs are closed at 15:15 and reopened in the next month at 09:16 the following session.
- No new pair is opened while either stock is in the F&O ban.
Why traders use it
Two banks of similar size face the same rates, regulation and credit cycle, so their prices tend to travel together. When one runs ahead of the other without news, pair traders bet the gap closes. Because the trade is long one stock and short the other, a broad market move largely cancels out, leaving the relative move. The cointegration test is meant to filter out pairs whose link has broken, and re-fitting on a rolling window keeps the hedge ratio current without using future data.
The year in plain words
On default settings, from 1 Oct 2025 to 30 Sep 2026, this strategy made no trades, so there is no profit or loss before or after costs to report. The code only looks for a signal while the latest re-fit passes the 5% cointegration test. Re-fits happen every 21 sessions counted from the start date, and at none of them in this year did the HDFCBANK/ICICIBANK residual pass. A run starting on another date re-fits on different days and could see a different outcome. The honest reading is that the rule set was untested on this pair in this year, not that it worked or failed.
What the settings do
Risks
- A pair that was cointegrated in the past can decouple after a merger, results surprise or regulatory event, and the spread then keeps widening.
- Both legs are full stock futures positions; the lots are matched by hedge ratio, but whole-lot rounding leaves some net exposure.
- Closing and reopening both legs at every monthly expiry adds orders, slippage and an overnight flat gap.
- Trades can last up to 30 sessions, so margin on two futures positions is tied up for weeks.
Who it suits
Traders interested in market-neutral, statistics-driven ideas who have margin for two stock futures positions at once and can accept long stretches with no trades while the pair fails its test.
Questions traders ask
Is HDFCBANK vs ICICIBANK pairs trading profitable?
This test cannot say. With default settings it made zero trades from 1 Oct 2025 to 30 Sep 2026 because the pair never passed the 5% cointegration test at a re-fit date, so there was no profit or loss before or after costs. Looser settings may produce trades, but past results, or their absence, do not guarantee anything about future ones.
What is cointegration in pairs trading?
Two prices are cointegrated when some fixed mix of them, here HDFCBANK minus a hedge ratio times ICICIBANK in log terms, keeps returning to a stable average. Correlation only says they move the same way day to day; cointegration says the gap between them does not drift off. Pairs traders want the second, since the trade bets on the gap closing.
Why use futures instead of shares for a pair trade in India?
The short leg has to be held for days or weeks, and retail traders cannot carry a short position in cash shares overnight. Stock futures allow both a long and a short leg in one account, at the cost of whole-lot sizing and closing and reopening both legs at each monthly expiry.
How do I get this strategy to trade more often?
The gate is the cointegration test. Setting the test level to 10%, changing the formation window or the re-fit step, or choosing a different second stock changes which periods qualify. Lowering the entry z-score adds signals once the pair is tradeable. None of these changes says anything about whether the extra trades would make money.