MQL4 - automated forex trading   /  

Forum

Newbie if statement qestion

Back to topics list To post a new topic, please log in or register

avatar
4
naz 2008.02.12 01:15 
Hello,

Can someone tell me why the following statement does not work?

if(Bid == SellOpenPrice)


For some reason it returns false most of the time when it is true.

Thank you in advance
article

Review of Participants by Yuriy Zaytsev (YuraZ)

I am not surprised by the profit of the top ten.


avatar
2462
phy 2008.02.12 01:58 

"Can someone tell me why the following statement does not work?"

Because they are not equal despite your protestations.

Comparing equality of doubles is a known problem because of the way doubles are represented in binary.


When you get stuck, you can change to something like:

if(MathAbs(Bid - SellOpenPrice) < .00001)


Or...

Rosh 2007.05.16 12:13

Use function from Stdlib.mq4

//+------------------------------------------------------------------+
//| right comparison of 2 doubles                                    |
//+------------------------------------------------------------------+
bool CompareDoubles(double number1,double number2)
  {
   if(NormalizeDouble(number1-number2,8)==0) return(true);
   else return(false);
  }

avatar
4
naz 2008.02.12 02:36 
phy wrote:

"Can someone tell me why the following statement does not work?"

Because they are not equal despite your protestations.

Comparing equality of doubles is a known problem because of the way doubles are represented in binary.


When you get stuck, you can change to something like:

if(MathAbs(Bid - SellOpenPrice) < .00001)


Or...

Rosh 2007.05.16 12:13

Use function from Stdlib.mq4

//+------------------------------------------------------------------+
//| right comparison of 2 doubles                                    |
//+------------------------------------------------------------------+
bool CompareDoubles(double number1,double number2)
  {
   if(NormalizeDouble(number1-number2,8)==0) return(true);
   else return(false);
  }
Thanks Phy. I will give it try.
Back to topics list  

To add comments, please log in or register