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
//--- return value of prev_calculated for next call
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.
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
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)
That's what I meant using a trick. Your explanation is clear for me now.
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.