zero divide

 

hello,


i got an issue with backtesting my ea...

Zero Divide


when i go to my mq4 file and check that line, i turn out to this..:


double BasketTP(string type,string s)
  {
   double PriceSum=0;
   double LotSum=0;

   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         if(OrderSymbol()==s)
           {
            PriceSum+=OrderOpenPrice()*OrderLots();
            LotSum+=OrderLots();
           }
     }
   if(type == "sell")
     {
     AverageTP[GetDevisePos(s)] = NormalizeDouble((PriceSum)/(LotSum), int(MarketInfo(s,MODE_DIGITS) + 0.1)) - 10*GetPips(s);     //+ <<<----------- 
      return(AverageTP[GetDevisePos(s)]);
    
   if(type == "buy")
     {
    AverageTP[GetDevisePos(s)] = NormalizeDouble((PriceSum)/(LotSum), int(MarketInfo(s,MODE_DIGITS) + 0.1)) + 10*GetPips(s);
      return(AverageTP[GetDevisePos(s)]);
     }
   return NormalizeDouble((PriceSum)/(LotSum),Digits);
  }
//+----

where i wrote the next text in the code, is the line 748 where this happens

 <<<-----------



anyone has any idea whats wrong??? 

thanks

 
   double PriceSum=0;
   double LotSum=0;

   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         if(OrderSymbol()==s)
           {
            PriceSum+=OrderOpenPrice()*OrderLots();
            LotSum+=OrderLots();
           }
     }

If there are no open orders with Symbol s, both PriceSum and LotSum will not increase their values from zero.

 
Keith Watford:

If there are no open orders with Symbol s, both PriceSum and LotSum will not increase their values from zero.

i can understand that...

so what do i put so it continues trading / testing in backtest without giving an zero divide?

 
Geoffrey Lemaire: so what do i put so it continues trading / testing in backtest without giving an zero divide?

Check that your LotSum is not zero before dividing by it.

 
William Roeder:

Check that your LotSum is not zero before dividing by it.

so can i put this?


if ((LotSum != 0) && (PriceSum!= 0))
else
  {
            PriceSum+=OrderOpenPrice()*OrderLots();
            LotSum+=OrderLots();
           }
     

or would this not make any sense at all....

 
Geoffrey Lemaire:

so can i put this?



or would this not make any sense at all....

this is not working ...

i tried to put it above 


if(type == "sell")


, but it still get divided by zero... how can i rule out when there are no trades running, that it is not trying to divide by zero... because the errors happens after it close the trades...?

 

The error is pretty obvious isn't it? "ZERO DIVIDE". Everybody know dividing a number with zero will simply return Syntax error even in a calculator.

What you need is to check for the LotSum value to make sure it is not zero. For example


double BasketTP(string type,string s)
  {
   double PriceSum=0;
   double LotSum=0;

   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         if(OrderSymbol()==s)
           {
            PriceSum+=OrderOpenPrice()*OrderLots();
            LotSum+=OrderLots();
           }
     }

   if(LotSum == 0){   //<<<------ add this so it will just return 0 if LotSum is zero.
        return 0;   
   }

   if(type == "sell")
     {
        AverageTP[GetDevisePos(s)] = NormalizeDouble((PriceSum)/(LotSum), int(MarketInfo(s,MODE_DIGITS) + 0.1)) - 10*GetPips(s);     //+ <<<----------- 
        return(AverageTP[GetDevisePos(s)]);
     }

   if(type == "buy")
     {
        AverageTP[GetDevisePos(s)] = NormalizeDouble((PriceSum)/(LotSum), int(MarketInfo(s,MODE_DIGITS) + 0.1)) + 10*GetPips(s);
        return(AverageTP[GetDevisePos(s)]);
     }

   return NormalizeDouble((PriceSum)/(LotSum),Digits);
  }
 
Ahmad Zuhairdi Noh:

The error is pretty obvious isn't it? "ZERO DIVIDE". Everybody know dividing a number with zero will simply return Syntax error even in a calculator.

What you need is to check for the LotSum value to make sure it is not zero. For example


yes, it is indeed obvious sir...  though i did not had zero divide error before i made allot of modifications to my file....


so i assumed the file knew that if there was no trade, then it would not divide by zero...


this is why i was surprised that all of a sudden i have this error...


your solution fixed my error... i am still a leet / newbie with coding, so please my apologies if i make any dumb errors or for dumb questions... though i try to search it myself by googling allot, i don't always find a solution for everything.

i hope u can understand this.


and again thanks allot for this solution.


i appreciate it allot.


And thanks for everyone else to take your time to read and response to my question.

i wish u all a good day.


Sincerely.

 
Geoffrey Lemaire: or would this not make any sense at all....

That “would not make any sense at all.” What part of “Check that your LotSum is not zero before dividing by it,” was unclear to you?

 
Geoffrey Lemaire:

hello,


i got an issue with backtesting my ea...


when i go to my mq4 file and check that line, i turn out to this..:


where i wrote the next text in the code, is the line 748 where this happens



anyone has any idea whats wrong??? 

thanks

 double BasketTP(string type,string s)
  {
   double PriceSum=0;
   double LotSum=0;
   double result=0.0;
   bool ResultOrders=true;
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         if(OrderSymbol()==s && type==OrderType())
           {
            PriceSum+=OrderOpenPrice()*OrderLots();
            LotSum+=OrderLots();
            ResultOrders=false;
           }
     }
     if(ResultOrders)  return(result);
   if(type == "sell" )
     {
     AverageTP[GetDevisePos(s)] = NormalizeDouble((PriceSum)/(LotSum), int(MarketInfo(s,MODE_DIGITS) + 0.1)) - 10*GetPips(s);     //+ <<<----------- 
      return(AverageTP[GetDevisePos(s)]);
    
   if(type == "buy")
     {
    AverageTP[GetDevisePos(s)] = NormalizeDouble((PriceSum)/(LotSum), int(MarketInfo(s,MODE_DIGITS) + 0.1)) + 10*GetPips(s);
      return(AverageTP[GetDevisePos(s)]);
     }
   return NormalizeDouble((PriceSum)/(LotSum),Digits);
  }
Reason: