OPEN-SOURCE SCRIPT

Actualizado

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:

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:

Notas de prensa

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.

Notas de prensa

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.

Notas de prensa

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).

Notas de prensa

update commentNotas de prensa

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).

Notas de prensa

update commentNotas de prensa

Update: add option to fill in with previous averaged correlation value, so that the correlation value stays constant during market close periods.Notas de prensa

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).

Notas de prensa

Update chart, forgot to add the index symbol in the last update.Notas de prensa

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:

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.

Notas de prensa

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

Notas de prensa

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.

Notas de prensa

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.

Notas de prensa

Update:* Add a few more comments on significance offset

Siguiendo fielmente el espíritu de TradingView, el autor de este script lo ha publicado en código abierto, permitiendo que otros traders puedan entenderlo y verificarlo. ¡Olé por el autor! Puede utilizarlo de forma gratuita, pero tenga en cuenta que la reutilización de este código en la publicación se rige por las Normas internas. Puede añadir este script a sus favoritos y usarlo en un gráfico.

La información y las publicaciones que ofrecemos, no implican ni constituyen un asesoramiento financiero, ni de inversión, trading o cualquier otro tipo de consejo o recomendación emitida o respaldada por TradingView. Puede obtener información adicional en las Condiciones de uso.