Zero Divide Error

 

As I compile my include file, no errors appear. However, when I run my script, it alerts me "zero divide" in the include file. Unfortunately I could narrow down the source of the problem. I have no idea how to solve this error. I'd appreciate anyone who could assist me.


int CandleStickPips = 20; // Proper function reqired
   int StopLossInPips = CandleStickPips + 1; 
   int TakeProfitInPips = StopLossInPips * 5;
      // double stopLoss = CalculateStopLoss();
  
   int NoMoreTrading = 19;   
   int StartTrading = 8;
   int WickToBodyRequiredRatio = 3;
   
   //--------------------------------------------

   double GetPipValue()
   {
      if(_Digits >= 4)
      {
         return 0.0001;
      }
      else
      {
         return 0.01;
      }
   }

   //--------------------------------------------

   bool TradingTime()
      {
         if(Hour() > StartTrading && Hour() < NoMoreTrading)
         {
            return true;
         }    
         else
         {
            return false;
         }
      }
      
   bool Weekday()
    {
      if(DayOfWeek() > 5)
      {
         return false;
      }    
      else
      {
         return true;
      }
   }
   
   bool IsTradingAllowed()
   {
      if(Weekday() == true && TradingTime() == true)
      {
         return true;
      }    
      else
      {
         return false;
      }
   }
   
   //--------------------------------------------
   
   double CalculateStopLoss(bool LongPosition, double entryPrice, int pips)
   {
      double stopLoss;
      if(LongPosition)
      {
         stopLoss = entryPrice - (StopLossInPips * GetPipValue());
      }
      else
      {
         stopLoss = entryPrice + (StopLossInPips * GetPipValue());
      }
      
      return stopLoss;
   }
   
   
   double CalculateTakeProfit(bool LongPosition, double entryPrice, int pips)
   {
      double takeProfit;
      if(LongPosition)
      {
         takeProfit = entryPrice + (TakeProfitInPips * GetPipValue());
      }
      else
      {
         takeProfit = entryPrice - (TakeProfitInPips * GetPipValue());
      }
      
      return takeProfit;
   }
   
   
   double GetStopLossPrice(bool LongPosition, double entryPrice, int maxLossInPips)
   {
      double stopLossPrice;
      if (LongPosition)
      {
         stopLossPrice = entryPrice - (maxLossInPips * 0.0001);
      }
      else
      {
         stopLossPrice = entryPrice + (maxLossInPips * 0.0001);
      }
      
      return stopLossPrice;
   }
   
   
     //--------------------------------------------
     
     
     bool BullishCandlestick()
  {
      if(iOpen("NULL", PERIOD_H1, 1) < iClose("NULL", PERIOD_H1, 1))
      {
         return true;
      }    
      else
      {
         return false;
      }
   }
  
  
     bool CalculateUpperWick()
  {
      if(BullishCandlestick() == true)
      {   
         UpperWick = (iHigh("EURCHF", PERIOD_H1, 1) - iClose("EURCHF", PERIOD_H1, 1));
       } 
      else
      {
         UpperWick = (iHigh("EURCHF", PERIOD_H1, 1) - iOpen("EURCHF", PERIOD_H1, 1));
      }
      
      return UpperWick;
   }
  
     bool CalculateLowerWick()
  {
      if(BullishCandlestick() == true)
      {   
         UpperWick = (iOpen("EURCHF", PERIOD_H1, 1) - iLow("EURCHF", PERIOD_H1, 1));
       } 
      else
      {
         UpperWick = (iOpen("EURCHF", PERIOD_H1, 1) - iClose("EURCHF", PERIOD_H1, 1));
      }
      
      return LowerWick;
  }
   
   //--------------------------------------------
  
  
   double UpperWick;
   double UpperWickinPips = UpperWick / (GetPipValue()); 
   double UpperWickinPips2 = NormalizeDouble(UpperWickinPips, 2); 
         
   double LowerWick; 
   double LowerWickinPips = LowerWick / (GetPipValue()); 
   double LowerWickinPips2 = NormalizeDouble(LowerWickinPips, 2); 
   
   double BodyOpen = iOpen("EURCHF", PERIOD_H1, 8);// Value of Most Recent Candlestick's Open
   double BodyClose = iClose("EURCHF", PERIOD_H1, 8);// Value of Most Recent Candlestick's Close
   double BodyinPips = (MathAbs(BodyOpen - BodyClose)) / (GetPipValue()); // Size of Candlestick Body in Pips
   double BodyinPips2dp = NormalizeDouble(BodyinPips, 2);// Size of Candlestick Body in Pips 2dp
  
   double PrevBodyOpen = iOpen("EURCHF", PERIOD_H1, 9);// Value of Previous Candlestick's Open
   double PrevBodyClose = iClose("EURCHF", PERIOD_H1, 9);;// Value of Previous Candlestick's Close
   double PrevBodyinPips = (MathAbs(PrevBodyOpen - PrevBodyClose)) / (GetPipValue());// Size of Previous Candlestick Body in Pips
   double PrevBodyinPips2dp = NormalizeDouble(PrevBodyinPips, 2);// Size of Previous Candlestick Body in Pips 2dp
   
   double DojiWickRatio = LowerWickinPips2 / UpperWickinPips2;
 
 
   //--------------------------------------------


  bool Doji()
   {
      if(0.85 < DojiWickRatio < 1.15)
      {
         return true;
      }    
      else
      {
         return false;
      }
   }     
    
  bool BullishHammer()
   {
      if((LowerWickinPips2 >= (BodyinPips2dp * WickToBodyRequiredRatio))&& (UpperWickinPips2 <= (LowerWickinPips2 / 3)))
      {
         return true;
      }    
      else
      {
         return false;
      }
   }     
   
   bool BearishShootingStar()
   {
      if((UpperWickinPips2 >= (BodyinPips2dp * WickToBodyRequiredRatio)) && (LowerWickinPips2 <= (UpperWickinPips2 / 3)))
      {
         return true;
      }    
      else
      {
         return false;
      }
   }  
  
  bool BullishEngulfingPattern()
   {
      if((BodyinPips2dp > PrevBodyinPips2dp) && (BodyClose > PrevBodyOpen) && (BodyinPips2dp > 5) && (PrevBodyinPips2dp > 5))
      {
         return true;
      }    
      else
      {
         return false;
      }
   }
  
  bool BearishEngulfingPattern()
   {
      if(BodyinPips2dp > PrevBodyinPips2dp && BodyClose < PrevBodyOpen && BodyinPips2dp > 5 && PrevBodyinPips2dp > 5)
      {
         return true;
      }    
      else
      {
         return false;
      }
   }
   
 
luxecapital:

As I compile my include file, no errors appear. However, when I run my script, it alerts me "zero divide" in the include file. Unfortunately I could narrow down the source of the problem. I have no idea how to solve this error. I'd appreciate anyone who could assist me.


double UpperWick;
   double UpperWickinPips = UpperWick / (GetPipValue()); 
   double UpperWickinPips2 = NormalizeDouble(UpperWickinPips, 2); 
         
   double LowerWick; 
   double LowerWickinPips = LowerWick / (GetPipValue()); 
   double LowerWickinPips2 = NormalizeDouble(LowerWickinPips, 2); 
   
   double BodyOpen = iOpen("EURCHF", PERIOD_H1, 8);// Value of Most Recent Candlestick's Open
   double BodyClose = iClose("EURCHF", PERIOD_H1, 8);// Value of Most Recent Candlestick's Close
   double BodyinPips = (MathAbs(BodyOpen - BodyClose)) / (GetPipValue()); // Size of Candlestick Body in Pips
   double BodyinPips2dp = NormalizeDouble(BodyinPips, 2);// Size of Candlestick Body in Pips 2dp
  
   double PrevBodyOpen = iOpen("EURCHF", PERIOD_H1, 9);// Value of Previous Candlestick's Open
   double PrevBodyClose = iClose("EURCHF", PERIOD_H1, 9);;// Value of Previous Candlestick's Close
   double PrevBodyinPips = (MathAbs(PrevBodyOpen - PrevBodyClose)) / (GetPipValue());// Size of Previous Candlestick Body in Pips
   double PrevBodyinPips2dp = NormalizeDouble(PrevBodyinPips, 2);// Size of Previous Candlestick Body in Pips 2dp
   
   double DojiWickRatio = LowerWickinPips2 / UpperWickinPips2;
 

It's not that hard to find the dividers.....  now use the debugger to check the values and adjust your code to control them.

double UpperWick;
   double UpperWickinPips = UpperWick / (GetPipValue()); 
given that you assign no value to UpperWick you should start there
 
  1. Those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

      Don't try to use any price or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)