'zero devide' error, what's wrong with my routine?

 

Greeting,

Sometimes, I get a 'zero devide' error while running my EA.
I've narrowed the possibility and found out that when I disable a routine called trend_watch( ),
the 'zero devide' doesn't happen anymore.

I looked into that routine thoroughly but couldn't see anything that might cause deviation by zero.

I'll be glad if someone of you knowledgeable people would be kind enough to look at it,
and tell what might be the problem with this routine that cases this 'zero devide' error.

int trend_watch ( )
{

int timeframe[5], i, result=0;
int candle_life[5];
double raw_result=0, bul_gup;

timeframe[0]=5;
timeframe[1]=15;
timeframe[2]=30;
timeframe[3]=60;
timeframe[4]=240;

for (i = 4; i >=0; i--) 
    {
    candle_life[i]=100*(MathMod(TimeCurrent(),(timeframe[i]*60))/(timeframe[i]*60)); //each candle starts again when = 100
    if (candle_life[i]>95) continue;

    if (iWPR(Symbol(),timeframe[i],7,0)>-15) raw_result=raw_result-0.6; //Williams..
    if (iWPR(Symbol(),timeframe[i],7,0)<-85) raw_result=raw_result+0.6;

    if (iCCI(Symbol(),timeframe[i],9,PRICE_CLOSE,0)>iCCI(Symbol(),timeframe[i],19,PRICE_CLOSE,0)&&
    iCCI(Symbol(),timeframe[i],9,PRICE_CLOSE,0)>iCCI(Symbol(),timeframe[i],50,PRICE_CLOSE,0))
       {
//       raw_result=raw_result+1;
       raw_result=raw_result+0.6;
//       Print ("iCCI(",timeframe[i]," added ",0.5);
       }

    if (iCCI(Symbol(),timeframe[i],9,PRICE_CLOSE,0)<iCCI(Symbol(),timeframe[i],19,PRICE_CLOSE,0)&&
    iCCI(Symbol(),timeframe[i],9,PRICE_CLOSE,0)<iCCI(Symbol(),timeframe[i],50,PRICE_CLOSE,0))
       {
//       raw_result=raw_result-1;
       raw_result=raw_result-0.6;
//       Print ("iCCI(",timeframe[i]," subtructed ",0.5);
       }

    if (iADX (Symbol(), timeframe[i], 9, PRICE_CLOSE, MODE_PLUSDI, 0)>iADX (Symbol(), timeframe[i], 9, PRICE_CLOSE, MODE_MINUSDI, 0)) raw_result=raw_result+0.6;
    if (iADX (Symbol(), timeframe[i], 9, PRICE_CLOSE, MODE_PLUSDI, 0)<iADX (Symbol(), timeframe[i], 9, PRICE_CLOSE, MODE_MINUSDI, 0)) raw_result=raw_result-0.6;

    bul_gup=iBands(Symbol(),timeframe[i],20,2,0,PRICE_CLOSE,MODE_UPPER,0)-iBands(Symbol(),timeframe[i],20,2,0,PRICE_CLOSE,MODE_LOWER,0);


/*    temporary disabled until I'll have time to impruve the formula..
---------------------------------------------------------------------------
   if (iHigh(Symbol(),timeframe[i],iHighest(Symbol(),timeframe[i],MODE_HIGH,1,0))>iBands(Symbol(),timeframe[i],20,2,0,PRICE_CLOSE,MODE_UPPER,1)
      &&iHigh(Symbol(),timeframe[i],0)<iBands(Symbol(),timeframe[i],20,2,0,PRICE_CLOSE,MODE_UPPER,1)) raw_result=raw_result-1;

   if (iLow(Symbol(),timeframe[i],iLowest(Symbol(),timeframe[i],MODE_LOW,1,0))<iBands(Symbol(),timeframe[i],20,2,0,PRICE_CLOSE,MODE_LOWER,1)
      &&iLow(Symbol(),timeframe[i],0)>iBands(Symbol(),timeframe[i],20,2,0,PRICE_CLOSE,MODE_LOWER,1)) raw_result=raw_result+1;
--------------------------------------------------------------------------- */


/* if (timeframe[i]==240) result=result+(raw_result/16);
   if (timeframe[i]==60) result=result+(raw_result/4);
   if (timeframe[i]==30) result=result+(raw_result/2);
   if (timeframe[i]==15) result=result+(raw_result); */
   }  // end of 'for (i = 4..'

result=MathFloor(raw_result+0.5);
// display=display+StringConcatenate ("\n\rTrend_watch(): ",result,"   (raw_result = ",raw_result,";   LOTS_SIZE: ",MarketInfo(Symbol(),MODE_LOTSIZE)/MathPow(10,MarketInfo(Symbol(),MODE_DIGITS)));
display=display+StringConcatenate ("\n\rM30(0) tail=",tail(30,0),"%   H1(0) tail=",tail(60,0),"%  H4(0) tail=",tail(240,0),"%");
// Print ("Safly out of trend_watch()");
return (result);
}


          Thank you,


                  James 

 

I've tried your function trend_watch() and did not catch any divide by zero error. Perhaps the issue is inside tail() function, which you may post.

 
abstract_mind:

I've tried your function trend_watch() and did not catch any divide by zero error. Perhaps the issue is inside tail() function, which you may post.




    NICE !!!

 

It seems that you nailed the problem spot on . I missed that call to tail(), and when I checked it, sure enough I found a possibility to divide by zero when the size of the candle's body is zero (open price = close price).

I fixed that problem and now all we have to do is to wait and see that 'zero devide' doesn't happen anymore.

  /*------------+
  | tail sizer  |
  +-------------+
  This routine size up the requested candle's tail */

double tail (int timeframe, int shift)
 {
  int    type;
  double open=iOpen(Symbol(),timeframe,shift), close=iClose(Symbol(),timeframe,shift),
         high=iHigh(Symbol(),timeframe,shift), low=iLow(Symbol(),timeframe,shift);
  double result, upper_body,lower_body, tail, body=MathAbs(open-close);

  
  if (open<close) // if it is a green candle
     {
      type=1;
      upper_body=close;
      lower_body=open;
     }
  else
     {
      type=-1;
      upper_body=open;
      lower_body=close;
     }
  
  double upper_tail=high-upper_body;
  double lower_tail=lower_body-low;

//  tail= MathMax(upper_tail,lower_tail);
  tail= MathMax(upper_tail,lower_tail);
  if (upper_tail>lower_tail) tail=-tail;
  if (body==0) result=9999999; // the actual result should be infinite
  else result=(tail/body)*100; // in precent


/*      Alpha_TF=StringTrimLeft(StringSubstr(comment,StringFind(comment," ",3),0));
        if (Alpha_TF=="M5") timeframe=5;
        if (Alpha_TF=="M15") timeframe=15;
        if (Alpha_TF=="M30") timeframe=30;
        if (Alpha_TF=="H1") timeframe=60;
        if (Alpha_TF=="H4") timeframe=240;
        if (Alpha_TF=="D1") timeframe=1440;
        if (Alpha_TF=="W1") timeframe=10080;
        if (Alpha_TF=="MN") timeframe=43200 ;  */


return (result);
 }

 


     Thank you,

          James
Reason: