OPEN-SOURCE SCRIPT

Index Construction Tool

97
🙏🏻 The most natural mathematical way to construct an index || portfolio, based on contraharmonic mean || contraharmonic weighting. If you currently traded assets do not satisfy you, why not make your own ones?

Contraharmonic mean is literally a weighted mean where each value is weighted by itself.

...

Now let me explain to you why contraharmonic weighting is really so fundamental in two ways: observation how the industry (prolly unknowably) converged to this method, and the real mathematical explanation why things are this way.

How it works in the industry.
In indexes like SPX or DJI the individual components (stocks) are weighted by market capitalization. This market cap is made of two components: number of shares outstanding and the actual price of the stock. While the number of shares holds the same over really long periods of time and changes rarely by corporate actions , the prices change all the time, so market cap is in fact almost purely based on prices itself. So when they weight index legs by market cap, it really means they weight it by stock prices. That’s the observation: even tho I never dem saying they do contraharmonic weighting, that’s what happens in reality.

Natural explanation
Now the main part: how the universe works. If you build a logical sequence of how information ‘gradually’ combines, you have this:
  1. Suppose you have the one last datapoint of each of 4 different assets;
  2. The next logical step is to combine these datapoints somehow in pairs. Pairs are created only as ratios, this reveals relationships between components, this is the only step where these fundamental operations are meaningful, they lose meaning with 3+ components. This way we will have 16 pairs: 4 of them would be 1s, 6 real ratios, and 6 more inverted ratios of these;
  3. Then the next logical step is to combine all the pairs (not the initial single assets) all together. Naturally this is done via matrices, by constructing a 4x4 design matrix where each cell will be one of these 16 pairs. That matrix will have ones in the main diagonal (because these would be smth like ES/ES, NQ/NQ etc). Other cells will be actual ratios, like ES/NQ, RTY/YM etc;
  4. Then the native way to compress and summarize all this structure is to do eigendecomposition. The only eigenvector that would be meaningful in this case is the principal eigenvector, and its loadings would be what we were hunting for. We can multiply each asset datapoint by corresponding loading, sum them up and have one single index value, what we were aiming for;
  5. Now the main catch: turns out using these principal eigenvector loadings mathematically is Exactly the same as simply calculating contraharmonic weights of those 4 initial assets. We’re done here.
  6. For the sceptics, no other way of constructing the design matrix other than with ratios would result in another type of a defined mean. Filling that design matrix with ratios Is the only way to obtain a meaningful defined mean, that would also work with negative numbers. I’m skipping a couple of details there tbh, but they don’t really matter (we don’t need log-space, and anyways the idea holds even then). But the core idea is this: only contraharmonic mean emerges there, no other mean ever does.



Finally, how to use the thing:
Good news we don't use contraharmonic mean itself because we need an internals of it: actual weights of components that make this contraharmonic mean, (so we can follow it with our position sizes). This actually allows us to also use these weights but not for addition, but for subtraction. So, the script has 2 modes (examples would follow):
  • Addition: the main one, allows you to make indexes, portfolios, baskets, groups, whatever you call it. The script will simply sum the weighted legs;
  • Subtraction: allows you to make spreads, residual spreads etc. Important: the script will subtract all the symbols From the first one. So if the first we have 3 symbols: YM, ES, RTY, the script will do YM - ES - RTY, weights would be applied to each.


At the top tight corner of the script you will see a lil table with symbols and corresponding weights you wanna trade: these are ‘already’ adjusted for point value of each leg, you don’t need to do anything, only scale them all together to meet your risk profile.

Symbols have to be added the way the default ones are added, one line : one symbol.

Pls explore the script’s Style setting:
  • You can pick a visualization method you like ! including overlays on the main chart pane !
  • Script also outputs inferred volume delta, inferred volume and inferred tick count calculated with the same method. You can use them in further calculations.


...

Examples of how you can use it
snapshot
^^ Purple dotted line: overlay from ICT script, turned on in Style settings, the contraharmonic mean itself calculated from the same assets that are on the chart: RTY1!, ES1!, NQ1!, YM1!

snapshot
^^ precious metals residual spread ( GC1! SI1! PL1! )

snapshot
^^ ZC1! vs ZW1! grain spread

snapshot
^^ BDI (Bid Dope Index), constructed from: MO, TPB, DGX, JAZZ, IIPR, CRON, CURLF, TCNNF

snapshot
^^ CL1! & BRN1! basket

snapshot
^^ resulting index price, inferred volume delta, inferred volume and inferred tick count of NQ1! vs ES1! spread

...

Synthetic assets is the whole new Universe you can jump into and never look back, if this is your way

...

Clause de non-responsabilité

Les informations et publications ne sont pas destinées à être, et ne constituent pas, des conseils ou recommandations financiers, d'investissement, de trading ou autres fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.