OPEN-SOURCE SCRIPT

Pair Creation

111
🙏🏻 The one and only pair construction tech you need, unlike others:
  • Applies one consistent operation to all the data features (not only prices). Then, the script outputs these, so you can apply other calculations on these outputs.
  • calculates a very fast and native volatility based hedge ratio, that also takes into account point value (think SPY vs ES) so you can easily use it in position sizing
  • Has built-in forward pricing aka cost of carry model, so you can de-drift pairs from cost of carry, discover spot price of oil based on futures, and ofc find arbitrage opportunities
  • Also allows to make a pair as a product of 2 series, useful for triangular arbitrage


This script can make a pair in 2 ways:
  • Ratio, by dividing leg 1 by leg 2
  • Product, by multiplying leg 1 by leg 2


The real mathematically right way to construct a pair is a ratio/product (Spreads are in fact = 2 legged portfolio, but I ain't told ya that ok). Why? Because a pair of 2 entities has a mathematically unique beauty, it allows direct comparisons and relationship analysis, smth you can't do directly with 3 and more components.

Multiplication (think inversions like (EURUSD -> USDEUR), and use cases for triangular arbitrage) is useful sometimes too.

...

Quickguide:
First, "Legs" are pair components: make a pair of related assets. Don’t be guided exclusively by clustering, cointegrations, mutual information etc. Common sense and exogenous info can easily made them all Forward pricing model: is useful when u work with spot vs futures pairs. Otherwise: put financing, storage and yield all on zeros, this way u will turn it off and have a pure ratio/product of 2 legs.

  1. Look at the 2 numbers on the script’s status line: the first one would always be 1), and the second one is a variable.
  2. First number (always 1) is multiplier for your position size on leg 1
  3. The second number is the multiplier for your position size on leg 2 in the opposite direction.
  4. If both legs are related, trading your sizes with these multipliers makes you do statistical arbitrage -> trading ~ volatility in risk free mode, while the relationship between the assets is still in place.



Also guys srsly, nobody ‘ever’ made a universal law that somewhy somehow for whatever secret conspiracy reason one shall only trade pairs in mean reverting style xd. You can do whatever you want:
  • Tilt hedge ratio significantly based on relative strength of legs
  • Trade the pair in momentum style
  • Ignore hedge ratio all together
  • And more and more, the limit is your imagination, e.g.:
  • Anticipate hedge ratio changes based on exogenous info and act accordingly
  • Scalp a pair just like any other asset
  • Make a pair out of 2 pairs
  • Like I mean it, whatever you desire



About forward pricing model:
  • It’s applied only to leg 2;
  • Direct: takes spot price and finds out implied futures price
  • Inverse: takes futures price and finds out implied spot price (try on oil)
  • Pls read online how to choose parameters, it’s open access reliable info



About the hedge ratio I use:
You prolly noticed the way I prefer to use inferred volumes vs the “real” ones. In pairs it’s especially meaningful, because real volumes lose sense in pair creation. And while volumes are closely tied to volatility, the inferred volumes ‘Are’ volatility irl (and later can be converted to currency space by using point value, allowing direct comparisons symbol vs symbol).

This hedge ratio is a good example of how discovering the real nature of entities beats making 100s of inventions, why domain knowledge and proper feature engineering beats difficult bulky models, neural networks etc. How simple data understanding & operations on it is all you need.

This script simply does this:
  • Takes inferred volume delta of both assets, makes a ratio, normalizes it by tick sizes and points values of both legs, calculates a typical value of this series.
  • That’s it, no step 2, we’re done. No Kalman filters, no TLS regression, no vine copulas, or whatever new fancy keywords you can come up with etc.


...

snapshot
^^ comparing real ES prices vs theoretical ones by forward-pricing model. Financing: 0.04, yield 0.0175

snapshot
^^ EURUSD, 6E futures with theoretical futures price calculated with interest rate differential 0.02 (4% USD - 2% EUR interest rates)

snapshot
^^4 different pairs (RTY/ES, YM/ES, NQ/ES, ES/ZN) each with different plot style (pick one you like in script's Style settings)

snapshot
^^ YM/RTY pair, each plot represents ratio of different features: ratio of prices, ratio of inferred volume deltas, ratio of inferred volumes, ratio of inferred tick counts (also can be turned on/off in Style settings)

...

How can u upgrade it and make a step forward yourself:
  • On tradingview missing values are automatically fixed by backfilling, and this never becomes a thing until you hit high frequency data. You can do better and use Kalman filter for filling missing values.
  • Script contains the functions I use everywhere to calculate inferred volume delta, inferred volume, and inferred tick count.


...


Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.