Zero Divide Error

 

Hi

I'm making a signal table where i'm going to put signals of indicators I like, so today I made a function that returns a signal for the indicator StepMA Stoch, now the problem is that sometimes it gives an error Zero Divde and most wierd thing is that sometimes, not aways, when I remove the function the error stops to show. So the problem is in the function's code but I don't know where because I tryied a lot and got no solution,

thanks in advance.



int StepMAStochSignal(string nSymbol, int PeriodWATR=7, double Kwatr=0.7, int HighLow=0)
{
   int      i,shift,TrendMin,TrendMax,TrendMid;
   double   SminMin0,SmaxMin0,SminMin1,SmaxMin1,SumRange,dK,WATR0,WATRmax,WATRmin,WATRmid;
   double   SminMax0,SmaxMax0,SminMax1,SmaxMax1,SminMid0,SmaxMid0,SminMid1,SmaxMid1;
   double   linemin,linemax,linemid,Stoch1,Stoch2,bsmin,bsmax;
   
   double   nSignalS1[2];
   double   nSignalS2[2];   
   int      nBars;
   
   nBars = iBars(nSymbol,PERIOD_H1) - (iBars(nSymbol,PERIOD_H1)-36);
   for(shift=nBars-1;shift>=0;shift--)
   {    
    SumRange=0;
    for (i=PeriodWATR-1;i>=0;i--)
        { 
       dK = 1+1.0*(PeriodWATR-i)/PeriodWATR;
       SumRange+= dK*MathAbs(iHigh(nSymbol,PERIOD_H1,i+shift)-iLow(nSymbol,PERIOD_H1,i+shift));
       }
    WATR0 = SumRange/PeriodWATR;
    
    WATRmax=MathMax(WATR0,WATRmax);
    if (shift==nBars-1-PeriodWATR) WATRmin=WATR0;
    WATRmin=MathMin(WATR0,WATRmin);
    
   int StepSizeMin=MathRound(Kwatr*WATRmin/Point);
    int StepSizeMax=MathRound(Kwatr*WATRmax/Point);
    int StepSizeMid=MathRound(Kwatr*0.5*(WATRmax+WATRmin)/Point);
        
      if(HighLow>0)
      {
          SmaxMin0=iLow(nSymbol,PERIOD_H1,shift)+2*StepSizeMin*Point;
          SminMin0=iHigh(nSymbol,PERIOD_H1,shift)-2*StepSizeMin*Point;
      
          SmaxMax0=iLow(nSymbol,PERIOD_H1,shift)+2*StepSizeMax*Point;
          SminMax0=iHigh(nSymbol,PERIOD_H1,shift)-2*StepSizeMax*Point;
      
          SmaxMid0=iLow(nSymbol,PERIOD_H1,shift)+2*StepSizeMid*Point;
          SminMid0=iHigh(nSymbol,PERIOD_H1,shift)-2*StepSizeMid*Point;
      
          if(iClose(nSymbol,PERIOD_H1,shift)>SmaxMin1) TrendMin=1; 
          if(iClose(nSymbol,PERIOD_H1,shift)<SminMin1) TrendMin=-1;
      
          if(iClose(nSymbol,PERIOD_H1,shift)>SmaxMax1) TrendMax=1; 
          if(iClose(nSymbol,PERIOD_H1,shift)<SminMax1) TrendMax=-1;
      
          if(iClose(nSymbol,PERIOD_H1,shift)>SmaxMid1) TrendMid=1; 
          if(iClose(nSymbol,PERIOD_H1,shift)<SminMid1) TrendMid=-1;
      }
     
      if(HighLow == 0)
      {
         SmaxMin0=iClose(nSymbol,PERIOD_H1,shift)+2*StepSizeMin*Point;
          SminMin0=iClose(nSymbol,PERIOD_H1,shift)-2*StepSizeMin*Point;
      
          SmaxMax0=iClose(nSymbol,PERIOD_H1,shift)+2*StepSizeMax*Point;
          SminMax0=iClose(nSymbol,PERIOD_H1,shift)-2*StepSizeMax*Point;
      
          SmaxMid0=iClose(nSymbol,PERIOD_H1,shift)+2*StepSizeMid*Point;
          SminMid0=iClose(nSymbol,PERIOD_H1,shift)-2*StepSizeMid*Point;
      
          if(iClose(nSymbol,PERIOD_H1,shift)>SmaxMin1) TrendMin=1; 
          if(iClose(nSymbol,PERIOD_H1,shift)<SminMin1) TrendMin=-1;
      
          if(iClose(nSymbol,PERIOD_H1,shift)>SmaxMax1) TrendMax=1; 
          if(iClose(nSymbol,PERIOD_H1,shift)<SminMax1) TrendMax=-1;
      
          if(iClose(nSymbol,PERIOD_H1,shift)>SmaxMid1) TrendMid=1; 
          if(iClose(nSymbol,PERIOD_H1,shift)<SminMid1) TrendMid=-1;
      }
         
       if(TrendMin>0 && SminMin0<SminMin1) SminMin0=SminMin1;
       if(TrendMin<0 && SmaxMin0>SmaxMin1) SmaxMin0=SmaxMin1;
        
       if(TrendMax>0 && SminMax0<SminMax1) SminMax0=SminMax1;
       if(TrendMax<0 && SmaxMax0>SmaxMax1) SmaxMax0=SmaxMax1;
      
       if(TrendMid>0 && SminMid0<SminMid1) SminMid0=SminMid1;
       if(TrendMid<0 && SmaxMid0>SmaxMid1) SmaxMid0=SmaxMid1;
      
      
       if (TrendMin>0) linemin=SminMin0+StepSizeMin*Point;
       if (TrendMin<0) linemin=SmaxMin0-StepSizeMin*Point;
      
       if (TrendMax>0) linemax=SminMax0+StepSizeMax*Point;
       if (TrendMax<0) linemax=SmaxMax0-StepSizeMax*Point;
      
       if (TrendMid>0) linemid=SminMid0+StepSizeMid*Point;
       if (TrendMid<0) linemid=SmaxMid0-StepSizeMid*Point;
      
       bsmin=linemax-StepSizeMax*Point;
       bsmax=linemax+StepSizeMax*Point;
       Stoch1=(linemin-bsmin)/(bsmax-bsmin);
       Stoch2=(linemid-bsmin)/(bsmax-bsmin);
      
       if(shift == 2){nSignalS1[0] = Stoch1; nSignalS2[0] = Stoch2;} //
       if(shift == 1){nSignalS1[1] = Stoch1; nSignalS2[1] = Stoch2;}      
      
       SminMin1=SminMin0;
       SmaxMin1=SmaxMin0;
      
       SminMax1=SminMax0;
       SmaxMax1=SmaxMax0;
      
       SminMid1=SminMid0;
      SmaxMid1=SmaxMid0;
    }
   //=====================================
    //---- Interpret the signal.
   if(nSignalS1[0] > nSignalS2[0])
    {
      if(nSignalS1[1] > nSignalS2[1])
      {
         // Trend Up.
         return(3);
      }else{
         // Side.
         if(nSignalS1[1] == nSignalS2[1]){return(0);}
         // Sell.
         return(2);   
      } 
    }else{
      // Side.
      if(nSignalS1[0] == nSignalS2[0]){return(0);}
      
      if(nSignalS1[1] < nSignalS2[1])
      {
         // Trend Down.
         return(4);
      }else{
         // Side.
         if(nSignalS1[1] == nSignalS2[1]){return(0);}
         // Buy.
         return(1);   
      }       
    }
}
 

You showseveral lines where you perform division.

You will have to figure out why the divisor becomes 0 sometimes.

int StepMAStochSignal(string nSymbol, int PeriodWATR=7, double Kwatr=0.7, int HighLow=0)
I have never seen variables intitalized in the call like that. If the variables are part of the call, they should already have values.

 

That's default values so when I make a call for the function and If I don't provide any value for those variables, thats the value that they are going to get.

Thanks, I'll try to figure out what's going on.

 
Hmm... Just never seen it done that way, anywhere. Does it work?
 

I made a test, and you can't do that:

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   int x = 5;
   
   Print("When x == 5 you get ",Test1(x));
   
   x = 0;
   Print("When x == 0 you get ",Test1(x));
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

int Test1(int xx = 2){
    return(xx);
    }

Output:

2008.07.04 20:08:24    Phy__CallTest GBPJPYm,Daily: removed
2008.07.04 20:08:24    Phy__CallTest GBPJPYm,Daily: uninit reason 0
2008.07.04 20:08:24    Phy__CallTest GBPJPYm,Daily: When x == 0 you get 0
2008.07.04 20:08:24    Phy__CallTest GBPJPYm,Daily: When x == 5 you get 5
2008.07.04 20:08:24    Phy__CallTest GBPJPYm,Daily: loaded successfully
2008.07.04 20:08:18    Compiling 'Phy__CallTest'
That is likely your zero divide problem.
 

Yes correct your test, the default value in your example is going to be used if you call the function this way:

You don't need to provide the parameter, so then the default is going to be used.
 
   Print("When x == 5 you get ",Test1());
 

Ok, show me an example that does what you say...

 
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   int x = 5;
   
   Print("When x == 5 you get ",Test1(x));
   
   x = 0;
   Print("When x == 0 you get ",Test1(x));

   Print("When you don't provide nothing you get ",Test1());

   
//----
   return(0);
  }
//+------------------------------------------------------------------+

int Test1(int xx = 2){
    return(xx);
    }
 

Oh, you are "overloading" your function call.

Ok.

Time for Print statements to track down what causes the divisor to be 0.