Zero Divide Error on Include File

 

Hi All,


Looking for a little help as I've spent the previous two days going through the zero divide error threads in the forum and can't quite find a fix to my issue.


Basically, my EA runs fine for several trades, but then I get a critical error closing the EA. It is a Zero divide error and it gives me the heads up that it is an issue in my include file and points me to the issue attached below. I've marked the line it seems to be struggling with. I have noticed, that it seems to be getting the Zero Divide Error when it is due to close, and what looks like open an opposite trade at the same time, I didn't know if that would help someone make sense of it.


I'm new to this and trying my best to learn, so would greatly appreciate anyone that can help unstick me from this issue!


Thanks


double OptimalLotSize(double maxRiskPrc, int maxLossInPips)
{

  double accEquity = AccountEquity();
  Print("accEquity: " + accEquity);
  
  double lotSize = MarketInfo(NULL,MODE_LOTSIZE);
  Print("lotSize: " + lotSize);
  
  double tickValue = MarketInfo(NULL,MODE_TICKVALUE);
  
  if(Digits <= 3)
  {
   tickValue = tickValue / 100;
  }
  
  Print("tickValue: " + tickValue);
  
  double maxLossDollar = accEquity * maxRiskPrc;
  Print("maxLossDollar: " + maxLossDollar);
  
  double maxLossInQuoteCurr = maxLossDollar / tickValue;
  Print("maxLossInQuoteCurr: " + maxLossInQuoteCurr);
  
  double optimalLotSize = NormalizeDouble(maxLossInQuoteCurr / (maxLossInPips * GetPipValue()) / lotSize,Digits); //This line is the issue
  
  return optimalLotSize;
 

}

double OptimalLotSize(double maxRiskPrc, double entryPrice, double stopLoss)
{
   int maxLossInPips = MathAbs(entryPrice - stopLoss) / GetPipValue();
   return OptimalLotSize(maxRiskPrc,maxLossInPips);
}
 

try this

double ZeroDivided(double value) {
   double tempVal = 1.0;
   if(value!=0) tempVal = value;
   return tempVal;
}
Market_TickValue  	= SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
Market_TickSize		= SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE)/_Point;
double deltaLot = ZeroDivided(Market_TickValue/double(Market_TickSize));
 

This error is occouring because your lot size is way smaller then your MIN_LOTSIZE.

First find out what you minimun lot size, then check if your return value of this function is above minimum lot. **If not. then increase your Risk percentage.

( Try to do these calculations on paper , you will find the error instantly .

Hope this will help you.

 
assfianazhar #:

This error is occouring because your lot size is way smaller then your MIN_LOTSIZE.

First find out what you minimun lot size, then check if your return value of this function is above minimum lot. **If not. then increase your Risk percentage.

( Try to do these calculations on paper , you will find the error instantly .

Hope this will help you.

Thanks for that tip assfianazhar. I was struggling with that issue too. I increased the Risk percentage thereby increasing lot size and it worked. Thanks again.

 

Just a few pointers here..

Initialize 

GetPipValue()

Follow BIDMAS

maxLossInQuoteCurr / (maxLossInPips * GetPipValue()) / lotSize,Digits

Whether that be:

((maxLossInQuoteCurr / (maxLossInPips * GetPipValue())) / lotSize,Digits)

or 

(maxLossInQuoteCurr / ((maxLossInPips * GetPipValue()) / lotSize,Digits))
 
rcartertrading:

Hi All,


Looking for a little help as I've spent the previous two days going through the zero divide error threads in the forum and can't quite find a fix to my issue.


Basically, my EA runs fine for several trades, but then I get a critical error closing the EA. It is a Zero divide error and it gives me the heads up that it is an issue in my include file and points me to the issue attached below. I've marked the line it seems to be struggling with. I have noticed, that it seems to be getting the Zero Divide Error when it is due to close, and what looks like open an opposite trade at the same time, I didn't know if that would help someone make sense of it.


I'm new to this and trying my best to learn, so would greatly appreciate anyone that can help unstick me from this issue!


Thanks


A lot of info is missing, that could be the cause of the problem.

If the line you said is definitely the one giving the error,

It could be that-

1) GetPipValue() returns 0

2) You are passing 0 as your 'maxLossInPips' input variable

3) MarketInfo(NULL,MODE_LOTSIZE) returns 0 for some reason.


assfianazhar #:

This error is occouring because your lot size is way smaller then your MIN_LOTSIZE.

First find out what you minimun lot size, then check if your return value of this function is above minimum lot. **If not. then increase your Risk percentage.

( Try to do these calculations on paper , you will find the error instantly .

Hope this will help you.

I don't see how it is related to his code and specifically to the line he mentioned is the problem.

Could you please explain mate?

 

MarketInfo can return 0. Most likely, this is theoretically possible for any property.

Once I encountered the fact that MarketInfo(Symbol(), MODE_TICKVALUE) returned 0 once. This was the very first call, apparently the data was still being updated. All subsequent calls returned the correct value.