PairsBalancer: trading robot for Python
PairsBalancer is a quant advisor that trades the divergence of two historically related instruments.
How the advisor works
The advisor tracks the spread between two instruments and enters on its divergence, balancing the sizes of the two legs by a computed hedge ratio so that the position stays neutral to the overall market move. Profit comes not from direction but from the spread converging back to the norm.
The foundation of pairs trading is not simple correlation but cointegration: a stable long-run equilibrium to which the spread returns. Correlation describes the synchrony of moves in the moment and can hold without any equilibrium, so a pair that looks pretty by historical correlation is the first to fall apart on a regime change.
Parameters and metrics
| Parameter | Value |
|---|---|
| Z-score window | 60 days |
| Entry threshold | |z| > 2 |
| Time stop | 20 trading days |
| Pair | EUR/USD × GBP/USD |
Why this advisor cannot be honestly tested on our data
The advisor balances two legs of a position on two correlated instruments and continuously recomputes their spread — for this it needs synchronous quotes of both symbols. A single-threaded tester on one symbol cannot reproduce this balance, and on the single EUR/USD pair there is nowhere to take the second leg of the position from.
We deliberately do not show a backtest here: a tester report computed on unsuitable data would create a false impression of how the advisor behaves on a live account.
Pros and cons
- Profit is independent of market direction.
- Market-neutral while the relationship holds.
- Automatically balances the two legs of the position.
- Correlation without cointegration is deceptive — it falls apart first.
- Requires trading two instruments simultaneously.
- A drifting hedge ratio quietly adds directional risk.
Pitfalls
A pairs advisor is undone by mistaking correlation for cointegration: the pair is chosen for the pretty historical synchrony of the charts, but synchrony of moves does not guarantee the spread will return to equilibrium — on a regime change such a pair diverges and does not converge back. The second, less visible trap is the hedge ratio: neutrality holds only with the correct ratio of leg sizes, and as that ratio drifts the "market-neutral" position accumulates directional risk even before any breakdown of the relationship.
Who it suits
For traders who understand the statistics of relationships and have infrastructure for simultaneous execution of two instruments. Requires constant monitoring of the stability of the relationship.
Related methodology
Frequently asked questions
Why is a standard tester unsuitable for a pairs advisor?
It runs one symbol at a time, while a pairs robot holds and balances two legs simultaneously. A synchronous two-symbol position cannot be reproduced in a single-threaded tester.
What is the difference between correlation and cointegration for a pairs robot?
Correlation only says the instruments move synchronously in the moment; cointegration says their spread returns to a stable equilibrium. A pairs robot profits precisely from the return to equilibrium, so a pair selected by correlation alone is more dangerous: synchrony can persist while the equilibrium is already gone.
Is it true that a pairs robot is safer than a directional one?
In a calm regime — yes, it is market-neutral. But the neutrality is conditional: it holds only with the correct ratio of leg sizes and preserved cointegration. When the equilibrium breaks, both legs go into the red at once, and the robot turns out to be a directional bet.