# Spearman Rank Indicator

Forums ProRealTime forum Italiano Supporto ProBuilder Spearman Rank Indicator

Currently, there are 0 users and 1 guest visiting this topic.
View all attachments
Viewing 9 posts - 1 through 9 (of 9 total)
• #102300 Report

Hello I discovered this indicator during a Dan Valcu speech (he was the first author to bring from Japan and promote in the West the heikin-ashi technique both as a visual and quantifiable instrument).

I found this article online the only I found a code to convert from:
https://www.motivewave.com/studies/spearman_indicator.htm

The Spearman Indicator is described by Dan Valcu in the Stocks and Commodities Magazine, February 2011. The indicator is named after Charles Spearman who was a British psychologist and mathematician of the late 19th and early 20th centuries. Spearman is a oscillator with values between +100 an -100. High plus values (+80) indicate an uptrend; high negative values (-80) represent a downtrend. A signal, which is a moving average of the Spearman, is also plotted. The user may change the input (close), method (SMA), period lengths and guide values. This indicator’s definition is further expressed in the condensed code given in the calculation below.

CALCULATION
//input = price, user defined, default is closing price
//method = moving average, user defined, default is SMA
//n = Spearman period, user defined, default is 10
//sigPeriod = signal period, user defined, default is 3

size = series.size();
r1[] = new int[n+1];
r22[] = new int[n+1];
r11[] = new double[n+1];
r21[] = new double[n+1];
temp = 0;
coefcorr = 0, sc = 0;
changed = 0, found = 0;
absum = 0, ab = 0, ab2 = 0 ;

for (int k = n; k lessThan size; k++ )
for (int i = n; i moreOrEqual 1; i–)
r1[i] = i;
r22[i] = i;
r11[i] = series.getDouble((k – n + i), key, 0);
r21[i] = series.getDouble((k – n + i), key, 0);
endFor
//sort r21 descending
changed = 1;
while (changed moreThan 0)
changed = 0;
for (int i = 1; i lessOrEqual (n-1); i++)
if (r21[i+1] lessThan r21[i])
temp = r21[i];
r21[i] = r21[i + 1];
r21[i+1] = temp;
changed = 1;
endIf
endFor
endWhile
////
for (int i = 1; i lessOrEqual n; i++)
found = 0;
while (found lessThan 1)
for (int j = 1; j lessOrEqual n; j++)
if (r21[j] == r11[i])
r22[i] = j;
found = 1;
endIf
endFor
endWhile
endFor
/////////
absum = 0;
for (int i = 1; i lessOrEqual; i++)
ab = r1[i] – r22[i];
ab2 = ab * ab;
absum = absum + ab2;
endFor
coefcorr = 1 – ((6 * absum) / (n * ((n * n) – 1)));
Plot: sc = 100 * coefcorr;
Plot: sig = ma(method, k, sigPeriod, SC);
end

Here below I found other article on web:
http://thedisciplinedinvestor.com/blog/2016/12/22/a-longer-term-look-at-the-spearman-indicator/

Is there somebody can help me to convert this code? I tested it with MT4 and I found the signal it gives are very interesting!

I found also the code in MQL4, I attached the file:
https://www.mql5.com/en/code/7065

Marzio

###### Attachments:
1. SpearmanRankCorr.mq4
#102318 Report
#102319 Report

Hi Fr7 , I used the link you suggested me to create this post? What I did wrong?

#102353 Report

Traslazione da MQ4 a PRT: “Spearman”

Nicolás sarebbe così gentile da tradurre questo codice in PRT?

Descrizione:

L’indicatore di Spearman è descritto da Dan Valcu nelle scorte e materie prime Magazine, febbraio 2011. L’indicatore prende il nome da Charles Spearman che era uno psicologo e matematico britannico della fine degli anni 20 ° secolo 19 ° e l’inizio. Lanciere è un oscillatore con valori compresi tra +100 una -100. elevati valori più (+80) indicano un rialzo; elevati valori negativi (-80) rappresentano un ribasso. Un segnale, che è una media mobile della Spearman, è anche tracciata. L’utente può cambiare l’ingresso (chiusura), metodo (SMA), lunghezze d’epoca e valori guida. definizione di questo indicatore è ulteriormente espressa nel codice condensata proposta nel calcolo di seguito.

Grazie.

###### Attachments:
#102355 Report

If it is simplest I found also mq4 code for this indicator. Many thanks in advance for your great help!

```//+------------------------------------------------------------------+ //| SpearmanRankCorrelation.mq4 | //| Copyright © 2007, MetaQuotes Software Corp. | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ // http://www.improvedoutcomes.com/docs/WebSiteDocs/Clustering/ // Clustering_Parameters/Spearman_Rank_Correlation_Distance_Metric.htm // http://www.infamed.com/stat/s05.html #property copyright "Copyright © 2007, MetaQuotes Software Corp." #property link "http://www.metaquotes.net" //---- #property indicator_separate_window #property indicator_buffers 1 #property indicator_color1 DarkBlue //---- input parameters extern int rangeN = 14; extern int CalculatedBars = 0; extern int Maxrange = 30; extern bool direction = true; //---- buffers double ExtMapBuffer1[]; double R2[]; double multiply; int PriceInt[]; int SortInt[]; //+------------------------------------------------------------------+ //| calculate RSP function | //+------------------------------------------------------------------+ double SpearmanRankCorrelation(double Ranks[], int N) { //---- double res,z2; int i; for(i = 0; i < N; i++) { z2 += MathPow(Ranks[i] - i - 1, 2); } res = 1 - 6*z2 / (MathPow(N,3) - N); //---- return(res); } //+------------------------------------------------------------------+ //| Ranking array of prices function | //+------------------------------------------------------------------+ void RankPrices(int InitialArray[]) { //---- int i, k, m, dublicat, counter, etalon; double dcounter, averageRank; double TrueRanks[]; ArrayResize(TrueRanks, rangeN); ArrayCopy(SortInt, InitialArray); for(i = 0; i < rangeN; i++) TrueRanks[i] = i + 1; if(direction) ArraySort(SortInt, 0, 0, MODE_DESCEND); else ArraySort(SortInt, 0, 0, MODE_ASCEND); for(i = 0; i < rangeN-1; i++) { if(SortInt[i] != SortInt[i+1]) continue; dublicat = SortInt[i]; k = i + 1; counter = 1; averageRank = i + 1; while(k < rangeN) { if(SortInt[k] == dublicat) { counter++; averageRank += k + 1; k++; } else break; } dcounter = counter; averageRank = averageRank / dcounter; for(m = i; m < k; m++) TrueRanks[m] = averageRank; i = k; } for(i = 0; i < rangeN; i++) { etalon = InitialArray[i]; k = 0; while(k < rangeN) { if(etalon == SortInt[k]) { R2[i] = TrueRanks[k]; break; } k++; } } //---- return; } //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators SetIndexStyle(0, DRAW_LINE); SetIndexBuffer(0, ExtMapBuffer1); ArrayResize(R2, rangeN); ArrayResize(PriceInt, rangeN); ArrayResize(SortInt, rangeN); if(Maxrange <= 0) Maxrange = 10; if(rangeN > Maxrange) IndicatorShortName("Decrease rangeN input!"); else IndicatorShortName("Spearman(" + rangeN + ")"); if(CalculatedBars < 0) CalculatedBars = 0; multiply = MathPow(10, Digits); //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int counted_bars = IndicatorCounted(); //---- if(rangeN > Maxrange) return(-1); int i, k, limit; if(counted_bars == 0) { if(CalculatedBars == 0) limit = Bars - rangeN; else limit = CalculatedBars; } if(counted_bars > 0) limit = Bars - counted_bars; for(i = limit; i >= 0; i--) { for(k = 0; k < rangeN; k++) PriceInt[k] = Close[i+k]*multiply; RankPrices(PriceInt); ExtMapBuffer1[i] = SpearmanRankCorrelation(R2,rangeN); } //---- return(0); } //+------------------------------------------------------------------+```

#102356 Report

Hi Fr7 , I used the link you suggested me to create this post? What I did wrong?

Ciao Marzibre;
Devi parlare nella lingua del tuo forum e inserire un titolo.
Devi allegare un’immagine dell’indicatore, allegare il codice, allegare il file mq4 e riassumere come ho fatto finalmente.
Guarda il mio esempio per la prossima volta
Ora spero che Nicolás lo traduca

#102359 Report

Grazie FR7 non avevo capito!!!

#102434 Report

or

#102437 Report

Buongiorno Nicolas, grazie per avermi risposto.

Conosco il concetto di ciò che deve fare ma non conosco nel dettaglio la formula che però ho trovato qui:

http://www.improvedoutcomes.com/docs/WebSiteDocs/Clustering/Clustering_Parameters/Spearman_Rank_Correlation_Distance_Metric.htm

Grazie ancora

Marzio

Viewing 9 posts - 1 through 9 (of 9 total)