Download MetaTrader 5
To add comments, please log in or register
Log in to MQL5.community using OpenID. It is convenient!
GTV Holdings, LLC
10122
Adam Perkins 2014.07.23 04:25 

Hello,

 I have an indicator I'm working on that when it is first placed on the chart, the bollinger band values are zero and I end up with a zero divide message in both MT4 and MT5.  After the bollinger band values and the indicator have a chance to calculate, it displays correctly but appears again when i change the chart or timeframe (and sometimes the indicator doesn't display after the change).

 What can I do to avoid the zero divide error?  Is there a wait until loaded function?  Or should I put the division calculation in an if statement to wait until the bollinger values are above zero?

  

Thank you

Stuart Browne
5525
Stuart Browne 2014.07.23 04:45  
Ahhh, the old MetaTrader divide by zero issue (https://www.mql5.com/en/search#!keyword=zero%20divide&module=mql5_module_forum&method=2

There's no "wait until loaded" function that I know of. But the if statement you suggested should fit the bill. Or maybe use IndicatorCounted function?
Alexander Puzanov
28358
Alexander Puzanov 2014.07.23 07:28  

Don't know is this applicable in your case, but universal killer for "zero divide" is increasing of divider by insufficient value. Example:

if(Group3_Bar_3_Body == SIZE_LONGER) {
   if(fabs(Open[i_Pivot_Bar] - Close[i_Pivot_Bar]) / (0.000000001 + High[i_Pivot_Bar] - Low[i_Pivot_Bar]) <= 0.5) continue;
} else if(Group3_Bar_3_Body == SIZE_SHORTER) {
   if(fabs(Open[i_Pivot_Bar] - Close[i_Pivot_Bar]) / (0.000000001 + High[i_Pivot_Bar] - Low[i_Pivot_Bar]) >= 0.5) continue;
}

Data loading - another story..

GTV Holdings, LLC
10122
Adam Perkins 2014.07.24 03:12  

Thanks for the replies.  To close this out for anyone that might search this zero divide situation in the future, it wasn't because of the data not loading before the indicator calculated.  It was because the bollinger bands really were zero value for the 20 bars before they started to calculate.  I added an if statement and that solved the issue.  No more zero divide.

 This screenshot shows the beginning of the chart where the bollinger bands do not calculate until the Moving Average has all the bars it needs.

 

EURUSD, D1, 2014.07.24, Forex Capital Markets, LLC, MetaTrader 5, Demo


Ahmed Soliman
11991
Ahmed Soliman 2014.08.11 06:56  
tfworks.com:

Hello,

 I have an indicator I'm working on that when it is first placed on the chart, the bollinger band values are zero and I end up with a zero divide message in both MT4 and MT5.  After the bollinger band values and the indicator have a chance to calculate, it displays correctly but appears again when i change the chart or timeframe (and sometimes the indicator doesn't display after the change).

 What can I do to avoid the zero divide error?  Is there a wait until loaded function?  Or should I put the division calculation in an if statement to wait until the bollinger values are above zero?

  

Thank you

Simply check if you divide on zero before divide.

for example:

A = B / C

change it to:

if(C>0) A = B / C

Hongliang Liu
1195
Hongliang Liu 2014.08.12 09:11  
tfworks.com:

Hello,

 I have an indicator I'm working on that when it is first placed on the chart, the bollinger band values are zero and I end up with a zero divide message in both MT4 and MT5.  After the bollinger band values and the indicator have a chance to calculate, it displays correctly but appears again when i change the chart or timeframe (and sometimes the indicator doesn't display after the change).

 What can I do to avoid the zero divide error?  Is there a wait until loaded function?  Or should I put the division calculation in an if statement to wait until the bollinger values are above zero?

  

Thank you

the official RSI.mq4 do like:

      if(ExtNegBuffer[InpRSIPeriod]!=0.0)
         ExtRSIBuffer[InpRSIPeriod]=100.0-(100.0/(1.0+ExtPosBuffer[InpRSIPeriod]/ExtNegBuffer[InpRSIPeriod]));
      else
        {
         if(ExtPosBuffer[InpRSIPeriod]!=0.0)
            ExtRSIBuffer[InpRSIPeriod]=100.0;
         else
            ExtRSIBuffer[InpRSIPeriod]=50.0;
        }
Irwan Adnan
808
Irwan Adnan 2014.08.12 09:31  

Also discussed here

https://www.mql5.com/en/forum/32245 

zero divide
zero divide
  • www.mql5.com
I have a question about zero divide in indicator when it run. - - Category: technical indicators
/
To add comments, please log in or register