Zero Divide (Found the issue - but why?)

 

So I have found the issue of what is causing zero divide - I have not seen this before until now. I thought I was finished with my code until this popped up! I have 3 variations of my EA. 1 works flawlessly, the other two are similar in this department (code below). I cannot understand why this would be causing zero divide?

Look for the lines with the Arrows indicating the lines that are causing problem... I have isolated it to being this problem here? Either that or I am getting an anomaly with AUDUSD backtest (I download my historical data from Forex Tester 2... seems to work with one of my EA's fine.)

//+----------------------------------------------------------------------------------------------------------------------------------------+  
//| Function that checks for an MA Cross                                                                                                   |
//+----------------------------------------------------------------------------------------------------------------------------------------+   

void CheckForMaTrade()
   {
   double CurrentSmallFish1   =  iMA(NULL,60,3,0,1,0,1),  Hour4_3   =  iMA(NULL,240,3,0,1,0,1),  Daily_3   =  iMA(NULL,1440,3,0,1,0,1); 
   double CurrentSmallFish2   =  iMA(NULL,60,5,0,1,0,1),  Hour4_5   =  iMA(NULL,240,5,0,1,0,1),  Daily_5   =  iMA(NULL,1440,5,0,1,0,1);
   double CurrentSmallFish3   =  iMA(NULL,60,8,0,1,0,1),  Hour4_8   =  iMA(NULL,240,8,0,1,0,1),  Daily_8   =  iMA(NULL,1440,8,0,1,0,1);
   double CurrentSmallFish4   =  iMA(NULL,60,10,0,1,0,1), Hour4_10  =  iMA(NULL,240,10,0,1,0,1), Daily_10  =  iMA(NULL,1440,10,0,1,0,1);
   double CurrentSmallFish5   =  iMA(NULL,60,12,0,1,0,1), Hour4_12  =  iMA(NULL,240,12,0,1,0,1), Daily_12  =  iMA(NULL,1440,12,0,1,0,1);
   double CurrentSmallFish6   =  iMA(NULL,60,15,0,1,0,1), Hour4_15  =  iMA(NULL,240,15,0,1,0,1), Daily_15  =  iMA(NULL,1440,15,0,1,0,1);
   double CurrentBigFish1     =  iMA(NULL,60,30,0,1,0,1), Hour4_30  =  iMA(NULL,240,30,0,1,0,1), Daily_30  =  iMA(NULL,1440,30,0,1,0,1);
   double CurrentBigFish2     =  iMA(NULL,60,35,0,1,0,1), Hour4_35  =  iMA(NULL,240,35,0,1,0,1), Daily_35  =  iMA(NULL,1440,35,0,1,0,1);
   double CurrentBigFish3     =  iMA(NULL,60,40,0,1,0,1), Hour4_40  =  iMA(NULL,240,40,0,1,0,1), Daily_40  =  iMA(NULL,1440,40,0,1,0,1);
   double CurrentBigFish4     =  iMA(NULL,60,45,0,1,0,1), Hour4_45  =  iMA(NULL,240,45,0,1,0,1), Daily_45  =  iMA(NULL,1440,45,0,1,0,1);
   double CurrentBigFish5     =  iMA(NULL,60,50,0,1,0,1), Hour4_50  =  iMA(NULL,240,50,0,1,0,1), Daily_50  =  iMA(NULL,1440,50,0,1,0,1);
   double CurrentBigFish6     =  iMA(NULL,60,60,0,1,0,1), Hour4_60  =  iMA(NULL,240,60,0,1,0,1), Daily_60  =  iMA(NULL,1440,60,0,1,0,1);
   double ema21               =  iMA(NULL,60,21,0,1,0,1);

 //-------------------(-H1 Fish-)------------------\\  - //------------(-H4 Fish-)-----------\\ - //------------(-D1 Fish-)-----------\\
   
  // Check for Moving Averages Fanned up ON THE DAILY TIME FRAME, creating an UP bias.   
    if(D1_Bias=="None") 
      if(Daily_3>Daily_5)
         if(Daily_5>Daily_8)
            if(Daily_8>Daily_10)
               if(Daily_10>Daily_12)
                  if(Daily_12>Daily_15)
                     if(Daily_15>Daily_30)
                        if(Daily_30>Daily_35)
                           if(Daily_35>Daily_40)
                              if(Daily_40>Daily_45)
                                 if(Daily_45>Daily_50)
                                    if(Daily_50>Daily_60)
                                       {
                                       D1_Bar=Time[1];
                                       D1_Bias="Daily is Up";
                                       Comment("Bias is: "+D1_Bias+" since: "+TimeToStr(D1_Bar,TIME_DATE|TIME_MINUTES));
                                       }
  
  // Check for Moving Averages Fanned up ON THE 4 HOUR TIME FRAME, creating an UP bias.  
    if(D1_Bias=="Daily is Up" && H4_Bias=="None") 
      if(Hour4_3>Hour4_5)
         if(Hour4_5>Hour4_8)
            if(Hour4_8>Hour4_10)
               if(Hour4_10>Hour4_12)
                  if(Hour4_12>Hour4_15)
                     if(Hour4_15>Hour4_30)
                        if(Hour4_30>Hour4_35)
                           if(Hour4_35>Hour4_40)
                              if(Hour4_40>Hour4_45)
                                 if(Hour4_45>Hour4_50)
                                    if(Hour4_50>Hour4_60)
                                       {
                                       H4_Bar=Time[1];
                                       H4_Bias="4 Hour is Up";
                                       Comment("Bias is: "+H4_Bias+" since: "+TimeToStr(H4_Bar,TIME_DATE|TIME_MINUTES));
                                       }
   
   // Check for Moving Averages Fanned up on H1, creating an UP bias.
   if(D1_Bias=="Daily is Up" && H4_Bias=="4 Hour is Up" && H1_Bias=="None")
      if(CurrentSmallFish1>CurrentSmallFish2)
         if(CurrentSmallFish2>CurrentSmallFish3)
            if(CurrentSmallFish3>CurrentSmallFish4)
               if(CurrentSmallFish4>CurrentSmallFish5)
                  if(CurrentSmallFish5>CurrentSmallFish6)
                     if(CurrentSmallFish6>CurrentBigFish1)
                        if(CurrentBigFish1>CurrentBigFish2)
                           if(CurrentBigFish2>CurrentBigFish3)
                              if(CurrentBigFish3>CurrentBigFish4)
                                 if(CurrentBigFish4>CurrentBigFish5)
                                    if(CurrentBigFish5>CurrentBigFish6)
                                       {
                                       triggerBarTime=Time[1];
                                       H1_Bias="H1 is Up";
                                       Comment("Bias is: "+H1_Bias+" since: "+TimeToStr(triggerBarTime,TIME_DATE|TIME_MINUTES));
                                       H4_Bias="4 Hour is Up";
                                       Comment("Bias is: "+H4_Bias+" since: "+TimeToStr(H4_Bar,TIME_DATE|TIME_MINUTES));
                                       D1_Bias="Daily is Up";
                                       Comment("Bias is: "+D1_Bias+" since: "+TimeToStr(D1_Bar,TIME_DATE|TIME_MINUTES));
                                       }
   
   
   
   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////   

   H1_low  = iLow(NULL, PERIOD_H1, 1);
   H1_close = iClose(NULL, PERIOD_H1, 1);
   if(H1_Bias=="H1 is Up" && H4_Bias=="4 Hour is Up" && D1_Bias=="Daily is Up" && H1_close > CurrentBigFish6)
      {
       
        if(ema21 - H1_low > Point / 2)  // << These parts here?
            {
            PullBack_Bar = Time[1];  // << These parts here?
            }
            if(PullBack_Bar > triggerBarTime)  // << These parts here?
                {
                H1_Buy_Touch = "H1 Buy Touch";
                OrderEntry(0); // Pending order Buy Stop function is called.
                }


  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  
// The other way I write it on the other EA thats works perfectly is:

  ///////////////////////////////////////////////////////////////////////////////////////

   H1_high  = iHigh(NULL, PERIOD_H1, 1);
   H1_close = iClose(NULL, PERIOD_H1, 1);
   H4_close = iClose(NULL, PERIOD_H4, 1);
   D1_close = iClose(NULL, PERIOD_D1, 1);
   
   if(H1_Bias=="Down" && H4_Bias=="4 Hour is Down" && D1_Bias=="Daily is Down" && H1_high >= ema21 && H1_close < CurrentBigFish6)
      {
      H1_Sell_Touch = "H1 Sell Touch";
         {
         OrderEntry(1); // Pending order Sell Stop function is called.
         }
      }
 
DomGilberto:

So I have found the issue of what is causing zero divide - I have not seen this before until now. I thought I was finished with my code until this popped up! I have 3 variations of my EA. 1 works flawlessly, the other two are similar in this department (code below). I cannot understand why this would be causing zero divide?

Look for the lines with the Arrows indicating the lines that are causing problem... I have isolated it to being this problem here? Either that or I am getting an anomaly with AUDUSD backtest (I download my historical data from Forex Tester 2... seems to work with one of my EA's fine.)

I'm not sure you have found the issue . . . how do you know you have ?

I don't think the code you have arrowed will cause a divide by zero error, to be certain replace this . . .

if(ema21 - H1_low > Point / 2)  // << These parts here?

with this . . .

double HalfAPoint = Point / 2.0;



if(ema21 - H1_low > HalfAPoint) 

and try again.

I suspect your error will remain as it is located somewhere else . . .

 

I can only locate one /division problem above.

The zero divide isn't coming from the above function.

If you don't want to show all the codes then:

Track down all your division problems within ea, custom_indicators and included files.

Make sure the expression on right_side of / cannot equal 0.

Added:

On another note, you could really benefit from ArraySort().

If you wanna know if Daily_3 is highest/lowest, make array and sort.

 
RaptorUK:

I'm not sure you have found the issue . . . how do you know you have ?

I don't think the code you have arrowed will cause a divide by zero error, to be certain replace this . . .

with this . . .

and try again.

I suspect your error will remain as it is located somewhere else . . .


I have "V1-V2-V3" of my EA.

I essentially copied over V1 to a new blank template and changed the part I illustrated above over to (below code) and it gave me a zero divide error during a back-test on AUDUSD. Does the data have anything to do with zero divide? When I run a back-test of all the version of my EA on EURUSD from 2001-2013 (Forex Tester downloaded data and imported in ST), I do not get any errors on any one of my EA's?

            PullBack_Bar = Time[1];  // << These parts here?
            }
            if(PullBack_Bar > triggerBarTime)  // << These parts here?
                {
                H1_Buy_Touch = "H1 Buy Touch";
                OrderEntry(0); // Pending order Buy Stop function is called.
                }
 
Im just running through other pairs and this zero divide problem doesn't seem to be showing up unless its on AUDUSD with V2 and V3? Correct me if the data has nothing to do with it?
 
DomGilberto:
Im just running through other pairs and this zero divide problem doesn't seem to be showing up unless its on AUDUSD with V2 and V3? Correct me if the data has nothing to do with it?
If you have code where you use the price as the divisor and the price is 0.0 then yes you can get a divide by zero error because of the price . . .
 
DomGilberto:


I have "V1-V2-V3" of my EA.

I essentially copied over V1 to a new blank template and changed the part I illustrated above over to (below code) and it gave me a zero divide error during a back-test on AUDUSD. Does the data have anything to do with zero divide? When I run a back-test of all the version of my EA on EURUSD from 2001-2013 (Forex Tester downloaded data and imported in ST), I do not get any errors on any one of my EA's?

Before you go changing the code you need to b able to reproduce the error at will . . . once you can do that then you can investigate it.
 
DomGilberto: Does the data have anything to do with zero divide?
There are no mind readers here. You haven't shown us the code with the divide, so no one here can help you.
 
WHRoeder:
There are no mind readers here. You haven't shown us the code with the divide, so no one here can help you.

All I asked was a simple question, does the data have anything to do with it? If not then I understand more about zero divide from people who have experience with it... Didn't think there were mind readers here...

There is too much code to post every little divide in here. As far as I was concerned I thought I isolated the issue to what I explained above, but apparently not. It's just a little weird how I change one tiny thing and then it doesn't work because I get zero divide (the little thing I changed is what I illustrated in the first post.)

@RaptorUK so essentially it could be an anomaly in the price of the data during my back-test, in which case, on the AUDUSD. Just a little weird that V2 and V3 run about 1/4 of the way through flawlessly and then it all of a sudden hits a zero divide and stops the EA from working all together.

 
DomGilberto:


@RaptorUK so essentially it could be an anomaly in the price of the data during my back-test, in which case, on the AUDUSD. Just a little weird that V2 and V3 run about 1/4 of the way through flawlessly and then it all of a sudden hits a zero divide and stops the EA from working all together.

If you want to spend days sorting this simple issue then by all means feel free . . . . I wouldn't.

If you know when it happens during your back test then it's easy to find . . . start the back test a day before the date when it happens . . . find out exactly, to the minute, when it is going to happen . . . for all the divisions in your code . . . yes, all of them, add a Print() before the line containing the division which prints the divisor and a reference to the line of code in question . . .

For example:

if(d == 0.0) Print("a = c / d - divisor d is " + d );

a = c / d;

When your code terminates with the divide by zero error check the log file and in the last few prints will be the print showing the line of code that produced the error and which variable was set to zero . . .

. . . learn to work smarter and hunt your issues down logically and efficiently.

 
DomGilberto: All I asked was a simple question,
That no one here can answer. You are lazy. You keep posting "will this work" or "what is wrong" but providing nothing of value.
DomGilberto:
Hey, you're right, sorry for being vague!

And you don't learn - You've been repeatedly asked for ALL the (relevant) code and your variable values. Why are we still having to ask 21 posts later? Put the print statements in your code and get some information like what and were.

Then if you still can't fix your problem, ask.

Reason: