Error when starting EA

 

Hi!

I'm trying to fill an array in the oninit function, but sometimes when I do the backtesting it gives me critical error because it tells me that I'm dividing by zero.

The part that fills the array is as follows:

   for(int i=0;i<vol_tBars-3;i++)
     {
      double sa=atr13Buff[i];
      double s1=vol_t[i+1];
      double s3=vol_t[i+3];
      double volu=sa/atr40Buff[i]+0.5*(s1-s3);
      vol_t[i]=volu;
     }

Now, I changed the initial value of i from 0 to 1 and sometimes it still gives me the error (specially when I switch timeframes). Any idea of why this happens? What I'm doing wrong?

Why I'm doing this? I'm trying to incorporate the damiani volatmeter indicator into this EA, but I don't know if I'm doing it right.

This is the full oninit function:

double vol_t[],vol_m[];
double atr13Buff[], atr40Buff[];
int atr13, atr40;

int OnInit()
  {
//---
   ArrayResize(vol_t,vol_tBars);
   ArrayResize(vol_m,vol_tBars);

   atr13 = iATR(NULL,PERIOD_CURRENT,13);
   atr40 = iATR(NULL,PERIOD_CURRENT,40);

   CopyBuffer(atr13,0,0,vol_tBars,atr13Buff);
   CopyBuffer(atr40,0,0,vol_tBars,atr40Buff);

   for(int i=0;i<vol_tBars-3;i++)
     {
      double sa=atr13Buff[i];
      double s1=vol_t[i+1];
      double s3=vol_t[i+3];
      double volu=sa/atr40Buff[i]+0.5*(s1-s3);
      vol_t[i]=volu;
     }
   if(_Digits==2 || _Digits==4)
     {
      Pips=_Point;
     }
   else
     {
      Pips=10*_Point;
     }
//---
   return(INIT_SUCCEEDED);
  }

This is the part of the Damiani volatmeter:

int damianiVolatmeter() 
  {
   int stDev20=iStdDev(NULL,PERIOD_CURRENT,20,0,MODE_LWMA,PRICE_TYPICAL);
   int stDev100=iStdDev(NULL,PERIOD_CURRENT,100,0,MODE_LWMA,PRICE_TYPICAL);

   double stDev20Buff[],stDev100Buff[];

   CopyBuffer(atr13,0,0,vol_tBars,atr13Buff);
   CopyBuffer(atr40,0,0,vol_tBars,atr40Buff);
   CopyBuffer(stDev20,0,0,vol_tBars,stDev20Buff);
   CopyBuffer(stDev100,0,0,vol_tBars,stDev100Buff);

   double volu=0;
   double tt=0;

   int not_changed_bars=Bars(Symbol(),PERIOD_CURRENT)-1;

   if(not_changed_bars<0)return(-1);
   if(not_changed_bars>0)not_changed_bars--;

   int changed_bars=Bars(Symbol(),PERIOD_CURRENT)-not_changed_bars;

   int loop_size;
   int max_per=MathMax(40,100);
   if(changed_bars>max_per+5)loop_size=changed_bars-max_per;
   else
      loop_size=changed_bars;

   for(int i=loop_size;i>=0;i--)
     {
      double sa=atr13Buff[i];
      double s1=vol_t[i+1];
      double s3=vol_t[i+3];

      volu=sa/atr40Buff[i]+0.5*(s1-s3);

      double anti_thres=stDev20Buff[i];
      
      anti_thres=anti_thres/
                 stDev100Buff[i];

      double t=Threshold_level;
      t=t-anti_thres;
      tt=t;

      if(volu>t)
        {
         vol_t[i]=volu;
         vol_m[i]=-1;
        }
      else
        {
         vol_t[i]=volu;
         vol_m[i]=0.03;
        }

     }

   bool volSi=true;
   for(int i=0;i<voltBarsToValid;i++)
     {
      if(vol_t[i]<=tt)
        {
         volSi=false;
        }
     }
   if(volSi) return 1;

   return 0;
  }

At last, whats the difference between the iVolume function and the iVolumes indicator? And the difference between the VOLUME_REAL and VOLUME_TICK of the applied iVolumes parameter?


In advance, thank you very much!

PS

 

Pedro Severin: I'm trying to incorporate the damiani volatmeter indicator into this EA, but I don't know if I'm doing it right.

Why did you start a new post when you already had one about this indicator already open? Don't double post!
          General rules and best pratices of the Forum. - General - MQL5 programming forum
 
whroeder1:
Why did you start a new post when you already had one about this indicator already open? Don't double post!
          General rules and best pratices of the Forum. - General - MQL5 programming forum
Hi whroeder, 
I did it because this is in mt5 section and I'm also asking other things. I'm trying to do the EA in both platforms and I thought that if I wanted to ask mt5 related things I should ask them in the mt5 part of the forum. 
Reason: