mortdiggiddy

Spearman Rank Correlation Coefficient

I'm pleased to introduce this script in honor of the new array functions introduced to PineScript version 4.0. This update is a long time coming and opens the door to amazing scripting possibilities!

Definition

Named after Charles Spearman and denoted by the Greek letter ‘ρ’ (rho), the Spearman rank correlation coefficient is the nonparametric version of the Pearson correlation coefficient. Use the Spearman rank correlation when you have two ranked variables, and you want to see whether the two variables covary. That is, as one variable increases/decreases, the other variable tends to increase/decrease respectively.

It is best used to discover if two variables (and in this first version of the indicator, two ticker symbols) increase and decrease together. There are advantages to using this version of correlation vs. the Pearson R.

Interpretation

The value oscillates between +1 and -1.

  • A value of +1 means the two variables are perfectly correlated, that is they are increasing and decreasing together in perfect harmony.
  • A value of -1 means the two variables exhibit a perfect negative correlation, that is they increase and decrease oppositely.
  • A value of zero means the two variables are not correlated at all (noise).
Note di rilascio:
  • Removed the 'lookahead = true' parameter from the security function, not needed.
  • Fixed logic of duplicates to be more straightforward.
Note di rilascio:
  • Fixed issue with hLines.
Note di rilascio:
  • Updated symbol input to input.symbol.
Note di rilascio:
  • Fixed -1 index out of bounds exception.
Note di rilascio:
  • Removed index out of bound corner cases.
Script open-source

Nello spirito di condivisione promosso da TradingView, l'autore (al quale vanno i nostri ringraziamenti) ha deciso di pubblicare questo script in modalità open-source, così che chiunque possa comprenderlo e testarlo. Puoi utilizzarlo gratuitamente, ma il riutilizzo del codice è subordinato al rispetto del Regolamento. Per aggiungerlo al grafico, mettilo tra i preferiti.

Vuoi usare questo script sui tuoi grafici?

Commenti

Thank you for the update!!!!
500 coin
+1 Rispondi
👏
500 coin
+1 Rispondi
500 coin
Rispondi
This publication is now featured in our Editors' Picks. In the name of all TradingView traders, thank you for your valuable contribution to the TradingView community, and congrats!
+5 Rispondi
mortdiggiddy PineCoders
@PineCoders, Thank you kindly!
+1 Rispondi
Thank you for this. This is very elegant coding. The only problem for me is that this implementation of the Spearman Rank Correlation does not work as a function. Pine Script is limited and does not yet allow the use of function within functions. So, the rank function and corr function prevent the adaptation of the script to serve as a function.
+1 Rispondi
thanks for sharing

I have a published study which implements Pearson correlation coefficient, where I attempted to discover correlation between two variables of the same instrument, such as Price(Close) and OBV. What would be you thoughts of such kind of usage?

Cheers
+1 Rispondi
@dgtrd, Spearman is superior when all you are looking for is if the two instruments are increasing and decreasing together. Pearson will go a step further to give weight to the magnitudes of increases and decreases.
Rispondi
dgtrd mortdiggiddy
thnaks @mortdiggiddy,
Rispondi
Thank you for this script !
Sadly it often displays errors "Index -1 is out of bounds. Array size is 1" when using intraday time frames, and sometimes on daily time frame, depending of the source to compare to. Do you have any idea why ?
Rispondi