Having a strange zero divide problem in a function code is here:

 

This is my first post on the forum, Have been coming here and reading off and on for some time. Its a great place to learn. One of the best I've found for the "mql" language! Thanks to all that make it possible and those who Moderate it as well!!

The problem is most likely easily fixed, I've tried several different conditionals and methods to extract the seconds, minutes, hours, and days till the end of the current bar. There are some indicators from the internet "bclose" and "End Of Candle" The latter is great but i'd rather have a personalized function to learn with and use instead .

Zero Divide comes up after the seconds on the 1 minute bar reach zero when the function is implemented. And of course the program fails to continue after.

Division is used in six places;

  • 1.seconds by minutes==minutes
  • 2.modulus of seconds by minutes==left over seconds
  • 3.minutes by hours==hours
  • 4,modulus of minutes by hours==left over hours
  • 5.hours by days==days
  • 6.modulus of hours by days==left over hours

Seems right to me but my understanding of the modulus and divide operators might be blurred.


Thanks in advance for the help!






string candle_close()

{
double i;
int days=0, hours=0, minutes=0, seconds=0, candleseconds=TimeCurrent()-Time[0];

if(candleseconds<=0) {return("0:0"); Print("candle seconds is zero");}

else if(candleseconds>Period()*60) {return("0:0"); Print("candle seconds is larger the period multiplied by 60");}

else
{
seconds=Period()*60-(TimeCurrent()-Time[0]);

if(seconds>59)
{
minutes=seconds/60; //divide here
seconds=seconds%60;

if(minutes>59)
{
hours=minutes/60; //divide here
minutes=minutes%60;

if(hours>23)
{
days=hours/24; //divide here
hours=hours%24;
//if(days>1) return("EOC:"+days+"d:"+hours+"h:"+minutes+":"+seconds);
// else return("EOC:"+hours+"h:"+minutes+":"+seconds);
}
// else return("EOC:"+minutes+":"+seconds);
}
// else return("EOC:"+seconds);
}
// else return("EOC:00");

if(days<0||hours<0||minutes<0||seconds<0) return("time data error");

else if (days>0) return("EOC:"+days+"d:"+hours+"h:"+minutes+":"+seconds);
else if (hours<24&&days==0) return("EOC:"+hours+"h:"+minutes+":"+seconds);
else if (minutes<59&&days==0&&hours==0) return("EOC:"+minutes+":"+seconds);
else if (seconds<59&&days==0&&hours==0&&minutes==0) return("EOC:"+seconds);


}
}
 
If you're going to use division then make sure that the Right-Side cannot be equal to 0. Otherwise you'll get that nasty 0-divide error which stops everything in its track. Example. if( Right_Side != 0 ){ Minutes=Left_Side / Right_Side; }.
 
  1. Your code as posted does not have a divide by zero. You only have a /60 and /24. Your problem lies elsewhere
  2. The current candle is SUPPOSED to end at Time[0] + Period()*60. But Mt4 will NOT start a new candle until it gets a new tick. Therefor your
    seconds=Period()*60-(TimeCurrent()-Time[0]);

    seconds can be any value from Period()*60 to zero to negative values (new candle is overdue or market is now closed.)

    You need to test seconds for zero and negative


  3. if(seconds>59)
    {
    minutes=seconds/60; //divide here
    seconds=seconds%60;
    Your If statements are both unnecessary and wrong. If seconds < 60, you still want to compute minutes, hours and days as zero.
int seconds = Period()*60-(TimeCurrent()-Time[0]);
if (seconds >= 0) string ov="EOC ";
else{   ov="Over Due "; seconds=-seconds;   }
int minutes = seconds/60,
    hours   = minutes/60,
    days    = hours/24;
hours   %= 24;  minutes %= 60;  seconds %= 60;
if (days>0)     return(ov+days+"d:"+hours+"h:"+minutes+":"+seconds);
if (hours>0)    return(ov++hours+"h:"+minutes+":"+seconds);
if (minutes>)   return(ov++minutes+":"+seconds);
return(ov+seconds);
 

I see what you mean. The divisors used in this case are not variables though 60,60,24 and they are not given the ability to change since they are never incremented etc

minutes=seconds/60;

Something that I failed to extrapolate on in the original post was that the immediate conditionals following the assignment of "seconds" should never be accessed once the value of seconds is less than 59.(if division isn't allowed to take place how can there be a zero division error)

"if(seconds>59){->divide->assign value->if(minutes>59)-> divide-> assign->if(hours>23)-> divide-> assign->}

It certainly adds a little more mystery to this error.

Try

init_start()

{

Print(string candle_close());

}

on the 1min chart and wait until the countdown is at zero if you have time. This is where the error occurs.

I'm certain its something simple... Its been seven or eight times to no avail. If I am overlooking the "right_side" and dividing by zero intact please point out the line. Thanks ubzen!

 
WHRoeder:
  1. Your code as posted does not have a divide by zero. You only have a /60 and /24. Your problem lies elsewhere
  2. The current candle is SUPPOSED to end at Time[0] + Period()*60. But Mt4 will NOT start a new candle until it gets a new tick. Therefor your

    seconds can be any value from Period()*60 to zero to negative values (new candle is overdue or market is now closed.)

    You need to test seconds for zero and negative


  3. Your If statements are both unnecessary and wrong. If seconds < 60, you still want to compute minutes, hours and days as zero.

Thanks A lot WHReader! You must be right I will continue the search through the rest of the code..

Thanks for your edits I am still learning I do know its painfully obvious.

Replies to my post came alot faster than expected. All are appreciated!


.

 
fractalcurve:

Thanks A lot WHReader! You must be right I will continue the search through the rest of the code..

Thanks for your edits I am still learning I do know its painfully obvious.

Replies to my post came alot faster than expected. All are appreciated!

Haven't had any educational courses of on programming that really was a great lesson. :D

Thanks again..


Reason: