Critical runtime error 503 in OnTick function zero divide

 

Hello!

The code below gives me occasional zero divide warning.

Please help me form the code in the way that will prevent this error.

Thank you.

double NormalizeScaleValuesRange(double old_min, double old_max,double new_min, double new_max,double value)
  {
   // Round numbers:
   old_min=NormalizeDouble(old_min,5);
   old_max=NormalizeDouble(old_max,5);
   
   if(MathRound(old_max-old_min) == 0)					// <-- THIS SHOULD CATCH THE 0 DIVIDE BUT IT DOESN'T ALWAYS WORK
      return(0);
    
   double scale = (new_max - new_min) / MathRound(old_max - old_min); // <-- THIS DIVISION GIVES 0 DIVIDE ERROR
   double result = (new_min + ((value - old_min) * scale));
   return(result);
  }
 
Matija Bensa:

Hello!

The code below gives me occasional zero divide warning.

Please help me form the code in the way that will prevent this error.

Thank you.

Well check whether new_max == new_min and think when this can happen e.g. initialization, or only one tick, or ...
 

I think that the problem is because of rounding the number.

MathRound(old_max - old_min)

anything between -0.5 and 0.5 will round to zero.

 
Keith Watford:

I think that the problem is because of rounding the number.

anything between -0.5 and 0.5 will round to zero.

Why would not it be caught in the line above ?

   if(MathRound(old_max-old_min) == 0)
      return(0);
 
Carl Schreiber:
Well check whether new_max == new_min and think when this can happen e.g. initialization, or only one tick, or ...

It happens only to some of the ticks, depending on the values.

I think that if new_max == new_min shouldn't be a problem because that would mean that we would be dividing zero with something else and that would result in zero.

 
Matija Bensa:

It happens only to some of the ticks, depending on the values.

I think that if new_max == new_min shouldn't be a problem because that would mean that we would be dividing zero with something else and that would result in zero.

Are you sure this is this exact code that triggers the critical error ? Seems surprising, as you are testing against 0 just before.

If you are sure, print the values in the log and check them.

Which MT5 build are you using ? Is this happening on a live chart only or also with the Strategy Tester ?

 
Alain Verleyen:

Are you sure this is this exact code that triggers the critical error ? Seems surprising, as you are testing against 0 just before.

If you are sure, print the values in the log and check them.

Which MT5 build are you using ? Is this happening on a live chart only or also with the Strategy Tester ?

I am sure this is the exact line. I am using the latest MT5. Below is the copy of the error from the MT5 journal.

It happens only in Strategy Tester when optimizing. I have to check the article.

Is there a difference weather the calculation is made on local PC or in the cloud?

2021.02.26 17:28:51.962 Core 2  genetic pass (0, 475) tested with error "critical runtime error 503 in OnTick function 
(zero divide, module Experts\x_EA_v1.94.7.ex5, file x_EA_v1.94.7.mq5, line 1808, col 42)" in 0:00:00.068
 
Matija Bensa:

I am sure this is the exact line. I am using the latest MT5. Below is the copy of the error from the MT5 journal.

It happens only in Strategy Tester when optimizing. I have to check the article.

Is there a difference weather the calculation is made on local PC or in the cloud?

Try it like that :

double NormalizeScaleValuesRange(double old_min, double old_max,double new_min, double new_max,double value)
  {
   // Round numbers:
   old_min      = NormalizeDouble(old_min,5);
   old_max      = NormalizeDouble(old_max,5);
   double diff  = MathRound(old_max-old_min);

   if(diff == 0)                                        // <-- THIS SHOULD CATCH THE 0 DIVIDE BUT IT DOESN'T ALWAYS WORK
      return(0);
    
   double scale = (new_max - new_min) / diff; // <-- THIS DIVISION GIVES 0 DIVIDE ERROR
   double result = (new_min + ((value - old_min) * scale));
   return(result);
  }
 
Alain Verleyen:

Why would not it be caught in the line above ?

You are right.

So the only answer is that the code posted by the OP is not the code that is causing the problem.

 
Alain Verleyen:

Try it like that :

Thank you! I have tested it and didn't get any errors lately. I hope this solved it.

Reason: