indi gone after TF change

 

Hi guys,

I wanted to write a simple indicator showing the average percentage change of few pairs (say all USD pairs or all GBP pairs). Indi is working, but when I'm changing the chart TF it disappears. I completely have no idea why. I guess its something ultra simple :) Thanks for help!

//+------------------------------------------------------------------+
//| Average % change                                                 |
//+------------------------------------------------------------------+

#property description "Avegage % change"

//--- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 2
#property  indicator_color1  Silver
#property  indicator_color2  Red
#property  indicator_width1  1
#property  indicator_width2  2
//--- indicator parameters
extern double  treshold = 0.001;
extern string  EUR_1_________    = "";
extern string  USD_2_________    = "";
extern string  GBP_3_________    = "";
extern string  JPY_4_________    = "";
extern string  AUD_5_________    = "";
extern int currencyNum = 1;
//--- indicator buffers
double    changeHi[];
double    changeLo[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   IndicatorDigits(Digits+1);

   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_HISTOGRAM);

   SetIndexBuffer(0,changeLo);
   SetIndexBuffer(1,changeHi);

   SetIndexLabel(0,"Lo");
   SetIndexLabel(1,"Hi");

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
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[])
  {
   int i,limit;
//--- last counted bar will be recounted
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit++;
   
   string currency = "";
   if (currencyNum==1) currency = "EUR";
   if (currencyNum==2) currency = "USD";
   if (currencyNum==3) currency = "GBP";
   if (currencyNum==4) currency = "JPY";
   if (currencyNum==5) currency = "AUD";
   
   //currency="JPY";
   double val1, val2, val3, val4, val5, val6, val7;
   double avg=0;
   for(i=0; i<limit; i++)
   
   {   
     if (currency=="USD")
     {
        val1 =  iClose("EURUSD", 0, i)/iOpen("EURUSD", 0, i)-1;
        val2 =  iClose("GBPUSD", 0, i)/iOpen("GBPUSD", 0, i)-1;
        val3 =  iClose("NZDUSD", 0, i)/iOpen("NZDUSD", 0, i)-1;
        val4 =  iClose("AUDUSD", 0, i)/iOpen("AUDUSD", 0, i)-1;
        val5 = 1- iClose("USDJPY", 0, i)/iOpen("USDJPY", 0, i);
        val6 = 1- iClose("USDCHF", 0, i)/iOpen("USDCHF", 0, i);
        val7 = 1- iClose("USDCAD", 0, i)/iOpen("USDCAD", 0, i);

        avg = MathAbs(val1+val2+val3+val4+val5+val6+val7)/7; 
     }
     
     if (currency=="EUR")
     {
        val1 =  iClose("EURUSD", 0, i)/iOpen("EURUSD", 0, i)-1;
        val2 =  iClose("EURGBP", 0, i)/iOpen("EURGBP", 0, i)-1;
        val3 =  iClose("EURJPY", 0, i)/iOpen("EURJPY", 0, i)-1;
        val4 =  iClose("EURAUD", 0, i)/iOpen("EURAUD", 0, i)-1;
        val5 =  iClose("EURCAD", 0, i)/iOpen("EURCAD", 0, i)-1;
        val6 =  iClose("EURNZD", 0, i)/iOpen("EURNZD", 0, i)-1;
        val7 =  iClose("EURCHF", 0, i)/iOpen("EURCHF", 0, i)-1;
 
        avg = MathAbs(val1+val2+val3+val4+val5+val6+val7)/7; 
     }
     
     if (currency=="JPY")
     {
        val1 =  iClose("USDJPY", 0, i)/iOpen("USDJPY", 0, i)-1;
        val2 =  iClose("GBPJPY", 0, i)/iOpen("GBPJPY", 0, i)-1;
        val3 =  iClose("EURJPY", 0, i)/iOpen("EURJPY", 0, i)-1;
        val4 =  iClose("AUDJPY", 0, i)/iOpen("AUDJPY", 0, i)-1;
        
        val5 =  iClose("CADJPY", 0, i)/iOpen("CADJPY", 0, i)-1;
        val6 =  iClose("NZDJPY", 0, i)/iOpen("NZDJPY", 0, i)-1;
        val7 =  iClose("CHFJPY", 0, i)/iOpen("CHFJPY", 0, i)-1;
 
        avg = MathAbs(val1+val2+val3+val4+val5+val6+val7)/7; 
     }

     
     changeLo[i]=avg;
     if (avg>treshold)
     {
      changeHi[i]=avg;
     }
         
   }
//--- done
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

I'm 99.9999... % positive that you dealing with a zero divide

read more about zero divide and how to avoid them

 
If zero divide occurs, the terminal would become super slow to load ... well ... that is what happening to me ... and I straight away know what to do next ... :)
 
qjol:

I'm 99.9999... % positive that you dealing with a zero divide

read more about zero divide and how to avoid them


So am I.

The result from iOpen MUST be checked. If the data is being updated (error 4066), iOpen will return zero

 
qjol:

I'm 99.9999... % positive that you dealing with a zero divide

read more about zero divide and how to avoid them


yes, that was zero divide problem. I've added if(..!=) check and its fine now.

Thanks!