# MQL4 Logical Operators

This article describes a class of operators known as *logical operators*. Learn in this complete MQL4 Logical Operators tutorial what is AND, GREATER THAN and other functions to help you program your own trading EA.We humans use these operators every day without thinking much about them.

For instance, we are constantly processing the logical operations of AND and OR. I will not leave the house when it is 10 degrees outside unless I first have my warm winter jacket AND hat AND I have someplace warm to go to. I will fall asleep tonight in my bed if I count endless sheep, OR imagine being part of a 17th Century Dutch landscape painting, OR read a thick Russian novel. We use these logical operations all the time but don’t usually write them down (or think of them) as machine instructions.

Our MT4 program needs to make decisions, right or wrong, and these decisions require the use of logical operators.

Simple logical operators evaluate to *true* or *false, *and often propose the relationship between two or more arguments or conditions, which is why the are often called relational operators. Here is a table of logical (relational) operators:

Sign | Meaning | Function | Example |
---|---|---|---|

== | Equal To | true, if left hand argument has the same value as the right | If x == y, the condition is true |

!= | Not Equal To, Inequality | opposite of equality, if left hand argument does not have the same value as the right | If x != y, the condition is false |

> | Greater Than | true, if the left hand argument is greater than the right-hand argument | If x > y, the condition is true |

< | Less Than | true, if the left hand argument is less than the right-hand argument | If x < y, the condition is true |

>= | Greater Than or Equal To | true, if left hand argument is greater than or equal to right | If x >= y, the condition is true |

<= | Less Than or Equal To | true, if left hand argument is less than or equal to right | If x <= y, the condition is true |

&& | AND | true, if both left and right-hand arguments are true | If x && y, the condition is true |

|| | OR | true, if either left or right-hand arguments are true | If x || y, the condition is true |

! | NOT | true, if its argument is false; otherwise, false | If !x, the condition is true |

All the entries except the last are relational or comparison operators. I am going to try to provide more specific examples of these relational operators.

Here are some lines of code taken from a custom **OrdersTotalMagicOpen()** function:

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

Within these two lines are contained four relational operators: **equal to** (==), **not equal to** (!=), **AND** (&&), and **OR** (||). The first line of code is a good representation of the **unequal** (!=) operator and the **OR** (||) operator. It is saying that if the open trade’s symbol is NOT the symbol of the EA’s underlying chart, OR if the open trade’s magic number is not that of the strategy, then the program can **continue**. What does **continue** mean? Well, it is another operator that gives control to the beginning of the nearest outward cycle **while** or **for** operator, that is, it skips the immediate series of computations because they no longer apply. The second and third line are a good example of the **equal to** (==) and **AND** (&&) operators. It is saying that if the open trade’s symbol is the symbol of the EA’s underlying chart, and the open trade has the EA’s magic number, then we can process the next computations.

We usually see these greater to or less than relational operators when comparing price points or indicators with each. For instance, let us look at one way of representing the buy condition of bollinger bands:

&& iBands(NULL,bandp, bandpx, banddev,0,PRICE_CLOSE,MODE_LOWER,0) >= iClose (NULL,0,0)

BuyCondition1 == true;

Here you can see a buy condition that becomes true only if two arguments joined by AND (&&) are also true: the first argument has the **less than** (<) operator in effect, and the second has the **greater than** **equal to** (>=) operator in effect. The first argument is saying that the lower band of the previous BB must have been less than the previous close. The second argument is saying that the lower band of the current BB must now be greater than or equal to the current close. The two arguments combined translates into: *buy when the close crosses over the lower band*. Since there is no resident cross over function, the cross over must be constructed in two parts: what occurred in the immediate past (lower band was below close), and what is occurring in the present (lower band is now touching or above close).

You can compare any two values as long as they are of the same data type. You can compare a boolean value to the constants true or false.

## Boolean Operations

We use the boolean operators AND (&&) and OR (||) to combine relation operations. The AND operator evaluates whether all conditions are true. If so, the entire statement is true. If any of the conditions are false, the entire statement is false.

{

// Open order

}

If BooleanVar1 is equal to true, and Indicator1 is greater than indicator2, the statement evaluates to true, and the code between the braces is run. If either of these conditions are false, the entire statement evaluates to false,a nd the code in the braces is not run. There any number of conditions combined together with the && operator, and they must all evaluate to true.

The OR operator evaluates whether any one of the conditions are true. If at least one condition is true, the entire statement is true. If all the conditions are false, the statement evaluates to false.

If either Booleanvar1 is equal to true, or Indicator1 is greater than Indicator2, the statement is evaluated as true. if both of these conditions are false, the statement evaluates to false.

You can combine AND and OR operations to create more complex trading conditions. When doing so, use parantheses to establish the order of operations.

The statement within parenthesis is evaluated first. If both of these conditions are true, the statement evaluates to true, and we are left with an OR operation.