Download MetaTrader 5

zero divide

To add comments, please log in or register
Irwan Adnan
927
Irwan Adnan  

Hi,

I have a question about zero divide in indicator when it run. What is wrong. Should we use a trick to fix it? Thanks.

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   double temp= 1/MarketInfo(Symbol(),MODE_MARGINREQUIRED); //<==== it is zero divide 
   Print(temp);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Alain Verleyen
Moderator
33593
Alain Verleyen  
belido:

Hi,

I have a question about zero divide in indicator when it run. What is wrong. Should we use a trick to fix it? Thanks.

A trick ? Yes, don't divide by zero.
Простак
315
Простак  
belido:

Hi,

I have a question about zero divide in indicator when it run. What is wrong. Should we use a trick to fix it? Thanks.

Function MarketInfo() is wrong. According to the documentation it MUST return a particular kind of "various data about securities listed in the "Market Watch" window" and in the particular case it is "Margin to maintain open orders calculated for 1 lot", which cannot be 0. Nevertheless, the function returns 0. It is wrong.

The trick you might use is first to keep the value returned by the function in an additional variable and then check its value for 0. If it is 0, an error should be signaled somehow instead of performing division. Otherwise, division is to be performed as usual.

Простак
315
Простак  
angevoyageur:
A trick ? Yes, don't divide by zero.
Sorry, there is NO division by zero in the code. Consult the documentation, please.
Irwan Adnan
927
Irwan Adnan  

Thanks all for the respons. Let me make more clear. 

MarketInfo(Symbol(),MODE_MARGINREQUIRED) does have non zero value. 

But when we run MT4 with that indicator in it, it has zero divide error. 

Then if we compile the indicator, it works.

The question is why it has to compile every we start MT4.

PS : that formula works well if we put in EA

 What it looks like at terminal

Eric Venturi-Bloxs
71360
Eric Venturi-Bloxs  

If the symbol you are using has no data at the time you apply the indicator, the MarketInfo() can't return the right value.

Why don't you filter the calculation with : if(MarketInfo(....) > 0)

Irwan Adnan
927
Irwan Adnan  
FerruFx:

If the symbol you are using has no data at the time you apply the indicator, the MarketInfo() can't return the right value.

Why don't you filter the calculation with : if(MarketInfo(....) > 0)

Thanks FerruFx.

That's what I meant using a trick. Your explanation is clear for me now.

Eric Venturi-Bloxs
71360
Eric Venturi-Bloxs  
belido:

Thanks FerruFx.

That's what I meant using a trick. Your explanation is clear for me now.

You're welcome.
Простак
315
Простак  
FerruFx:

If the symbol you are using has no data at the time you apply the indicator, the MarketInfo() can't return the right value.

Why don't you filter the calculation with : if(MarketInfo(....) > 0)

MarketInfo() must be called one time. The returned value should be stored in a variable. Then the value stored in the variable must be used as in comparison as in division instead of making multiple calls to MarketInfo() (one for comparison and one for division) because there is no guarantee that if the first time MarketInfo() returned non-0 sometime during MT4 starting then it will not return 0 when it is called almost immediately the second time. It is wrong to make an assumption that MarketInfo() will not ever return 0 since it the first time returned non-0 during MT4 starting.
Alain Verleyen
Moderator
33593
Alain Verleyen  
simpleton:
Sorry, there is NO division by zero in the code. Consult the documentation, please.
Of course, the OP get a zero divide error, but there is no division by zero in the code. I suppose you are understanding yourself what you mean.
Простак
315
Простак  
angevoyageur:
Of course, the OP get a zero divide error, but there is no division by zero in the code. I suppose you are understanding yourself what you mean.

According to the documentation MarketInfo() CANNOT return 0 for the particular parameter.

The fact it does means the quality of MT4 implementation is quite poor.

What if MarketInfo() returns a non-zero but still wrong value?

Do programmers need to calculate the same value MarketInfo() should return themselves and then check whether MarketInfo() returned a proper value and they can trust it or not?

The system functions must strictly adhere their contracts. If documentation says MarketInfo() does not return zero (for a particular parameter) then there is NO division by zero in "1 / MarketInfo()" expression. But you implicitly stated that there is.

This is definitely the bug of MarketInfo(). And therefore a workaround is needed.

12
To add comments, please log in or register