Can price != price ? - page 8

 

For those who jumped through all the hoops and tried all solutions here's what I wrote as a quite-reliable solution for myself.

If you still in search for solution this means you came across something like this:

coparing 107.123111111 and 107.123999 results in 107.12being compared with 107124 as a result of double rounding variance.

Here's my solution for this problem that disregards range of +1 and -1 to remove double rounding variance from comparison. 


Try this:

      // double compare by leoa451
        int CompDoubleE0L1M2(double DoubleA, double DoubleB, int PointsToCompare)
      {
         int IntForDoubleA = (int)(MathFloor(MathPow(10,PointsToCompare) * DoubleA));
         int IntForDoubleB = (int)(MathFloor(MathPow(10,PointsToCompare) * DoubleB));
         
         if(IntForDoubleA >= IntForDoubleB-1 && IntForDoubleA <= IntForDoubleB+1) { return 0; }         // if doubleA within +1/-1 range of doubleB (to compensate for the double rounding veriance)
         else if(IntForDoubleA < IntForDoubleB) { return 1; }                                           // if doubleA < doubleB
         else if(IntForDoubleA > IntForDoubleB) { return 2; }                                           // if doubleA > doubleB
         else return -2;                                                                                // error
      }


 

 

And here is another solution if simply comparing two doubles and requiring a bool result:

// return true if same double value within digits comparison
bool CompareDoubleEqual(double a, double b, int digits)
{
   if (MathAbs(a - b) > MathPow(10, -digits))
      return(false);

   return(true);
}


 

 
Dretta: And here is another solution if simply comparing two doubles and requiring a bool result:
  1. If they differ by a point, you return true. Which is wrong — they are not equal.

  2. Simplify

    bool CompareDoubleEqual(double a, double b, int digits)
    {
       return MathAbs(a - b) < MathPow(10, -digits);
    }
    Increase Order after stoploss - MQL4 programming forum #1.3 2017.05.29
 

I like to use a function created by me that make easy for me to know what I did.

To test I use:

 D_C( 5.564 , ">=" , 9.29) 

 D_C( 5.564 , "==" , 9.29) 


Here is the function:


bool D_C(double Valor_1,string sinal,double Valor_2)

  {

   int precision = 5;

   if(sinal!=">" && sinal!=">=" && sinal!="<" && sinal!="<=" && sinal!="==")

     {

      Print("Erro at the function [D_C].\nsinal = ["+sinal+"]");

      return(false);

     }

   int valor_teste_1=(int)MathRound(Valor_1*MathPow(10, precision ));

   int valor_teste_2=(int)MathRound(Valor_2*MathPow(10, precision ));

   if(sinal==">")

     {

      if(valor_teste_1>valor_teste_2)

         return true;

      else

         return false;

     }

   else

      if(sinal==">=")

        {

         if(valor_teste_1>=valor_teste_2)

            return true;

         else

            return false;

        }

      else

         if(sinal=="<")

           {

            if(valor_teste_1<valor_teste_2)

               return true;

            else

               return false;

           }

         else

            if(sinal=="<=")

              {

               if(valor_teste_1<=valor_teste_2)

                  return true;

               else

                  return false;

              }

            else

               if(sinal=="==")

                 {

                  if(valor_teste_1==valor_teste_2)

                     return true;

                  else

                     return false;

                 }

               else

                  return(false);

  }
 
Luiz Alberto Barbosa Leal #:

      if(valor_teste_1>valor_teste_2)

         return true;

      else

         return false;

Simplify your code
          Increase Order after stoploss - MQL4 programming forum #1.3 (2017)

      return valor_teste_1>valor_teste_2;
 
William Roeder #:

You want the biggest value that can not be considered roundoff error or equivalently, the smallest value that can not be considered a price change. Since prices can only change by a multiple of point, point/2 is just that.

Hi. Isn't the 5 that would result from division of _Point/2 also rounded up hence also considered a price change. In that case, why not use the number just before (_Point/2)?

 
salitos #: . Isn't the 5 that would result from division of _Point/2 also rounded up 

Could be rounded up 0.0000050000001, could be rounded down 0.0000049999999. A change in price is twice that.

 
William Roeder #:

Could be rounded up 0.0000050000001, could be rounded down 0.0000049999999.

But conventionally its rounded up right?

Would it be okay to modify your code which is equivalent to this

   if((newTickPrice-oldTickPrice)>(MathPow(0.1,1)*_Point*5))
     {
     //logic for (positive) price change
     }

to this

   if((newTickPrice-oldTickPrice)>(MathPow(0.1,2)*_Point*49))
     {
     //logic for (positive) price change
     }
 
salitos #: But conventionally its rounded up right?

No such thing. 0.1 can not be represented in floating point.

You are overthinking it. If a price changes by at least point - rounding, it has changed. Point/2 is less than that.

 
William Roeder #:

No such thing. 0.1 can not be represented in floating point.

Had overlooked that sir. And I guess the fact that in the 'real-world' computer 5 may round down factors in the >0.49 case.

Reason: