Need help creating custom deviation indicator - page 2

 
ExtHHBuffer[i]=ExtStdDevBuffer[ArrayMaximum(ExtStdDevBuffer,i-ExtPeriod+1,ExtPeriod)];
ExtLLBuffer[i]=ExtStdDevBuffer[ArrayMinimum(ExtStdDevBuffer,i-ExtPeriod+1,ExtPeriod)];

This seems to work. Let's see how it goes.

 
Are you using ArraySetAsSeries? That's why.
 
Dominik Egert:
Are you using ArraySetAsSeries?

No, here is the code :

#include <MovingAverages.mqh>

#property indicator_separate_window
#property indicator_buffers 5 
#property indicator_plots   5

#property indicator_label2  "StdDev"
#property indicator_type2   DRAW_LINE
#property indicator_color2  MediumSeaGreen
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

#property indicator_label3  "HH"
#property indicator_type3   DRAW_LINE
#property indicator_color3  Green
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2

#property indicator_label4  "LL"
#property indicator_type4   DRAW_LINE
#property indicator_color4  Red
#property indicator_style4  STYLE_SOLID
#property indicator_width4  2

//#property indicator_label5  "NewDev "
//#property indicator_type5   DRAW_LINE
//#property indicator_color5  clrYellow
//#property indicator_style5  STYLE_DASH
//#property indicator_width5  

//--- input parameters
input int            InpPeriod=20;   // Period
input int            InpShift=0;     // Shift

//--- indicator buffers
double ExtNewDevBuffer[];
double ExtMABuffer[];
double ExtStdDevBuffer[];
double ExtHHBuffer[];
double ExtLLBuffer[];

int    ExtPeriod,ExtShift;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- check for input values
   if(InpPeriod<=1)
     {
      ExtPeriod=20;
      PrintFormat("Incorrect value for input variable InpPeriod=%d. Indicator will use value %d for calculations.",
                   InpPeriod,ExtPeriod);
     }
   else
      ExtPeriod=InpPeriod;
   if(InpShift<0)
     {
      ExtShift=0;
      PrintFormat("Incorrect value for input variable InpShift=%d. Indicator will use value %d for calculations.",
                  InpShift,ExtShift);
     }
   else
      ExtShift=InpShift;
//--- define indicator buffers as indexes
   SetIndexBuffer(0,ExtMABuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(1,ExtStdDevBuffer);
   SetIndexBuffer(2,ExtHHBuffer);
   SetIndexBuffer(3,ExtLLBuffer);
   SetIndexBuffer(4,ExtNewDevBuffer);
  }
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,const int prev_calculated,const int begin,const double &price[])
  {
   if(rates_total<ExtPeriod)
   //if(rates_total<ExtPeriod-1+begin)
      return(0);
      
//--- starting work
   int start=prev_calculated-1;
   
   
//--- correct position for first iteration
   if(prev_calculated==0)
     {
      start=ExtPeriod-1;
      //start=ExtPeriod-1+begin;
      ArrayInitialize(ExtMABuffer,0.0);
      ArrayInitialize(ExtStdDevBuffer,0.0);
      ArrayInitialize(ExtHHBuffer,0.0);
      ArrayInitialize(ExtLLBuffer,0.0);
      ArrayInitialize(ExtNewDevBuffer,0.0);
     }
   
//--- main cycle  
   for(int i=start; i<rates_total && !IsStopped(); i++)
     {
      //--- Calculate StdDev
      ExtMABuffer[i]=SimpleMA(i,ExtPeriod,price);
      ExtStdDevBuffer[i]=StdDevFunc(price,ExtMABuffer,i);
     }  
   
   for(int i=start; i<rates_total && !IsStopped(); i++)
     {
      //--- Calculate HH and LL on ExtStdDevBuffer
      ExtHHBuffer[i]=ExtStdDevBuffer[ArrayMaximum(ExtStdDevBuffer,i-ExtPeriod+1,ExtPeriod)];
      ExtLLBuffer[i]=ExtStdDevBuffer[ArrayMinimum(ExtStdDevBuffer,i-ExtPeriod+1,ExtPeriod)];
     }   
   
   for(int i=start; i<rates_total && !IsStopped(); i++)
     {
      //--- Calculate NewDev
      ExtNewDevBuffer[i]=ExtStdDevBuffer[i]+ExtHHBuffer[i]-ExtLLBuffer[i];
     }        
     
//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }
  
//+------------------------------------------------------------------+
//| Calculate Standard Deviation                                     |
//+------------------------------------------------------------------+
double StdDevFunc(const double &price[],const double &ma_price[],const int position)
  {
   double dev=0.0;
   for(int i=0; i<ExtPeriod; i++)
      dev+=MathPow(price[position-i]-ma_price[position],2.0);
   dev=MathSqrt(dev/ExtPeriod);
   return(dev);
  }
Reason: