//------------------------------------------------------------------ #property copyright "© mladen, 2016, MetaQuotes Software Corp." #property link "www.forex-tsd.com, www.mql5.com" #property version "1.00" //------------------------------------------------------------------ #property indicator_separate_window #property indicator_buffers 5 #property indicator_plots 1 #property indicator_label1 "correlation candles" #property indicator_type1 DRAW_COLOR_CANDLES #property indicator_color1 clrGray,clrLimeGreen,clrSandyBrown #property indicator_maximum 1 #property indicator_minimum -1 // // // // // enum enCorrType { cor_spe, // Spearman rank correlation cor_pea // Pearson rank correlation }; input int CorPeriod = 32; // Correlation rank input enCorrType CorType = cor_spe; // Correlation type double canc[],cano[],canh[],canl[],colors[]; string corrNames[] = {"Spearman","Pearson"}; //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // int OnInit() { SetIndexBuffer(0,cano ,INDICATOR_DATA); SetIndexBuffer(1,canh ,INDICATOR_DATA); SetIndexBuffer(2,canl ,INDICATOR_DATA); SetIndexBuffer(3,canc ,INDICATOR_DATA); SetIndexBuffer(4,colors,INDICATOR_COLOR_INDEX); IndicatorSetString(INDICATOR_SHORTNAME,corrNames[CorType]+" candles ("+(string)CorPeriod+")"); return(0); } //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // int OnCalculate(const int rates_total, const int prev_calculated, const datetime& time[], const double& open[], const double& high[], const double& low[], const double& close[], const long& tick_volume[], const long& volume[], const int& spread[]) { if (Bars(_Symbol,_Period)values[1]) ? 2 : (values[0]close[i]) ? values[1] : values[2]; canc[i] = (open[i]=0; k++) { double val = workPearson[i-k][instanceNo]; SumX += val; SumY += k; SumXX += val*val; SumYY += k*k; SumXY += val*k; } double SXY = period*SumXY-SumY*SumX; double SXXYY = (period*SumXX-SumX*SumX)*(period*SumYY-SumY*SumY); double result = (SXXYY!=0) ? -SXY/(MathSqrt(MathAbs(SXXYY))) : 0; return(result); } // // // // // double workSpearman[][correlationInstances]; double iSpearman(double value, int period, int i, int bars, int instanceNo=0) { if (ArrayRange(workSpearman,0)!=bars) ArrayResize(workSpearman,bars); workSpearman[i][instanceNo]=value; // // // // // double total=0; double data[]; ArrayResize(data, period); ArrayInitialize(data,0); for (int k=0; k=0; k++) data[k] = workSpearman[i-k][instanceNo]; for (int k=0; k