Need some help with Divided By Zero Error

 

when i compile i have zero errors  but when i run the ea i see this sometimes pop up 

2022.04.28 15:27:22.012 RoyalPrinceDash AUDCAD,H1: zero divide in 'RoyalPrinceDash.mq4' (1186,107)

and in Metaeditor  Debugger  it highlights  this section of code.  i will post it below. 


void PlotSpreadPips() {
             
   for (int i=0;i<ArraySize(TradePairs);i++) {
      if(MarketInfo(TradePairs[i],MODE_POINT) != 0 && pairinfo[i].pipsfactor != 0) {
       pairinfo[i].Pips = (iClose(TradePairs[i],PERIOD_D1,0)-iOpen(TradePairs[i], PERIOD_D1,0))/MarketInfo(TradePairs[i],MODE_POINT)/pairinfo[i].pipsfactor;     
       pairinfo[i].Spread=MarketInfo(TradePairs[i],MODE_SPREAD)/pairinfo[i].pipsfactor;
       signals[i].Signalperc = (iClose(TradePairs[i], 1, 0) - iClose(TradePairs[i], trigger_TF_HM1, 1)) / iClose(TradePairs[i], trigger_TF_HM1, 1) * 100;
       signals[i].Signalperc1 = (iClose(TradePairs[i], 1, 0) - iClose(TradePairs[i], trigger_TF_HM2, 1)) / iClose(TradePairs[i], trigger_TF_HM2, 1) * 100;    
       signals[i].Signalperc2 = (iClose(TradePairs[i], 1, 0) - iClose(TradePairs[i], trigger_TF_HM3, 1)) / iClose(TradePairs[i], trigger_TF_HM3, 1) * 100;
       signals[i].Signalperc3 = (iClose(TradePairs[i], 1, 0) - iClose(TradePairs[i], trigger_TF_HM4, 1)) / iClose(TradePairs[i], trigger_TF_HM4, 1) * 100;    
       signals[i].Signalperc4 = (iClose(TradePairs[i], 1, 0) - iClose(TradePairs[i], trigger_TF_HM5, 1)) / iClose(TradePairs[i], trigger_TF_HM5, 1) * 100;
                 
      }  



line  1186   is    

signals[i].Signalperc = (iClose(TradePairs[i], 1, 0) - iClose(TradePairs[i], trigger_TF_HM1, 1)) / iClose(TradePairs[i], trigger_TF_HM1, 1) * 100;

so i am confused at how to fix this error   any help would be much appreciated. thank you for all the help community 

 
signals[i].Signalperc = (iClose(TradePairs[i], 1, 0) - iClose(TradePairs[i], trigger_TF_HM1, 1)) / iClose(TradePairs[i], trigger_TF_HM1, 1) * 100;

The divisor must be nonzero.

 
Ahmet Metin Yilmaz #:

The divisor must be nonzero.

so what should I replace the code with. because the math should be correct at least I think. because I can apply sometimes the ea to the chart without seeing this error but then other times I do see the error. 
 

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 (2019)

 
William Roeder #:

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 (2019)

this is a multi currency and multi time-frame  dashboard.  

but I have never seen any of the errors that you mentioned.   4066 4073
 
Richard Louis Pastor #: this is a multi currency and multi time-frame  dashboard. but I have never seen any of the errors that you mentioned.   4066 4073

That is exactly the point! You are not checking for those errors.

ResetLastError();

double dbClose = iClose( TradePairs[i], trigger_TF_HM1, 1 );

if( _LastError == ERR_NO_ERROR )
{
   // proceed with calculations
}
else
{
   PrintFormat("Error code %d obtaining close price for %s", _LastError, TradePairs[i] );
   // Handle the situation;
};
 
Fernando Carreiro #:

That is exactly the point! You are not checking for those errors.

i tried to add your code and i get the error  "resetLastError - Unexpected token, probably type is missing?

 
Richard Louis Pastor #: i tried to add your code and i get the error  "resetLastError - Unexpected token, probably type is missing?

My code was only a sample to illustrate the idea. You can't just copy/paste it into your code.

So, show your code with the error!

 
Fernando Carreiro #:

My code was only a sample to illustrate the idea. You can't just copy/paste it into your code.

So, show your code with the error!

read the opening post .  it shows the error in terminal and the line of code it references to. 
 
Richard Louis Pastor #: read the opening post .  it shows the error in terminal and the line of code it references to. 

It seems you don't understand. The reason you are getting a zero divide error is because:

  • You are not checking your divisor
  • You are not checking you return value from iClose
  • You not checking for returned errors
  • You are not synchronising your data for multi-symbol

I gave you sample code to check the iClose and the error code.

You said that you got "Unexpected token ". I said show your new code with that compile error.

Do you understand that?

Reason: