QuantsappFaster in the app: live data, alerts, tradingOpen
Log inStart free
Stock F&OPositionalHDFCBANKmedium complexity

Clustered pairs portfolio

This strategy runs many pair trades at once instead of one. It sorts a list of 36 large-cap F&O stocks into eight groups whose daily returns behave alike, picks the most closely related pairs inside each group, and trades the pairs whose price gap has stretched furthest from normal. Each pair is long one stock future and short the other, held until the gap settles.
Groups 36 large-cap stocks into clusters that move alike, then trades the most stretched pair spreads inside each cluster with stock futures, long one leg and short the other, until the spread settles.
Market viewFades moves (mean reversion)
TradesStock futures pairs from a 36-stock universe, several at once
RiskOpen-ended (loss not capped by the structure)
CapitalFutures margin
SuitsAdvanced
Trades a month3.8
Typical hold18 days
Profitable months6 of 12
Longest losing run3 trades
Made money inEntry days when NIFTY closed below its openNIFTY rising over 1% while the pair was heldChoppy, wide-range sessions on entry
Lost money inEntry days when NIFTY closed above its openNIFTY falling over 1% while the pair was heldQuiet sessions and calm India VIX below 11.5
Default settings
Symbol
Universe
N clusters
Formation days
Refit step
Max pairs per cluster
Z window
Entry z
Exit z
Max pairs
Min hold days
Exec time
Roll time
Lots a
Backtest this strategy →Change any setting, pick your own period and run it in the Quantsapp web app. Backtesting is part of the Max plan. Pro+ members can use it now as a free trial.
1

How it trades

What it trades
Universe
HDFCBANK Positional · 1 Oct 25 → 30 Sep 26
Enters
09:20 next session after a pair's spread z-score closes beyond ±2.0, most stretched first
Legs
BUY FUT · Near month (cheap stock of the pair) · Current month · 1 lotSELL FUT · Near month (rich stock, value-matched lots) · Current month · 1 lot
Exits
signal After 5 sessions, z-score back within ±0.5; closed at 09:20 next sessionroll Both legs closed 15:15 on monthly expiry, reopened next session at 09:20time Anything still open on the last test day closes at 15:15
Re-entry
Yes - new pairs open daily while slots are free; no stock in two pairs
Universe
the stocks in UNIVERSE (comma-separated), near-month futures, daily closes.
Clusters
every REFIT_STEP sessions, each stock's last FORMATION_DAYS daily log returns are standardised and the stocks are grouped into N_CLUSTERS clusters by Ward linkage. Candidate pairs are pairs inside one cluster, the MAX_PAIRS_PER_CLUSTER most correlated of them.
Spread
for each candidate pair (A, B), beta = OLS slope of log A on log B over the formation window; spread = log A - beta x log B; z = (spread - mean) / std over the Z_WINDOW closes before it.
Entry
on each session's EXEC_TIME, from yesterday's closes (signal at the close, trade T+1): candidates with |z| >= ENTRY_Z, highest |z| first, at most one open pair per cluster, no stock in two pairs, no stock in the F&O ban, until MAX_PAIRS pairs are open. z <= -ENTRY_Z -> long spread (buy A, sell B); z >= ENTRY_Z -> short spread. A is LOTS_A lots; B is the beta-weighted notional in whole lots (at least 1). Each pair is ONE position holding both legs, tagged with its side and the two stocks.
Exit
after at least MIN_HOLD_DAYS sessions, the first close where |z| <= EXIT_Z -> both legs closed at the next EXEC_TIME. The pair keeps the beta it entered with.
Roll
on a futures expiry day both legs are closed at ROLL_TIME and re-opened in the new near month at the next EXEC_TIME (stock futures cannot be held past expiry).
End
anything open is closed on the last day at ROLL_TIME.
2

Results

PRE-BUILT · DEFAULTS
HDFCBANK · 2025-10-01 → 2026-09-30 · 46 trades · before costs
Net P&L+₹32,813
Win rate52%
Max drawdown−₹2,38,855
Avg per trade+₹713
Profit factor1.05
Worst trade−₹93,705
3

Where it made and lost money

Although each trade is long one stock and short another, results in this test tracked the NIFTY backdrop closely, so the pairs were not fully market-neutral. Trades held while NIFTY rose over 1% made +₹2,59,279 over 14 trades; those held while it fell over 1% lost ₹1,54,536 over 13. Entry days when NIFTY closed below its open earned ₹3,29,976 over 21 trades, while days it closed above its open lost ₹2,97,162 over 25. Quiet entry sessions lost ₹1,04,763 over 26 trades and calm evenings with India VIX below 11.5 lost ₹77,680 over 12. Whole-lot rounding and stocks with different sensitivity to the index leave a net market tilt in each pair, and that tilt drove much of the outcome.

Session direction on the entry day
NIFTY closed above its open−₹2,97,16225 trades32% won
NIFTY closed below its open+₹3,29,97621 trades76% won
Market while the trade was held
NIFTY rose over 1% while held+₹2,59,27914 trades64% won
NIFTY within ±1% while held−₹75,51616 trades50% won
NIFTY fell over 1% while held−₹1,54,53613 trades46% won
Kind of session on the entry day
Quiet session (range under 0.75%)−₹1,04,76326 trades50% won
Trend day (closed near the high or low)−₹47,6656 trades33% won
Choppy day (wide range, closed mid-range)+₹1,85,24114 trades64% won
Trend going in
Uptrend (NIFTY above its 20-day average)−₹1,06,84515 trades40% won
Downtrend (NIFTY below its 20-day average)+₹1,39,65831 trades58% won
India VIX the evening before
VIX below 11.5 (calm)−₹77,68012 trades58% won
VIX 11.5-14.5 (normal)+₹83,81721 trades52% won
VIX above 14.5 (nervous)+₹26,67613 trades46% won
How the entry day opened
Gap up of 0.4% or more+₹59,11528 trades54% won
Flat open+₹57,48115 trades60% won
Gap down of 0.4% or more−₹83,7833 trades0% won
Expiry day (NIFTY weekly)
Expiry day+₹3,0223 trades67% won
Other days+₹29,79143 trades51% won
Day of the week
Monday+₹59,2224 trades25% won
Tuesday+₹3,0223 trades67% won
Wednesday+₹16,43633 trades55% won
Thursday−₹68,9702 trades0% won
Friday+₹23,1024 trades75% won
The same trades, split by the market conditions around each entry, before costs.
4

About this strategy

  1. Every 21 sessions, standardise each stock's last 250 daily returns and group the 36 stocks into 8 clusters of similar behaviour.
  2. Inside each cluster keep the 10 most correlated pairs and fit each pair's hedge ratio from log prices over the same 250 sessions.
  3. Each evening, measure every pair's spread as a z-score against its previous 60 closes.
  4. At 09:20 next session, open pairs whose z-score is beyond ±2.0, most stretched first: buy the cheap stock's future, sell the rich one. The first stock is 1 lot; the second is value-matched by the hedge ratio in whole lots. Both legs sit in one position.
  5. Only one open pair per cluster, no stock in two pairs, none in the F&O ban, and at most 15 pairs in all.
  6. After at least 5 sessions, close a pair at 09:20 once its z-score is back within ±0.5.
  7. On monthly expiry every open pair is closed at 15:15 and reopened in the next month at 09:20 the following session.

Why traders use it

A single pair gives few trades and depends on one relationship holding. Spreading the idea across a broad universe gives more opportunities and spreads the risk of any one link breaking. Clustering by return behaviour lets the data decide which stocks belong together, rather than relying on sector labels alone. Because each pair is long one stock and short another, the hope is that broad market swings cancel and the profit comes from relative moves.

Works best whenIn this test, pairs entered on days NIFTY closed below its open, and held while NIFTY then rose over 1%, did best. Choppy, wide-range entry sessions and normal India VIX between 11.5 and 14.5 were also profitable.
Struggles whenPairs entered on days NIFTY closed above its open and held through a fall of over 1% lost heavily. Quiet entry sessions, calm India VIX below 11.5 and NIFTY uptrends were also loss-making in this test.

The year in plain words

From 1 Oct 2025 to 30 Sep 2026 on default settings, the portfolio closed 46 trades and made ₹32,813 before costs, with a 52% win rate and a profit factor of 1.05. After ₹20 per order and bid-ask slippage it lost ₹11,255. Each monthly roll closes a pair and reopens it, so 27 of the 46 trades ended at a roll and only 17 on the spread normalising; the median hold was 18 days. Swings were large: February made ₹2,03,165 but January lost ₹1,54,328, July lost ₹87,720, and the drawdown reached ₹2,38,855 before costs. Six of 12 months were positive. Overall it roughly broke even.

What the settings do

UniverseThe comma-separated list of stocks the clusters are built from. A larger list gives more candidate pairs but more of them may be loosely related.N clustersHow many groups the universe is split into. Because only one pair per cluster can be open, this also caps how many pairs run at once.Formation daysSessions of history used for clustering, correlations and hedge ratios. Shorter windows react to recent behaviour; longer ones are steadier.Refit stepSessions between re-clustering. Smaller steps refresh the groups and pairs more often.Max pairs per clusterHow many of the most correlated pairs in each cluster are watched for signals.Z windowHow many previous closes set the spread's normal level and range for the z-score.Entry zHow stretched a spread must be to open a pair. Lower values trade more often on smaller gaps.Exit zHow close to normal the spread must return before the pair is closed. Zero waits for a full return.Max pairsThe most pairs open at once. With one pair per cluster, the number of clusters is the tighter limit at default settings.Min hold daysSessions a pair must be held before the normalisation exit can close it, to avoid closing on one day's noise.Exec timeThe time next morning at which entries, exits and post-roll re-entries are placed.Roll timeThe time on monthly expiry day at which open pairs are closed before reopening in the next month.Lots aLots of the first stock in each pair; the second stock's lots scale with it by value and hedge ratio.

Risks

  • There is no stop: a spread that keeps widening stays open until it normalises or the test ends, and the largest single loss was ₹93,705 before costs.
  • Whole-lot sizing and stocks with different index sensitivity leave each pair with a net market tilt, which drove much of the P&L here.
  • Several pairs open at once need margin for many stock futures, and a drawdown above ₹2.3 lakh before costs is possible.
  • Closing and reopening every pair at each monthly expiry adds four orders per pair, which turned a small gain into a loss after costs.

Who it suits

Experienced futures traders with substantial margin who want a rules-based relative-value portfolio, can monitor many positions and rolls, and can tolerate deep, lasting drawdowns for results close to break-even in this test.

5

Questions traders ask

Is clustered pairs trading profitable on NSE stock futures?

Barely, in this test. From 1 Oct 2025 to 30 Sep 2026 the default portfolio made ₹32,813 before costs but lost ₹11,255 after ₹20 per order and bid-ask slippage, with a drawdown of ₹2,38,855 before costs. Statutory charges are not included. Past results do not guarantee future ones.

What does clustering add to pairs trading?

Instead of choosing pairs by sector label, clustering groups stocks whose daily returns have actually moved alike over the past 250 sessions. Pairs are then picked only inside a group, and only one pair per group can be open, which spreads trades across different parts of the market.

Why did a market-neutral pairs strategy follow NIFTY?

The pairs are matched by value and hedge ratio, but lots must be whole and the two stocks rarely react to the index equally. Each pair therefore keeps some net long or short market exposure. In this test that leftover tilt mattered: trades held through a NIFTY rise over 1% made money and those held through a fall over 1% lost.

Why are there so many monthly roll exits?

Stock futures cannot be held past expiry, so every open pair is closed at 15:15 on expiry day and reopened the next morning. Each of those segments counts as a separate trade, which is why 27 of the 46 trades ended at a roll.

6

Before you rely on it

ADAPTED FOR TODAY'S NSE RULES- The source clusters NIFTY 100 cash prices and trades cash equities; NSE retail cannot carry an overnight cash short, so both legs are near-month stock futures in whole lots. - The source gives no thresholds or z window: Z_WINDOW 60, ENTRY_Z 2.0, EXIT_Z 0.5 are the spec's interpretation. N_CLUSTERS, REFIT_STEP and the per-cluster pair cap are ours. Ward linkage is computed here (no scipy in the sandbox), on squared Euclidean distance with the Lance-Williams update. - UNIVERSE is today's list of large caps (survivorship, as in the source). Daily bars of every stock come from one combined read; a stock missing from it is read on its own.
WATCH OUTsurvivorship of today's NIFTY 100; cash prices vs futures; the 2025 out-of-sample claim (Sharpe ~1.5) is the source's own. Gross of costs.
Also known asclustering pairs trading
Backtests run on historical NSE data with fills at the last traded price. "Before costs" excludes brokerage, taxes and slippage. Quantsapp is not recommending any strategy; it is just a backtesting utility. Past results are not a promise of future returns.

Log in or sign up

Enter your mobile number. New to Quantsapp? The same OTP creates your free account.

+91

By continuing you agree to the Terms of Use and Privacy Policy.