Problem with arrays out of range

 

Hello guys, I'm new with MQL5 and I am trying to create a indicator which calculates the standard deviation from a vector of new calculated prices of the diference between close and opening from each candle.

I have successfully created the moving average with no problem but when I use MA values to calculate the STD there is no error running the code, but there is a error of array out of range in the expert tab.

can anyone just point what is wrong with my code? 



int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,std_COBuffer,INDICATOR_DATA);
   SetIndexBuffer(0,ma_coBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(1,C_OBuffer,INDICATOR_CALCULATIONS);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---
   for(int i=0;i<rates_total;i++)
     {
        
      C_OBuffer[i] = close[i]-open[i];
      if(i>periodo)
        {
          ma_coBuffer[i]=SimpleMA(i,periodo,C_OBuffer); // OK works normal.
        }
        
      }
      
    for(int i=0;i<rates_total;i++)
      {
         if(i>=periodo)
             {
                std_COBuffer[i] = StdDev_Func(i,C_OBuffer,ma_coBuffer,periodo); //array out of range??
             }
       }          
    
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
double StdDev_Func(const int position,const double &price[],const double &ma_price[],const int period)
  {
   double std_dev=0.0;
//--- calcualte StdDev
   if(position>=period)
     {
      for(int i=0; i<period; i++)
         std_dev+=MathPow(price[position-i]-ma_price[position],2.0);
      std_dev=MathSqrt(std_dev/(period-1));
     }
//--- return calculated value
   return(std_dev);
  }
Standard Deviation - Trend Indicators - MetaTrader 5 Help
Standard Deviation - Trend Indicators - MetaTrader 5 Help
  • www.metatrader5.com
Standard Deviation — value of the market volatility measurement. This indicator describes the range of price fluctuations relative to Moving...
Files:
 
Whatever position has for a value, you are subtracting i from it, this will and must lead to a value below NULL... That's why you are out of range.

EDIT: the debugger would have shown you...
 
Dominik Egert:
Whatever position has for a value, you are subtracting i from it, this will and must lead to a value below NULL... That's why you are out of range.

EDIT: the debugger would have shown you...
yep, thats it. thank you very much
 
For the beginners it's very common problem. If you understand it's very easy to solve. Suppose an array size is 10 i.e. array[10] that it's index start from 0 to 9. Somehow inside code if there something happened that index become less than 0 or more than 9 then this array out of range happen. 
Reason: