Spearman Rank Indicator
Forums › ProRealTime forum Italiano › Supporto ProBuilder › Spearman Rank Indicator
- This topic has 8 replies, 3 voices, and was last updated 5 years ago by
marzibre.
-
-
07/09/2019 at 6:40 PM #102300
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.htmThe 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 3size = 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);
endHere below I found other article on web:
http://thedisciplinedinvestor.com/blog/2016/12/22/a-longer-term-look-at-the-spearman-indicator/
https://protrader.org/codebase/indicators/spearman-rank-indicatorIs 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/7065Many thanks in advance
Marzio07/10/2019 at 8:40 AM #10231807/10/2019 at 9:04 AM #10231907/10/2019 at 1:38 PM #102353Traslazione 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.
Spearman1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859CALCOLO// input = prezzo, prezzo definito dall'utente, difetto sta chiudendo// method = media mobile, definito dall'utente, default è SMA// n = periodo Spearman, definito dall'utente, default è 10// sigPeriod = periodo del segnale, definito dall'utente, predefinito è 3size = series.size ();r1 [] = new int [n + 1];r22 [] = new int [n + 1];R11 [] = new double [n + 1];r21 [] = new double [n + 1];temperatura = 0;coefcorr = 0, sc = 0;cambiato = 0, trovato = 0;Absum = 0, ab = 0, AB2 = 0;for (int k = n; k dimensioni lessThan; k ++)for (int i = n; i moreOrEqual 1; i-)r1 [i] = i;r22 [i] = i;r11 [i] = series.getDouble ((k - n + i), chiave, 0);r21 [i] = series.getDouble ((k - n + i), chiave, 0);EndFor// sorta r21 decrescentecambiato = 1;mentre (cambio Morethan 0)modificato = 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] = temperatura;cambiato = 1;endIfEndForENDWHILE////for (int i = 1; i lessOrEqual n; i ++)trovato = 0;mentre (trovato lessThan 1)per (int j = 1; j lessOrEqual n; j ++)if (r21 [j] == r11 [i])r22 [i] = j;trovati = 1;endIfEndForENDWHILEEndFor/////////Absum = 0;for (int i = 1; i lessOrEqual; i ++)ab = r1 [I] - r22 [i];AB2 = ab * ab;Absum = Absum + ab2;EndForcoefcorr = 1 - ((6 * Absum) / (n * ((n * n) - 1)));Trama: sc = 100 * coefcorr;Trama: sig = ma (metodo, k, sigPeriod, Carolina del Sud);fine07/10/2019 at 1:45 PM #102355If 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);
}
//+------------------------------------------------------------------+07/10/2019 at 1:53 PM #102356Hi 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 traduca07/10/2019 at 1:56 PM #10235907/11/2019 at 7:43 AM #10243407/11/2019 at 8:00 AM #102437Buongiorno 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:
Grazie ancora
Marzio
-
AuthorPosts
Find exclusive trading pro-tools on