How to solve zero divide error in my code?

 

Hello guys,


I am running into a zero divide error because my formula is based on the percentage change from the lowest/highest the last x bars and due to the nature of this it gives me a zero divide error. I am not sure how to solve this, if anybody could provide some insight it would be much appreciated. 


My code is below.


int TicketBuy;
int Ticketsell;
int magic = 35422;

void OnTick()
  {

   double Lowest = iLowest(Symbol(),0,MODE_LOW,7,0);
   double Highest = iHighest(Symbol(),0,MODE_HIGH,7,0);
   double CurrentpriceBuy = Ask;
   double CurrentpriceSell = Bid;
   double PercentchangeBuy = (CurrentpriceBuy - Lowest)/Lowest;      // This causes the error
   double PercentchangeSell = (Highest - CurrentpriceSell)/Highest;  // This causes the error
   
   if(getBuyOrderCount(Symbol(),magic)<1){
   if(PercentchangeBuy > 0.01){
   TicketBuy = OrderSend(Symbol(),OP_BUY,2,Ask,10,0,0,NULL,magic,0,clrAliceBlue);
   }}
   
   
   if(OrderProfit()>100){
   OrderClose(TicketBuy,2,Bid,0,clrAliceBlue);
   }
   
   if(SellOrderCount(Symbol(),magic)<1){
   if(PercentchangeSell > (0.01) ){
   Ticketsell = OrderSend(Symbol(),OP_SELL,2,Bid,10,0,0,NULL,magic,0,clrAqua);
   }
   }
   
   if(OrderSelect(Ticketsell,SELECT_BY_TICKET,MODE_TRADES) == True){
   if(OrderProfit()>100){
   OrderClose(OrderTicket(),OrderLots(),OrderOpenPrice(),0,clrAliceBlue);
   }}
   
  }
 
Change this:

   double PercentchangeBuy = (CurrentpriceBuy - Lowest)/(Lowest+DBL_MIN);      // This causes the error
   double PercentchangeSell = (Highest - CurrentpriceSell)/(Highest+DBL_MIN);  // This causes the error
   
 
Rahul Shaji Parmeshwar:

Hello guys,


I am running into a zero divide error because my formula is based on the percentage change from the lowest/highest the last x bars and due to the nature of this it gives me a zero divide error. I am not sure how to solve this, if anybody could provide some insight it would be much appreciated. 


My code is below.


use this sample below

//Use the function like this in your code when you divide

//Example of usage; Division(xxx,xxx);

//----
double Division(double numerator,double denominator)
 {
  if(denominator == 0)
     return(0);
   else
     return(numerator/denominator);
 }
 
Your solution seems better.

Although both are only patches to an error, that's not even related to the effect and patch...

It should be considered, querying data and checking for errors before continuing to process the results.

Even though I am personally the opinion div/zero is undefined, division by DBL_MIN is closer to what would be a valid result than returning zero as result. At least from an data processing algorithmic standpoint.
 
Ahmet Metin Yilmaz #:

use this sample below

nice workaround.

 

Also, you may cast all the integer divisions (unsafe) in your code to floating-point divisions (safe).

void OnStart()
  {
   int a = 1;
   int b = 0;
   Print( a / b );          // zero divide error
   Print( a / (double)b );  // the result is infinity
  }


Or use division through a function

'

double safeDiv(double x, double y)
  {
   return (y != 0) ? x / y : (double)"nan";
  }
 

While everybody is providing to solutions to "disable the division by 0", a better alternative is for the programmer
to find out why it's dividing by zero and prevent it, because this to me looks like an edge case/scenario that could affect
the strategy/script and should not happen at all.

 
Yes, true. It's a fake patch. And not a good one either.

Someone else had similar issue, I stated exactly that...
 
Alexandre Borela #:

While everybody is providing to solutions to "disable the division by 0", a better alternative is for the programmer
to find out why it's dividing by zero and prevent it, because this to me looks like an edge case/scenario that could affect
the strategy/script and should not happen at all.

most of us arent mathmaticians, so workarounds and patches are what we are stuck with, so if you can show a more reliable patch to either of the above, then please post it for us to see. Or post links where we can invent better coding/matmatical skills better suited for coding scenarios.

 
Revo Trades #:

most of us arent mathmaticians, so workarounds and patches are what we are stuck with, so if you can show a more reliable patch to either of the above, then please post it for us to see. Or post links where we can invent better coding/matmatical skills better suited for coding scenarios.

There's no need for complex formulas, all OP needs is to learn to use the debugger, find out why at one point the denominator of one of the divisions
is becoming zero and see if that could affect the logic, and IF it doesn't a simple MathMax, if condition or one of the patches posted here would solve it.

My issue is with the blind patching, OP doesn't know why something is becoming zero and how that is affecting the state of the entire script.
 
Rahul Shaji Parmeshwar:


I am running into a zero divide error because my formula is based on the percentage change from the lowest/highest the last x bars and due to the nature of this it gives me a zero divide error. I am not sure how to solve this, if anybody could provide some insight it would be much appreciated. 


  Your code below is wrong and maybe returning 0 as the correct answer - iLowest/iHighest return an Integer index value not a double price.

  Learn to check the proper use of called functions and use the returned values appropriately.

  Make sure the terminal has the amount of data availabe that you are querying before you query it.

  Catch potential zero divides before they happen.


  double Lowest = iLowest(Symbol(),0,MODE_LOW,7,0);
  double Highest = iHighest(Symbol(),0,MODE_HIGH,7,0);
Reason: