Indicators: SpearmanRankCorrelation_Histogram

 

SpearmanRankCorrelation_Histogram:

SpearmanRankCorrelation indicator implemented in the form of a color histogram

SpearmanRankCorrelation_Histogram

Author: Nikolay Kositsin

 

Mql4 code

//+------------------------------------------------------------------+
//| SpearmanRankCorrelation_Histogram.mq4 |
//| Copyright © 2007, MetaQuotes Software Corp. | |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
// http://www.infamed.com/stat/s05.html
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//---- indicator version number
#property version   "1.00"
//---- rendering of indicator in a separate window
#property indicator_separate_window
//---- number of indicator buffers 1
#property indicator_buffers 1 
//---- only one graphical construction was used
#property indicator_plots   1
//+----------------------------------------------+
//|| Parameters of indicator drawing |
//+----------------------------------------------+
//---- drawing the indicator as a histogram
#property indicator_type1 DRAW_HISTOGRAM
//---- as the colour of the histogram is used
#property indicator_color1 clrViolet
//---- indicator line is solid
#property indicator_style1 STYLE_SOLID
//---- indicator line thickness is 2
#property indicator_width1 2
//+----------------------------------------------+
//---- parameters of minimum and maximum indicator values
#property indicator_minimum -1.0
#property indicator_maximum +1.0
//+----------------------------------------------+
//|| Indicator input parameters |
//+----------------------------------------------+
input int  rangeN=14;
input int  CalculatedBars=0;
input int  Maxrange=30;
input bool direction=true;
input double inHighLevel=+0.5;
input double inLowLevel=-0.5;
//+----------------------------------------------+
//---- Declaration of integer variables of the data start point
int min_rates_total;
//---- declaration of dynamic arrays that will be further used as indicator buffers
double IndBuffer[],ColorIndBuffer[];
double multiply;
double R2[],TrueRanks[];
int    PriceInt[],SortInt[],Maxrange_;
//+------------------------------------------------------------------+
//| calculate RSP function|
//+------------------------------------------------------------------+
double SpearmanRankCorrelation(double &Ranks[],int N)
  {
//----
   double res,z2=0.0;

   for(int iii=0; iii<N; iii++) z2+=MathPow(Ranks[iii]-iii-1,2);
   res=1-6*z2/(MathPow(N,3)-N);
//----
   return(res);
  }
//+------------------------------------------------------------------+
//| Ranking array of prices function|
//+------------------------------------------------------------------+
void RankPrices(double &TrueRanks_[],int &InitialArray[])
  {
//----
   int i,k,m,dublicat,counter,etalon;
   double dcounter,averageRank;

   ArrayCopy(SortInt,InitialArray,0,0,WHOLE_ARRAY);

   for(i=0; i<rangeN; i++) TrueRanks_[i]=i+1;
   
   ArraySort(SortInt);
   
   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 initialisation function |
//+------------------------------------------------------------------+ 
void OnInit()
  {
//---- memory allocation for variable arrays 
   ArrayResize(R2,rangeN);
   ArrayResize(PriceInt,rangeN);
   ArrayResize(SortInt,rangeN);
//---- change of indexing of elements in the array of variables
   if(direction) ArraySetAsSeries(SortInt,true);
   ArrayResize(TrueRanks,rangeN);
//---- initialising variables
   if(Maxrange<=0) Maxrange_=10;
   else Maxrange_=Maxrange;
   multiply=MathPow(10,_Digits);
//---- turning dynamic array IndBuffer into indicator buffer
   SetIndexBuffer(0,IndBuffer,INDICATOR_DATA);
//---- shifting of the indicator drawing start countdown
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,rangeN);
//---- setting of indicator values that will not be visible on the chart
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---- prohibition to display indicator values in the upper left corner of the indicator window
   PlotIndexSetInteger(0,PLOT_SHOW_DATA,false);
//---- indexing of items in the buffer as in time series
   ArraySetAsSeries(IndBuffer,true);
//---- initialisation of the variable for the short indicator name
   string shortname;
   if(rangeN>Maxrange_) shortname="Decrease rangeN input!";
   else StringConcatenate(shortname,"SpearmanRankCorrelation_Histogram(",rangeN,")");
//--- creating a label to be displayed in DataWindow
   PlotIndexSetString(0,PLOT_LABEL,shortname);
//--- creating a name to be displayed in a separate subwindow and tooltip
   IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//--- determining the accuracy of displaying the indicator values
   IndicatorSetInteger(INDICATOR_DIGITS,2);
//---- number of horizontal levels of the indicator 
   IndicatorSetInteger(INDICATOR_LEVELS,3);
//---- values of horizontal levels of the indicator 
   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,inHighLevel);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,0);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,2,inLowLevel);
//---- pink and blue colours are used as colours of horizontal level lines 
   IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,clrMagenta);
   IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,clrGray);
   IndicatorSetInteger(INDICATOR_LEVELCOLOR,2,clrBlue);
//---- a short dashed line is used in the horizontal level line 
   IndicatorSetInteger(INDICATOR_LEVELSTYLE,0,STYLE_DASHDOTDOT);
   IndicatorSetInteger(INDICATOR_LEVELSTYLE,1,STYLE_DASH);
   IndicatorSetInteger(INDICATOR_LEVELSTYLE,2,STYLE_DASHDOTDOT);
//----
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,    // amount of history in bars on the current tick
                const int prev_calculated,// amount of history in bars on the previous tick
                const datetime &time[],
                const double &open[],
                const double& high[],     // price array of price maxima for indicator calculation
                const double& low[],      // price array of price minima for indicator calculation
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---- check the number of bars for sufficiency for calculation
   if(rates_total<rangeN) return(0);
   if(rangeN>Maxrange_) return(0);

//---- local variable declarations 
   int limit;

//---- calculating the start limit number for the bar recalculation cycle
   if(prev_calculated>rates_total || prev_calculated<=0) // check for the first start of indicator calculation
     {
      limit=rates_total-2-rangeN; // start number for calculation of all bars
     }
   else
     {
      if(!CalculatedBars) limit = rates_total - prev_calculated;
      else limit = CalculatedBars;
     }

//---- indexing of elements in arrays as in timeseries 
   ArraySetAsSeries(close,true);

//---- main indicator calculation cycle
   for(int bar=limit; bar>=0 && !IsStopped(); bar--)
     {
      for(int k=0; k<rangeN; k++) PriceInt[k]=int(close[bar+k]*multiply);

      RankPrices(TrueRanks,PriceInt);
      IndBuffer[bar]=SpearmanRankCorrelation(R2,rangeN);
     }
//---- 
   return(rates_total);
  }
//+------------------------------------------------------------------+