# Coding logic inquiry

is there a reason why it should be coded this way in order to get the required result

I was trying to create a condition to check if either the type of trade is position buy or sell and below is the outcome

if(OrderType() == (OP_BUY || OP_SELL)) >>>>>>> did not work properly
if(OrderType() == OP_BUY || OP_SELL) >>>>>>> did not work properly

if(OrderType() == OP_BUY || OrderType() ==OP_SELL) >>>>>>> worked properly

although I see the three have the same meaning, so I need some advice about the difference in how the system interprets the three differently

thanks

Did you ever figure this one out?

Hi,

(BUY or SELL) itselft does not return any logical (true/false) value. You need to always have the other side.
Type = BUY OR Type = SELL

If you are programming beginner it may seem strange, as human language sounds differently - Wilczasty is Male or Female but it REALLY means - Wilczasty is Male or Wilczasty is Female.

But if you would ask just “Male or Female?” you cannot define if it is true or false, as you don’t know what is the subject of this question.

OrderType() == (OP_BUY || OP_SELL) - won’t work becuase BUY or SELL does not return true/false itself
OrderType() == OP_BUY || OP_SELL - won’t work becuase SELL is not true/false itself
OrderType() == OP_BUY || OrderType() ==OP_SELL - that’s the way to do it

Hi wilczasty,

Actually a bit more complicated than that because OP_BUY and OP_SELL does return true or false.

I have mentioned it before to Michael that MQL is very forgiving with it’s typecasting, which often leads to logic bombs.

In this case MQL4 will typecast OP_BUY or OP_SELL to boolean. OP_BUY is simply a constant of 0, so it returns false and OP_SELL returns true because it is 1. In fact MQL will turn any number into except 0 into a true. Let’s say you assign a bool variable a value of -55, MQL will turn it into true.

Furthermore, when you create an expression of OP_BUY||OP_SELL, MQL sees this as a logic expression(ie AND OR XOR, etc) and evaluates it as such. When looking at logic expressions, OP_BUY||OP_SELL is evaluated as TRUE||FALSE and the result is true. That means his core expression turns in this

if(OrderType()==TRUE){…

Again, in MQL TRUE = 1, so MQL equation turns into this

if(OrderType()==OP_SELL){…

As a result, the code will compile and even run and works normally when the order type is a sell but it will not work with a buy order.

That is why you need to be explicit about your expressions.