!= evaluates to true when equal

 
Hi all,

I have written an EA that contains the code listed below. It is basically running exactly as I need but I am concerned that there is some sort of bug because unnecessary OrderModify commands are made to the server.

The EA is running on an hourly chart and, as such, the LowestLow variable only changes at the close of a bar. Therefore, the EA should only modify the order if the LowestLow changes at most once an hour. However, what I am seeing is a modify command every 10-20 seconds which means that (LowestLow + Spread) is not equal to the TakeProfit price. My debug statement tells me that LastError = 1 (No error returned, but the result is unknown.) and that OrderTakeProfit() and (LowestLow + Spread) are exactly the same. Why then did the outer if statement evaluate to true?

OrderSelect(TicketNo, SELECT_BY_TICKET, MODE_TRADES);
if (OrderTakeProfit() != LowestLow + Spread) {
    if (OrderModify(TicketNo, HighestHigh, OrderStopLoss(), LowestLow + Spread, 0, Red) == false) {
        LastError = GetLastError();
        Print("[MODIFY] ", TicketNo, ": ", LastError, " ", OrderTakeProfit(), " != ", LowestLow + Spread);
    }
}
 
use normalization before compare of double values.
see "Double trouble Bug?"
 
use normalization before compare of double values.
see "Double trouble Bug?"


The discussion there ist still open. Unresolved.
 
OrderTakeProfit() and (LowestLow + Spread) are exactly the same

That's you see with 4 decimals.Try :
if(NormalizeDouble(OrderTakeProfit(),Digits) != NormalizeDouble(LowestLow + Spread,Digits))
 
That's you see with 4 decimals.Try :
if(NormalizeDouble(OrderTakeProfit(),Digits) != NormalizeDouble(LowestLow + Spread,Digits))



If we should use the Normalize-Function on every comparing of double values the whole EA would get very unreadable.
By the way, the requirement for using this function is very uncommon.
There is no real C compiler, that has such behavior.
And Mq4 is very near to C language.
Whats the problem with implementing the normalization in automatically background?
Mq4 Compiler knows all double variables and can normalize by itself.
 
If we should use the Normalize-Function on every comparing of double values the whole EA would get very unreadable.

The problem is that even simple compare like this does not work.

if(1.2 - 1.0 == 0.2)
Print("true");
else
Print("false");

As I said earlier double arithmetic is virtually not usable in MT4 environment.
 

if(1.2 - 1.0 == 0.2)
Print("true");
else
Print("false");

As I said earlier double arithmetic is virtually not usable in MT4 environment.


Did you try
if((1.2 - 1.0) == 0.2)?
May be brackets can help?
I will test it later.
 
Thanks Slawa, I had an idea that it had something to do with the precision of the number but the print function does not reveal it and the way around it was not immediately obvious.

For sub and lomme, I would recommend you have a look at an article like https://en.wikipedia.org/wiki/Floating_point that explains the reason why MT4 is "plagued" with this problem. The NormalizeDouble function exists for this reason and it is effective for most tasks. It would be an issue if this function did not exist.

It would seem that MQL4 was built using a proprietary platform which does not implicity normalize doubles but it does provide the functions necessary to achieve this. Why waste time overloading the operator?
 

if(1.2 - 1.0 == 0.2)
Print("true");
else
Print("false");

As I said earlier double arithmetic is virtually not usable in MT4 environment.


If this is true we can forget MT4.
Did you try
if((1.2 - 1.0) == 0.2)?
May be brackets can help?
I will test it later.

NO!
Not in mine MT4.
 
Thanks ajw,

after reading the article I tested if(1.2 - 1.0 == 0.2) with an ANSI C Compiler.
Same behavior.

So we have to live with that NormalizeDouble() function.
 
Thanks Slawa, I had an idea that it had something to do with the precision of the number but the print function does not reveal it and the way around it was not immediately obvious.

For sub and lomme, I would recommend you have a look at an article like https://en.wikipedia.org/wiki/Floating_point that explains the reason why MT4 is "plagued" with this problem. The NormalizeDouble function exists for this reason and it is effective for most tasks. It would be an issue if this function did not exist.

It would seem that MQL4 was built using a proprietary platform which does not implicity normalize doubles but it does provide the functions necessary to achieve this. Why waste time overloading the operator?

Good article!
I am coming from the PERL environment so I guess I am spoiled.
There is no "double" issue in the PERL.

The question is: Should one use double and keep normalizing or should one convert data to an integer and then if needed convert to a double?

I am still unclear how to properly normalize?
Should I keep normalizing a normalized result?
Example:
a=Normalize(1.2-1.0, 4)
if(a==0.2)

or

if(Normalize(a==0.2,4))

or

Should every operation be Normalized?
I think there should be more explanation on how to properly Normalize without over Normalizing.
I think Normalizing makes program slower.
Reason: