OPEN-SOURCE SCRIPT

Delta-Agnostic Correlation Coefficient (alt)

Aggiornato
Calculate a sort of correlation between two symbols based only on the sign of their changes, regardless of the amplitude of price change.

When positive, the two symbols tend to move together. When negative, the symbols move in opposite directions.

Since there is no significance calculation, and that the result is binary, keep in mind that correlation will always tend to go towards 1 or -1 even when there is no correlation. To reduce this issue, an EMA or SMA is applied to smooth out transitions: SMA smoothes over the selected length period but adds lag, whereas EMA smoothes amplitude without any additional lag. Hence, to know if the correlation is true or not, try to look at the amplitude and the number of consecutive days the correlation is maintained (both quantities are related), because when the correlation is spurious, it will tend to switch more or less alternatively between 1 and -1 and hence will hover around 0, whereas if the correlation is true, it will get further away from 0 and closer to 1 or -1.

In addition, since there is some time lag for the correlation to switch sign, the area is colored to know the current candle's correlation, regardless of past data's correlation: blue is a positive correlation (1), yellow is negative. The coloring can allow to know a trend reversal early on, but it's noisy.

Finally, symbols with closing days are better accounted for, with the correlation set to 0 on closed days (e.g., on week-ends), and the area is then colored in gray to signal that there is no new correlation data.

This is an improved fork over the original indicator by alexjvale, please show him some love if you like this work:

Delta Agnostic Correlation Coefficient
Note di rilascio
Updates:
* Add option to re-use previous value instead of setting correlation to 0 (but disabled by default because setting to 0 produce better results in practice, it allows to revert to the mean astime passes on, which reflects how sentiment can lose momentum over time even when market is closed).
* Add tooltips to explain the unusual options directly from the options dialog.
Note di rilascio
Update: added an adequate statistical test for the way this indicator works, by using a two-sided binomial statistical test.

Feature Description: Highlight the background only if the recent past streak of correlations is sufficiently out of the ordinary to be statistically significant. In other words, when the background is highlighted, it means that over the lookback period, both symbols were so much often correlated that statistically they are very likely correlated.

Usage: When the background is highlighted, it means that the correlation is statistically significant, and hence can be considered reliable. Combine the background highlighting with amplitude and color to confirm a strong correlation. For example, a maximally anticorrelated bar will have both a high negative value close to -1 (which means that the anticorrelation went on for several bars at least), area color will be yellow (which means that the current bar continues to be anticorrelated), and the background will be highlighted in green (which means that over the significance test lookback period, default 14 bars, both symbols were reliably anticorrelated way out of what we would expect from chance). The opposite example: a maximally correlated bar will have both a high positive value close to +1, area color will be blue, and the background will be highlighted in green.
Note di rilascio
Update:
* Fix binomial statistical test, lowerthan() side was incorrect.
* Reduce default lookback period for the significance test to 11 instead of 14.
* Added an option to use DCC_MA to define the primary direction of the H1 test (enabled by default).
* Internals: added comments to explain how functions work, and allow return of multiple outputs, to allow drawing of significance stats lines (disabled by default, can be enabled in the Style tab).

I discovered the test used here is in fact a well defined statistical test called the Sign Test. So here is an updated description of this indicator:

New description: Calculates a correlation between two symbols based only on the sign of their changes using a Sign Test (en.m.wikipedia.org/wiki/Sign_test), regardless of the amplitude of price change. Compared to a standard t-test Pearson correlation, Sign Test correlations are highly sensitive to directional change with 0 lag, at the expense of lacking sensitivity to quantity correlation (ie, it does not matter if changes are big or small).

Hence, this indicator is better used to detect early changes in correlations, and then confirmation with a typical Pearson correlation or a non-parametric Wilcoxon signed rank test can be a robust combination to demonstrate strong correlations.

Adequate statistical significance testing, using a two-sided binomial statistical test, is also implemented.

-----

Apriori, this will be the last update on this indicator, it implements all features that I could imagine, unless someone has a brillant suggestion. Next up is implementation of Wilcoxon rank test for correlation, and trying to make an indicator that combines both types of tests (quantitative and discrete).
Note di rilascio
update comment
Note di rilascio
Update:
* Modularize the main test as a standalone function f_signTest() can be helpful for forks).
* Add other types of MAs as additional options (VWMA seems particularly interesting, but EMA remains my favorite in practice as it shows the least bias).
Note di rilascio
update comment
Note di rilascio
Update: add option to fill in with previous averaged correlation value, so that the correlation value stays constant during market close periods.
Note di rilascio
Big Update:
* Corrected calculation of lower range of H1 hypothesis test. This reduces the number of false positive significance highlight.
* Added Confidence Interval band by calculating the Binomial Proportion Confidence Interval with Wilson Score including with and without continuity correction.
* Changed some default colors: blue areas become green to better highlight positive correlation ; gray areas for closed market periods are now blue.

Usage:
The k/n ratio (aqua line) represents the most likely win rate if you were to bet on whether any bar would be correlated with the index symbol, over the significance test lookback period. For example, if 0.5, then the win rate would 50%, just random chance. If 0.7, it would be 70%. The confidence band around shows where we are 95% certain the true value should be. For example, if bound is 0.5 to 0.8, it means that we are 95% sure that the true win rate should be between 50% to 80%.

The confidence interval is shown in its own range from 0.0 to 1.0 with neutral probability being 0.5, contrary to the correlation coefficient which remains between -1.0 to 1.0 with neutral correlation being 0.0. It is advised to combine the information of both plots, the correlation coefficient and the confidence interval probability, to ensure robust interpretation of current correlation.

Also keep in mind that while DCC_RAW (and hence the color of the areas of DCC_MA) are zero-lag, on the other hand DCC_MA, the confidence interval and k/n ratio are lagging indicators, as they indicate the correlation over the lookback period, although this allows to reduce noise and the slope may be predictive of the future or of long lasting trends (especially when used on longer timeframes such as weekly).
Note di rilascio
Update chart, forgot to add the index symbol in the last update.
Note di rilascio
Update:
* Fix statistical calculation errors for lowerthan side of the significance test, now the 2sided test is accurate (checked against another programming language implementation).
* Simplified statistical test and hence confidence interval: now the test is not reversed depending on positive or negative correlation, so the values can go below 0.5 when there is negative correlation. This allows for a more intuitive reading, and avoids some mixups when there is a fast change in correlation.
* Updated instructions, please read inside the script, instructions too long to be posted here. Essentially, the color changed, and how to read the indicator is better described. Excerpt of the most important part:

Usage: When positive, the two symbols tend to move together. When negative, the symbols move in opposite directions.
Since the raw DCC value (of one bar) is binary, it will always go towards 1 or -1 at each new bar, because technically there is always a correlation or anticorrelation for each bar, unless one or both markets are closed (in which case, the value is filled with 0). Nevertheless, if there is no correlation, the value will hover alternatively between -1 and 1, without a clear trend. There are two solutions for this issue: visualize historical trends, and significance testing, both of which are implemented here.

The first approach, to visualize historical trends, is by using an EMA or SMA that is applied to smooth out transitions: SMA smoothes over the selected length period but adds lag, whereas EMA smoothes amplitude without any additional lag. Hence, to know if the correlation is true or not, try to look at the amplitude and the number of consecutive days the correlation is maintained (both quantities are related), because when the correlation is spurious, it will tend to switch more or less alternatively between 1 and -1 and hence will hover around 0, whereas if the correlation is true, it will get further away from 0 and closer to 1 or -1.

Intuitively, the value of the averaged DCC (height of the curve) represents the probability of a correlation (if positive) or anticorrelation (if negative) of any bar during the selected period. For example, if the length is set to 14, and the value is 0.5, then it means that during the 14 last bars, you would have been right about 75% of the time if you had bet any bar during this period would been in positive correlation between these two symbols.

Hence, the averaged DCC allows to account for the temporality, to know not just the correlation of current bar but of a number of past bars. However, the downside is that as with any moving average indicator, the averaged correlation is lagging by about length/2, as John F. Ehlers indicated.

Consequently, to overcome this time lag of the average DCC, the raw DCC is also used by coloring the area to know the current candle's correlation, regardless of past data's correlation: green is a positive correlation (1), yellow is negative. The coloring can allow to know a trend reversal early on, but it's noisy.

Intuitively, the area's color represent the current, instantaneous binary correlation, which can be in the same direction as the average DCC correlation and hence increasing it, or in opposite direction and hence reducing it. The raw DCC (area's color) can hence be very useful to visualize the start or end of trends, that will get reflected later on the averaged DCC after the time lag period has passed.
Note di rilascio
Update:
* Add Zero-Lag EMA (ZLMA)
* Add option to offset significance highlight and curves, to counter-act the unavoidable lag due to the SMA calculation embedded
Note di rilascio
Update:
* Add directional test, so that the test can be conducted only when the index symbol is bullish ('Up') or bearish ('Down'). This is useful for example if you suspect that the index symbol is correlated with the current chart's symbol, but only when the index symbol is bullish, then you can use this feature to test this hypothesis.
Note di rilascio
Update:
* Fix significance calculation when using the directional test variants, otherwise before it was completely off.
* Fix significance window length in automatic detection of k, before it did one too many iteration.
Note di rilascio
Update:
* Add a few more comments on significance offset
CCcoefficientcorrelationCorrelation Coefficient (CC)correlationsTrend Analysis

Script open-source

In pieno spirito TradingView, l'autore di questo script lo ha pubblicato open-source, in modo che i trader possano comprenderlo e verificarlo. Un saluto all'autore! È possibile utilizzarlo gratuitamente, ma il riutilizzo di questo codice in una pubblicazione è regolato dal nostro Regolamento. Per aggiungerlo al grafico, mettilo tra i preferiti.

Vuoi usare questo script sui tuoi grafici?

Declinazione di responsabilità