zero divide error

 

I have a zero divide coming up in my experts journal.

Any obvious errors in the code below?


//+------------------------------------------------------------------+
//|                Copyright © 2009, SanMiguel, ForexFactory Forums. |
//|                                        http://www.sidekicker.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, SanMiguel, ForexFactory Forums."
#property link      "http://www.sidekicker.net"

#include <WinUser32.mqh>
#include <stderror.mqh>
#include <stdlib.mqh>

/*
1.0.0    Created
*/

int start()
{

   double MovetoBreakEven;
   double Buffer;
   int nDigits;
   double  varHigh = iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, 0);
   double  varLow = iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_LOW, 0);
   double StopLoss = (varHigh - varLow)/2;
   
   if (GlobalVariableGet(Symbol()+"BiasDirection") == 0) {return(0);}

   int total = OrdersTotal();
   for(int i=total-1;i>=0;i--)  
   {
      if( OrderSelect(i,SELECT_BY_POS) && OrderCloseTime()==0)
      {
            // ----------------------------------------
            // ----- 1 -----
            // Create orders 
            // To be done manually for the moment
            // ----------------------------------------
   
               
            // ----------------------------------------
            // ----- 2 -----
            // Manage existing orders 
            // (a) move pending orders 
            if (  OrderType()==OP_BUYLIMIT && OrderSymbol() == Symbol()) {
               OrderModify(OrderTicket(), varLow, varLow-StopLoss, varHigh, 0);
            }   
            
            if (  OrderType()==OP_SELLLIMIT && OrderSymbol() == Symbol()) {
               OrderModify(OrderTicket(), varHigh, varHigh+StopLoss, varLow, 0);
            } 
            
            // (b) move TP on open orders 
            // ----------------------------------------
            if (  OrderType()==OP_BUY && OrderSymbol() == Symbol() &&
                  OrderTakeProfit()!= varHigh )
            {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),varHigh,0);
            }   
            
            if (  OrderType()==OP_SELL && OrderSymbol() == Symbol()
                  && OrderTakeProfit()!= varLow )
            {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),varLow,0);
            } 
         
            
            // ----------------------------------------
            // ----- 3 -----
            // ----------------------------------------
            // if bias has changed and wrong orde ris in delete it
            // I will do this manually for the moment
       }
   }
            
}
 

Only division line in there:

double StopLoss = (varHigh - varLow)/2;

You need to be prepared for the high and low being the same val, which results in a 0 after the subtraction.

 
circlesquares:

Only division line in there:

double StopLoss = (varHigh - varLow)/2;

You need to be prepared for the high and low being the same val, which results in a 0 after the subtraction.


The high and low should never be the same value though. One is an MA applied to the high and one to the low of the weekly candle.
 
SanMiguel:
The high and low should never be the same value though. One is an MA applied to the high and one to the low of the weekly candle.
...And the code wouldn't generate an error even if they were the same value. 0/2 isn't division by zero, and won't trigger an error. It looks as though the problem must be in an area of the code which isn't included in your original post. Can't see anything in there which could lead to division by zero.
 
jjc:
...And the code wouldn't generate an error even if they were the same value. 0/2 isn't division by zero, and won't trigger an error. It looks as though the problem must be in an area of the code which isn't included in your original post. Can't see anything in there which could lead to division by zero.

I just realised it was the indicator error not the EA.

This is the code, it must be the averaging I use:

//+------------------------------------------------------------------+
//|                                     Weekly 2LWMAs TP SL Diff.mq4 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window

int scaleX=5,
    scaleY=5,
    offsetX=0,
    offsetY=0,
    fontSize=10,
    corner=2,
    symbolCodeBuy=233, // a symbol code for a buy signal
    symbolCodeSell=234, // sell signal
    symbolCodeNoSignal=183; // no signal
           
extern color signalBuyColor=Green, // color of the symbol of a buy signal
             signalSellColor=Tomato, // for a sell signal
             noSignalColor=Yellow, // no signal
             textColor=Black; // color of all writings
             
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   
   double  varHigh = iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, 0);
   double  varLow = iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_LOW, 0);
   
   ObjectCreate("signal",OBJ_LABEL,0,0,0,0,0);
   ObjectSet("signal",OBJPROP_CORNER,corner);
   ObjectSet("signal",OBJPROP_XDISTANCE,scaleX+offsetX);
   ObjectSet("signal",OBJPROP_YDISTANCE,scaleY+offsetY);
   ObjectSetText("signal",CharToStr(symbolCodeNoSignal),fontSize,"Wingdings",noSignalColor);
   
   //1 = up
   //0 = neutral
   //-1 = down
   
   if (GlobalVariableGet(Symbol()+"BiasDirection") == 1) {
      ObjectSetText("signal",CharToStr(symbolCodeBuy),fontSize,"Wingdings",signalBuyColor);
   }
   if (GlobalVariableGet(Symbol()+"BiasDirection") == 0) {
      ObjectSetText("signal",CharToStr(symbolCodeNoSignal),fontSize,"Wingdings",noSignalColor);
   }
   if (GlobalVariableGet(Symbol()+"BiasDirection") == -1) {
      ObjectSetText("signal",CharToStr(symbolCodeSell),fontSize,"Wingdings",signalSellColor);
   }
   
   //Average move beyond the LWMAs
   int i,                           
       Counted_bars;                

   Counted_bars=IndicatorCounted(); 
   i=Bars-Counted_bars-1;           
   double varPriceExceededHigh=0;
   double varPriceExceededLow=0;
   double varPriceExceededHighCount=0; 
   double varPriceExceededLowCount=0;
   double bufferHigh[];
   double bufferLow[]; 

   while(i>=0)                      
   {
      for (int j=19; j>=0; j--) {

         if (High[i+j] > iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, i+j)) {
            //count this bar towards the total and add it to the var
            varPriceExceededHighCount++;
            varPriceExceededHigh= varPriceExceededHigh + (High[i+j] - iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, i+j));
         }
         if (Low[i+j] <  iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_LOW, i+j)) {
            varPriceExceededLowCount++;
            varPriceExceededLow= varPriceExceededLow + (iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_LOW, i+j) - Low[i+j]);
         }
      } // for

      //calculate averages
      double varavHigh = varPriceExceededHigh/varPriceExceededHighCount;
      bufferHigh[i] = varavHigh;
      double varavLow = varPriceExceededLow/varPriceExceededLowCount;
      bufferLow[i] = varavLow;
      i--;                          
   } // while

   Comment (
               "Difference: " + DoubleToStr(varHigh - varLow, 5) +
               "\nHigh: " + DoubleToStr(varHigh, 5) +
               "\nLow: " + DoubleToStr(varLow, 5) +
               "\nSL: " + DoubleToStr((varHigh - varLow)/2, 5) +
               "\nTrade size: " + DoubleToStr(AccountBalance()*0.0001, 2) +
               "\nAv. above high: " + DoubleToStr(varavHigh, 5) +
               "\nAv. below low: " + DoubleToStr(varavLow, 5)
   );

   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
SanMiguel:
This is the code, it must be the averaging I use:
Yes. Both varPriceExceededHighCount and varPriceExceededLowCount are obviously capable of being zero if the high/low doesn't exceed the average during the last 20 bars.
 

SanMiguel you should build code to have a default value in the event that a divide by zero is analytically possible, something along the lines of the following:

//calculate averages
double varavHigh = 9999.;       // Assume a default value that is meaningful and relevant to the author

if (varPriceExceededHighCount!=0) varavHigh = varPriceExceededHigh/varPriceExceededHighCount;  //  Only perform the division if the denominator is non-zero

bufferHigh[i] = varavHigh;     // bufferHigh[i] will equal 9999 (or whatever value the author elects to implement) if varPriceExceededHighCount == 0
 
1005phillip:

SanMiguel you should build code to have a default value in the event that a divide by zero is analytically possible, something along the lines of the following:


Shouldn't this part of my code in the loop oprder make it analytically impossible?

varPriceExceededHighCount++;

Reason: