I keep getting divide by 0 please help

 

Hi community! I have coded this system and i keep having the error of zero divide in 'my code name'

could you please review my coding, what is the problem here?


int magic = 100222;
int OrderID;
double CurrentSpread = NormalizeDouble((Ask-Bid),Digits); 

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
 
//---
double CurrentSpread = NormalizeDouble((Ask-Bid),Digits);
double Ema50 = NormalizeDouble((iMA(NULL,PERIOD_CURRENT,50,0,1,0,0)),Digits);
double PosizeBuy = Posize(0.01,High[1]+CurrentSpread,Ema50-CurrentSpread);
double PosizeSell = Posize(0.01,Low[1]-CurrentSpread,Ema50+CurrentSpread);
double buyprice = NormalizeDouble(( High[1]+CurrentSpread),Digits);
double buystop = NormalizeDouble((Low[1]-CurrentSpread),Digits);
double sellprice = NormalizeDouble((Low[1]-CurrentSpread),Digits);
double sellstop = NormalizeDouble((High[1]+CurrentSpread),Digits);

if (!Checkifopenorders(magic))
{      
   if (Open[2]<Close[2] && High[1]<High[2] && Low[1]>Low[2]&&iMA(NULL,PERIOD_CURRENT,200,0,1,0,0)< Ema50) //BUY-ORDER
      {
      double ProfitBuy = (MathAbs(High[1]-Low[1])*5)+High[1];
      OrderID = OrderSend(NULL,OP_BUYSTOP,PosizeBuy,buyprice,2,buystop,ProfitBuy,"Buy Order",magic,Time[0]+Period()*60);
      Alert ("OrderID" + OrderID);
      if (OrderID<0)
      Alert("Error buy: "+GetLastError());
      }
   
   else if(Open[2]>Close[2] && High[1]<High[2] && Low[1]>Low[2]&&iMA(NULL,PERIOD_CURRENT,200,0,1,0,0)> Ema50) //SELL-ORDER
      {
      double ProfitSell = Low[1]-((High[1]-Low[1])*5);
      OrderID = OrderSend(NULL,OP_SELLSTOP,PosizeSell,sellprice,2,sellstop,ProfitSell,"Sell Order",magic,Time[0]+Period()*60);
      Alert ("OrderID" + OrderID);
      if (OrderID<0)
      Alert("Error sell: "+GetLastError());
      }
    }
    
   int openOrders = OrdersTotal();
   for(int i=0;i<openOrders;i++)
   {
       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)== True)
          {
           if(OrderMagicNumber()== magic && OrderSymbol()== Symbol())
           {
            if(OrderType()== ORDER_TYPE_BUY && Ema50>Close[1] && Close[2]>Ema50 && Close[3]>Ema50 && Close[4]>Ema50 && Close[5]>Ema50)
                  {OrderClose(OrderID,OrderLots(),NormalizeDouble(Bid,Digits),5);}
             if(OrderType()== OP_SELL && Ema50<Close[1] && Close[2]<Ema50 && Close[3]<Ema50 && Close[4]<Ema50 && Close[5]<Ema50)
                  {OrderClose(OrderID,OrderLots(),NormalizeDouble(Ask,Digits),5);}  
             }
         }
   }
      
  }
//+------------------------------------------------------------------+
double TickValueSpread()
{
   if(_Digits>=4)
   {return 0.00001;}
   else
   {return 0.001;}
}

bool Checkifopenorders(int magic)
{
int openOrders = OrdersTotal();
   for(int i=0;i<openOrders;i++)
   {
      if (OrderSelect(i,SELECT_BY_POS) == true)
    {
      if (OrderMagicNumber() == magic && OrderSymbol()==Symbol())
      return true;
    }
   }   
return false;  
}

double Posize(double riskperc,double entryprice,double SL)
{
  
  double risk = AccountEquity()*riskperc;
  double Lotsize = MarketInfo(NULL,MODE_LOTSIZE);
  double tickvalue = MarketInfo(NULL,MODE_TICKVALUE);
  
  if (Digits <=3)
  {tickvalue = tickvalue/100;}
  
  double Slpips = MathAbs((entryprice - SL)/pipsCurrency());
      
  double positionsize = ((risk/tickvalue) / MathAbs(Slpips*pipsCurrency()) / (Lotsize)) ;
    
  double posize = NormalizeDouble(positionsize,2);
  
  return posize;}
  
  double pipsCurrency()
{
   if(_Digits>=4)
   {return 0.0001;}
   else
   {return 0.01;}
}   
 
JonathanPepperstone:

Hi community! I have coded this system and i keep having the error of zero divide in 'my code name'

could you please review my coding, what is the problem here?

Could be helpful if you specify which line produces the error.

I personally check the value of divisor before the divition operation . I do that almost always, unless I am sure that it is imposible that divisor be eual to zero.

 

As far as I can see, This is the only piece in your code that may result as zero :

 double Slpips = MathAbs((entryprice - SL)/pipsCurrency());

You should make sure that entryprice and SL are not equal.

Because it is going to be the divisor in this line:

double positionsize = ((risk/tickvalue) / MathAbs(Slpips*pipsCurrency()) / (Lotsize)) ;



Also, you do not need to calculate the spread, you can just call SymbolInfoInteger using SYMBOL_SPREAD.

SymbolInfoInteger - Market Info - MQL4 Reference
SymbolInfoInteger - Market Info - MQL4 Reference
  • docs.mql4.com
SymbolInfoInteger - Market Info - MQL4 Reference
Reason: