zero divide causing errors in EA

 


This is the code thats returning the zero divide problem . Which occurs when I change the lot size from MODE_LOTSIZE  to MODE_MINLOT.


double optimalLotSize = NormalizeDouble(maxLossInQuoteCurr /(maxLossInPips * GetPipValue())/lotSize,2);

  

  return optimalLotSize;


-

-

-

 

double lotSize = MarketInfo(NULL,MODE_MINLOT);

  Print("lotSize: " + lotSize);

 
Alan Juarez:


This is the code thats returning the zero divide problem . Which occurs when I change the lot size from MODE_LOTSIZE  to MODE_MINLOT.


double optimalLotSize = NormalizeDouble(maxLossInQuoteCurr /(maxLossInPips * GetPipValue())/lotSize,2);

  

  return optimalLotSize;


-

-

-

 

double lotSize = MarketInfo(NULL,MODE_MINLOT);

  Print("lotSize: " + lotSize);

use some code to calculate the divide elements first, check they are not zero then perform the divide...

 
Alan Juarez:


This is the code thats returning the zero divide problem . Which occurs when I change the lot size from MODE_LOTSIZE  to MODE_MINLOT.


double optimalLotSize = NormalizeDouble(maxLossInQuoteCurr /(maxLossInPips * GetPipValue())/lotSize,2);

  

  return optimalLotSize;


-

-

-

 

double lotSize = MarketInfo(NULL,MODE_MINLOT);

  Print("lotSize: " + lotSize);

use a simple function like this and use in your code when you divide

//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);
 }
 
Kenneth Parling:

use a simple function like this and use in your code when you divide

won't work in MT5


you need to use :

if (fabs(v) > 0)
 
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Alan Juarez: double lotSize = MarketInfo(NULL,MODE_MINLOT);
    Don't use NULL.
    • You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not. OrderSend does not.
    • Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    • Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    • MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
 
Jean Francois Le Bas:

won't work in MT5


you need to use :

i was referring to MT4