Division problem

 

Hi ,

 First of all sorry, I know there are a lot of topics with this issue, but couldn't find an answer to my problem.


  Here is piece of code :

   

   Dt[i] = ((alpha * MathAbs(Et[i] - Xt[i])) + ((1 - alpha) * Dt[i+1]));
         
       CI[i] = ((High[i]+Low[i])/2) / (0.015 * Dt[i]]);

where Dt[] is defined as double, of course.


 Depending on the instrument, the Dt[i] value is greater or lower than 0. For example, on GBPUSD, it's value is 0.0002 :

If I want to run the code like this, I get zero divide error on this division for the value of CI[i]. But if Inter the value manually in the code, it works :

CI[i] = ((High[i]+Low[i])/2) / (0.015 * 0.000275);

How can i fix this problem? I could go with a fixed value instead of the Dt[i], but compared to the GBPUSD, the US100 has a Dt value of 8.xxx


  Thank you !

 

Because you cannot drop the value availability checks for the previous bar that are in the original algorithm.



 
Vladislav Boyko #:

Because you cannot drop the value availability checks for the previous bar that are in the original algorithm.



Have done that, I check for the previous value to not be empty.

  if ( Dt[i+1] == EMPTY_VALUE ) Dt[i] = MathAbs(Et[i] - Xt[i]);
         else 
         Dt[i] = ((alpha * MathAbs(Et[i] - Xt[i])) + ((1 - alpha) * Dt[i+1]));
         
       CI[i] = ((High[i]+Low[i])/2) / (0.015 * Dt[i]]);

The problem is with the diviion, with this line :

CI[i] = ((High[i]+Low[i])/2) / (0.015 * Dt[i]]);

more specifically with this part :

 / (0.015 * Dt[i]])

In my case, Dt[i] = 0.000275. So I get an error if I use the Dt[i] as a variable, but it works like this :

 / (0.015 * 0.000275)


There are no variables with empty value in this line. The Dt[i] has a value, you can see in the screenshot of my initial post. 

If I try to make the division with the variable Dt[i] in the function, I get zero divide error. If I enter any numeric value to replace the Dt[i], it works.

 

OLIVER GYILA #:

Yes, I was wrong. The division by zero is not caused by what I wrote there.

Try doing something like this:

   if ( Dt[i+1] == EMPTY_VALUE )
      Dt[i] = MathAbs(Et[i] - Xt[i]);
   else Dt[i] = ((alpha * MathAbs(Et[i] - Xt[i])) + ((1 - alpha) * Dt[i+1]));
   //---
   if(Dt[i] == 0.0)
     {
      Print("Division by 0 is expected...");
      Print("i = ", i);
      Print("Dt[i+1] == EMPTY_VALUE --> ", Dt[i+1] == EMPTY_VALUE); // To find out exactly how Dt[i] was calculated
      Print("< print all values ​​that were used to calculate Dt[i] >");
     }
   //---
   CI[i] = ((High[i]+Low[i])/2) / (0.015 * Dt[i]]);