Help - Zero Divide

 

Hi,

I'm triyng to write my first EA and I've already got the first part of it (entry points).

Unfortunately it gives me various Zero Divide errors in Journal.. :(

I'm around this error for days and getting crazy!!! :(

Can someone please take a look?

//+------------------------------------------------------------------+
//|                                               EXPERT STRUTCH.mq4 |
//|                                                          StRuTcH |
//|                                   http://www.xtb.pt/pt/forum-xtb |
//+------------------------------------------------------------------+
#property copyright "StRuTcH"
#property link      "http://www.xtb.pt/pt/forum-xtb"



// EXTERN PARAMETERS


   extern int PMA_FAST = 5;          // Periodo MA Rápida
   extern int PMA_SLOW = 50;         // Periodo MA lenta
   extern int MA_MODE = 0;           // 0=sma, 1=ema, 2=smma, 3=lwma
   extern int ADXPeriod=14;          // Periodo do ADX
   extern int ADX_START=20;          // Valor do ADX a partir do qual há negócio

   extern int DIST_EXT = 28;         // Periodo para suportes e resistencias dos stops
   extern double Risk_percent = 2;   // Risco máximo (em %) de cada negócio 
   extern double TakeProfit=100;     // TakeProfit definido p/ utilizador
  

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

int start()
  {
//----

int ticket, total;

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

double DIF_ACT_PRIM  = (iMA(NULL,0,PMA_FAST,0,MA_MODE,0,0)-iMA(NULL,0,PMA_SLOW,0,MA_MODE,0,0));

int TREND_PRIMARIA = 0;

if (DIF_ACT_PRIM > 0)        TREND_PRIMARIA = 1; //up
   else
      {
      if(DIF_ACT_PRIM <= 0)  TREND_PRIMARIA = 2; //down
      }

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

int TREND_SECUNDARIA;
   
double DIF_ACT_SEC  = (iMA( NULL,0,PMA_FAST,0,MA_MODE,0,0)-iMA(NULL,0,PMA_SLOW,0,MA_MODE,0,0));
double DIF_PREV_SEC = (iMA( NULL,0,PMA_FAST,0,MA_MODE,0,1)-iMA(NULL,0,PMA_SLOW,0,MA_MODE,0,1));

if(DIF_ACT_SEC > DIF_PREV_SEC)TREND_SECUNDARIA = 1; //up
if(DIF_ACT_SEC < DIF_PREV_SEC)TREND_SECUNDARIA = 2; //down
      
//--------------------------------------------------------------------------------------------------------------------------  

double ADX = iADX(NULL,0,ADXPeriod,PRICE_CLOSE,MODE_MAIN,0);

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

double tickvalue = (MarketInfo(Symbol(),MODE_TICKVALUE))*10;

double STOP_HIGH = NormalizeDouble((iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,DIST_EXT,0))-Bid)/(Point*10),Digits);
double STOP_LOW = NormalizeDouble((Ask - iLow(NULL,0,iLowest(NULL,0,MODE_LOW,DIST_EXT,0)))/(Point*10),Digits);

double RESISTENCIA = NormalizeDouble(iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,DIST_EXT,0)),Digits);
double SUPORTE = NormalizeDouble(iLow(NULL,0,iLowest(NULL,0,MODE_LOW,DIST_EXT,0)),Digits);

double LotsDN = NormalizeDouble((AccountBalance()*Risk_percent/100)/((STOP_HIGH)*tickvalue),1);
double LotsUP = NormalizeDouble((AccountBalance()*Risk_percent/100)/((STOP_LOW)*tickvalue),1); 

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


total = OrdersTotal();


if ( total<1 )
   {

// LONG BUY   
   if ( LotsUP>0.09 && TREND_PRIMARIA==1 && TREND_SECUNDARIA==1 && ADX > ADX_START)
      {
      ticket=OrderSend(Symbol(),OP_BUY,LotsUP,Ask,3,SUPORTE,Ask+TakeProfit*Point*10,"Ordem Longa",12345,0,Green);
      if(ticket>0)
         {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
         Print("ENTRADA LONGA AOS : ",OrderOpenPrice());
         }
         else Print("Error opening BUY order : ",GetLastError());
         return(0);
      }

// SHORT BUY     
   if ( LotsDN>0.09 && TREND_PRIMARIA==2 && TREND_SECUNDARIA==2 && ADX > ADX_START)
      {
      ticket=OrderSend(Symbol(),OP_SELL,LotsDN,Bid,3,RESISTENCIA,Bid-TakeProfit*Point*10,"Ordem Curta",12345,0,Red);
      if(ticket>0)
         {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
         Print("ENTRADA CURTA AOS : ",OrderOpenPrice());
         }
         else Print("Error opening BUY order : ",GetLastError());
         return(0);
      }
      return(0);
   }


//----
   return(0);
  }
//+------------------------------------------------------------------+
 
Wherever you have a Division(/) make sure the expression on the right of the (/) sign cannot equal 0.
 

I only have those divisions in the EA:

double STOP_HIGH = NormalizeDouble((iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,DIST_EXT,0))-Bid) / (Point*10),Digits);
double STOP_LOW = NormalizeDouble((Ask - iLow(NULL,0,iLowest(NULL,0,MODE_LOW,DIST_EXT,0))) / (Point*10),Digits);

double LotsDN = NormalizeDouble((AccountBalance()*Risk_percent/100) / ((STOP_HIGH)*tickvalue),1);
double LotsUP = NormalizeDouble((AccountBalance()*Risk_percent/100) / ((STOP_LOW)*tickvalue),1);

I've tried to understand what is wrong but can't see no mistakes in that!!! :(:(:(

 

Example:

if( STOP_HIGH !=0 && tickvalue !=0 ){

double LotsDN = NormalizeDouble((AccountBalance()*Risk_percent/100) /

((STOP_HIGH)*tickvalue),1);

}

Since you already know all the divisions within the EA. Then Line Comment the OTHERS //--. Or /* ---- */.

And fix them One by One. And test em in Back-tester til you no longer see 0-Divide.

 

I will do that... than post here the results :)

Thanks ubzen

 

.

I've made what you've told me..

I found that the EA is given sometimes the ZeroDivision (not always... just a few separated times) because of the

/ (STOP_HIGH)*tickvalue

division. But I can't understand why!!!

I'll continue to break my head on this rock...

.

 

FOUND THE PROBLEM!!! :) :) :)

Sometimes the:

(iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,DIST_EXT,0))-Bid)

That is part of STOP_HIGH can be zero!!!

LOL.. Tester is always right!!

Thank you very much ubzen!! : )

 
Another happy customer. Welcome :)
Reason: