Zero divide in LotSize calculation

 

Hi there I have this code working fine on the simple version of my advisor:

   //------------------------------------------- Money management ---!
   double Tick=MarketInfo(_Symbol,MODE_TICKVALUE);
   double minLot=MarketInfo(_Symbol,MODE_MINLOT),maxLot=MarketInfo(_Symbol,MODE_MAXLOT);
   double LotSize=NormalizeDouble(((RiskFactor/100)*AccountBalance()/((ATR/3)/_Point))/Tick,2);
   if(LotSize<minLot){LotSize=minLot;};if(LotSize>maxLot){LotSize=maxLot;}

But it's giving a zero divide error in the multipair version:

      //---------------------------------------- Money management ---!
      double minLot=MarketInfo(SName,MODE_MINLOT),maxLot=MarketInfo(SName,MODE_MAXLOT);
      double SPoint=MarketInfo(SName,MODE_POINT),STick=MarketInfo(SName,MODE_TICKVALUE);
      double LotSize=NormalizeDouble(((RiskFactor/100)*AccountBalance()/((ATR/3)/SPoint))/STick,2);
      if(LotSize<minLot){LotSize=minLot;};if(LotSize>maxLot){LotSize=maxLot;}

The same code is also working in the MQ5 multipair version:

      double Balance=AccountInfoDouble(ACCOUNT_BALANCE);
      double SPoint=SymbolInfoDouble(SName,SYMBOL_POINT);
      double minLot=SymbolInfoDouble(SName,SYMBOL_VOLUME_MIN);
      double maxLot=SymbolInfoDouble(SName,SYMBOL_VOLUME_MAX);
      double STick=SymbolInfoDouble(SName,SYMBOL_TRADE_TICK_VALUE);
      double LotSize=NormalizeDouble(((RiskFactor/100)*Balance/((ATR[0]/3)/SPoint))/STick,2);
      if(LotSize<minLot){LotSize=minLot;};if(LotSize>maxLot){LotSize=maxLot;}

Anyone knows what's the mistake on that code?

The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks, as a small error in the expert or indicator logic can cause losses on the trading account. That is why we have developed a series of basic checks to ensure the required quality level of the Market products. If any errors are identified by the Market...
 
David Diez: Anyone knows what's the mistake on that code?

yes.
     How To Ask Questions The Smart Way. 2004
          Only ask questions with yes/no answers if you want "yes" or "no" as the answer.

But since you were argumentative (twice) and (five times) I won't be helping.

 
David Diez:

Hi there I have this code working fine on the simple version of my advisor:

But it's giving a zero divide error in the multipair version:

The same code is also working in the MQ5 multipair version:

Anyone knows what's the mistake on that code?

Hi David,

It is probably because the MarketInfo function gives you zero.

Try to print SPoint and STick variables.

The error generates when the market is off (weekend)?

Jan.

 
JAN OPOCENSKY:

Hi David,

It is probably because the MarketInfo function gives you zero.

Try to print SPoint and STick variables.

The error generates when the market is off (weekend)?

Jan.

Hi there Jan, thank you, I forgot to Print values. This is the result:

2020.07.26 22:15:16.334 2017.07.03 03:00:40  (Advisor) EURUSD,M1: SPoint: 1e-05, STick: 0.8580229435335102

Now am trying to get this value by NormalizeDouble with symbol digits:

int SDigits=(int)MarketInfo(SName,MODE_DIGITS);
double SPoint=NormalizeDouble(MarketInfo(SName,MODE_POINT),SDigits);
double STick=NormalizeDouble(MarketInfo(SName,MODE_TICKVALUE),SDigits);

2020.07.26 22:23:46.725 2017.07.05 02:55:00  (Advisor) EURUSD,M1: SDigits: 5, SPoint: 1e-05, STick: 0.85802

It's not working... there is also some point where tickvalue is zero (zero divide):

2020.07.26 22:29:40.714 2017.07.06 05:42:50  (Advisor) EURUSD,M1: SDigits: 5, SPoint: 1e-05, STick: 0.0


 
David Diez:

Hi there Jan, thank you, I forgot to Print values. This is the result:

2020.07.26 22:15:16.334 2017.07.03 03:00:40  (Advisor) EURUSD,M1: SPoint: 1e-05, STick: 0.8580229435335102

Now am trying to get this value by NormalizeDouble with symbol digits:

2020.07.26 22:23:46.725 2017.07.05 02:55:00  (Advisor) EURUSD,M1: SDigits: 5, SPoint: 1e-05, STick: 0.85802

It's not working... there is also some point where tickvalue is zero (zero divide):

2020.07.26 22:29:40.714 2017.07.06 05:42:50  (Advisor) EURUSD,M1: SDigits: 5, SPoint: 1e-05, STick: 0.0


Hello,

try to check ATR value.

If return 0.0, then the problem is in ATR calculate method.

 
Nikolaos Pantzos:

Hello,

try to check ATR value.

If return 0.0, then the problem is in ATR calculate method.

ATR is ok, the flaw might be between SPoint which its strange value and STick which sometimes it's giving 0.0. This is not happening on chart symbol version.

 

These things happen all the time so you have to detect them before failure in your code.

You can always add something like a check before dividing the value.

if(SPoint != 0)
  {
   // go on....
  }
 
Marco vd Heijden:

These things happen all the time so you have to detect them before failure in your code.

You can always add something like a check before dividing the value.

I've ensured to get a higher value than zero for TickValue(STick):

      double SPoint=MarketInfo(SName,MODE_POINT),STick=MarketInfo(SName,MODE_TICKVALUE);
      double LotSize=0;if(STick!=0){NormalizeDouble(((RiskFactor/100)*AccountBalance()/((ATR/3)/SPoint))/STick,2);}

And now the advisor is running... but it's working 0.01 lots everytime, when it might be around 0.08 for 1000 eur. capital.

2020.07.27 13:59:05.733 2019.05.15 04:24:06  (Advisor) EURUSD,M1: open #284 sell 0.01 EURUSD at 1.12050 sl: 1.12216 tp: 1.11801 ok

So still it's not properly calculating the two percent riskfactor  to make lotsizing. I'm just thinking that MetaTrader4 does not work properly on every case.

Reason: