z-score indicator

 

Hello, MQL community

I am trying to create an indicator which returns, in Histogram format, values equivalent to the Standardize function in Excel.

However, I am trying to do it for Ranges of candles, not prices.

Description of Standardize function from Excel Help: Returns a normalized value from a distribution characterized by mean and standard_dev.

The code compiles fine but nothing prints on the separate_window. Can anyone tell me why?

Thanks in advance,

Thad

#property strict
#property indicator_separate_window
#property indicator_minimum -3
#property indicator_maximum 10
#property indicator_buffers 1

extern int Aver_Bars = 260;
                        
double zScore[];                       // Indicator array of z_scores
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,zScore);           //Assigning an array to a buffer
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,1,Gray);//Histogram Style
   //---
   return(0);
  }
//+------------------------------------------------------------------+

int start()
  {
   int i,                              //Bar index
       n,                              //Formal parameter
       counted_bars;                   //# of calculated bars
   double Sum_R,                       //Sum of Ranges
          Sum_Dev,                     //Sum of Deviations from mean
          Std_Dev;                     //Std deviation from ranges' mean
//+------------------------------------------------------------------+
   counted_bars = IndicatorCounted();  //# of calculated bars
   i = Bars - counted_bars - 1;        //#index of 1st uncounted
   while(i>=0)                         //Cycle of uncounted bars
      {
        Sum_R=0;
        Sum_Dev=0;
        Std_Dev=0;                     //Nulling at beginning of loop
        
        for(n=i;n<=i+Aver_Bars-1;n++)  //Loop of summing values
         Sum_R=Sum_R + (High[n]-Low[n]);//Accumulating range values
        
        for(n=i;n<=i+Aver_Bars-1;n++)  //Loop of summing values
         Sum_Dev=Sum_Dev+((High[n]-Low[n])-Sum_R/Aver_Bars)*
                         ((High[n]-Low[n])-Sum_R/Aver_Bars);
                                       //Sum of sq deviations from mean
         
        Std_Dev = MathSqrt(Sum_Dev/(Aver_Bars-1));//Std Deviation
        
        zScore[i]=((High[i]-Low[i]) - Sum_R/Aver_Bars) / Std_Dev;
                                       //calculation of z_score
        
        i--;                           //index of next bar
      }
     return(0);                           //Exit function start()
  }
 
Have you checked the error messages? Have you checked the data available in the Data Window?
 
You are getting array exceeded because you don't handle your lookbacks correctly (at all)
#define LOOKBACK Aver_Bars - 1
counted_bars = IndicatorCounted();  //# of calculated bars
i = Bars - MathMax(LOOKBACK, counted_bars) - 1;        //#index of 1st uncounted
while(i>=0)                         //Cycle of uncounted bars
{
   Sum_R=0;
   Sum_Dev=0;
   Std_Dev=0;                     //Nulling at beginning of loop
   for(n=i;n<=i+Aver_Bars-1;n++)  //Loop of summing values
Simplify your code: for(n=i; n-i <Aver_Bars; ++-n)
 

You can try this

 
  i = Bars - 500;
    if( counted_bars >= 500)  i = Bars - counted_bars - 1;        //#index of 1st uncounted
   while(i>=0)                         //Cycle of uncounted bars
      {

what kind of information this z score will provide ?

 

Thanks, WHRoeder. Of course, Aver_Bars was leading to arrayOutOfRange.

ffoorr, the z score informs me of ranges that are too large compared to earlier bars. 

Reason: