TradingView
lejmer
2021年7月18日上午9點25分

Cointegration Matrix (Indices) 

Dow Jones Industrial Average IndexTVC

描述

This indicator plots a cointegration matrix for pairings of most major stock indices. The matrix is populated with ADF t-stats (from an ADF-test with 1 lag). An ADF-test (Augmented Dickey-Fuller test) tests the null hypothesis that an AR process has a unit root. If rejected, the alternative hypothesis is usually that the AR process is either stationary or trend-stationary.

Technically what we're doing is running an ADF-test on the residuals (spread) of each pairing (i.e. a cointegration test). So we're testing if there is a unit root in the spread between the two assets of a pairing. A unit root being present in the spread essentially means the spread varies randomly over time, and any mean reversion in the spread is very hard to predict, or may never happen. If there is no evidence of a unit root, the spread (distance between the assets) should remain more or less constant over time, or rise/fall in close to the same rate over time (if it can be modelled as an AR process). The more negative the number, the stronger the rejection of the idea that the spread has a unit root. So in statistics we choose a critical value that corresponds to a confidence level of the test. In this case, -3.238 equals a confidence level of 90%, -3.589 equals a confidence level of 95% and -4.375 equals a confidence level of 99%. So the colors are based on the confidence level of the test statistic (the t-stat, i.e. the number of the pairing in the matrix). So if the number is greater than -3.238 it is green, if it's between -3.238 and -3.589 it's yellow, if it's between -3.589 and -4.375 it's orange, and if its lower than -4.375 it's red.

There are multiple ways to interpret the results. A strong rejection of the presence of a unit root (i.e. a value of -4.375 or below) is not a guarantee that there is no unit root, or that any of the two alternative hypotheses (that the spread is stationary or trend-stationary) are correct. It only means that in 99% of the cases, if the spread is an AR process, the test is right, and there is no unit root in the spread. Therefore, the results of this test is no guarantee that the result proves one of the alternative solutions. Green therefore means that a unit root cannot be ruled out (which can be interpreted as "the two indices probably don't move together over time"), and red means that a unit root is likely not present (which can be interpreted as "the two indices may move together over time").

NOTE: The indicator can take a while to load since it is loading data from 20 stock indices and calculating the ADF t-stat for 380 pairings.

發布通知

Some tickers were removed from TVC, causing the indicator to stop loading. This has now been fixed by removing those tickers from the matrix.
評論
TheTradingParrot
Incredible work!

it'd be great to offer a version that allowed cherry picking of symbols (40 I think is the max).
BSSKN
Might wanna share the same matrix for a top 20 cryptos?
Tradingview might not remain in the dark depth of Bullshit TA after all...
SORAXA
This is seriously so good. Top marks to the dev/s
theodorewakefield
One of the symbols seems to be no longer recognized, causing the whole indicator to not work. Is it possible to allow user selectable symbols? Thanks
TheTradingParrot
Hi @lejmer would you be able to publish some code examples of how to calculate cointegration using pine script? Just an example with two assets, I wonder if you did:
- linear regression, how much lookback period, which timeframe, using linear or log scale for prices
- how did you calculate the spread, A- B or A/B ?
- have you tried other methods of cointegration apart from ADF?

thanks!

I've struggled so much finding decent information on this area, so unfortunate that the only script that calculates it in pine script is not open source :(
lejmer
@TheTradingParrot, there is at least one open source ADF implementation on trading view. However it is quite computationally heavy, so if you're content with a simple Dickey-Fuller test (without the augmented part, the part that doesn't assume AR(1)), all you need is a simple linear regression. The Dickey-Fuller test is just regressing differenced data (so Data - Lagged data) on the lagged data, meaning the differenced data is Y and the lagged data is X in the regression. Then you look at the T-stat (which you can calculatet by calculating the standard error of the slope). This is all that test is. Not much to it.

Regarding your questions:
- There is no lookback period. The time frame is the time frame of the chart, and the data is all data available on the chart (so for a premium user that would be roughly 20k bars for most tickers and time frames).
- The spread is A - estimated B (estimated using a linear regression where we regress the cumulative log returns of one asset on the cumulative log returns of another asset)
- Yes, but none that work within the limitations of PineScript
TheTradingParrot
@lejmer, thanks for your answers! I think I found the implementation :)
HanDollarian
hey mate, its not loading data. What am I doing wrong?
lejmer
@HanDollarian, I have fixed the issue now. Hopefully the newly published version loads for you. It was using old tickers that were no longer available, which is why it failed. These old tickers have now been removed.
HanDollarian
@lejmer, awesome. Thanks a lot!
更多