A program or expert advisor is constantly evaluating logical expressions to take decisions and to act accordingly. Logical expressions are answered by true or false. In our Fig.49 example, we are illustrating once again our simple moving average strategy:

  1. The first IF statement evaluates if the value of the LastPrice variable is bigger than that of the MovingAverage. If the statement is answered by true, the program continues to execute the code inside the curly brackets, which is to call the function to close existing SELL POSITIONS. Note that every IF statement is using a logical operator, which is (>) for bigger than or (<) for smaller than.
  2. The second IF statement is nested in the first IF statement. It means that the first IF statement should be true for the nested second IF statement to be evaluated.
  3. The third block of the IF statement is the exact opposite of the first IF statement. The condition is true if LastPrice is smaller than the value of the MovingAverage.
  4. The fourth and the last block of the IF statement is nested in the third IF statement. The function to close existing BUY POSITIONS is only executed if both conditions are met, LastPrice smaller than MovingAverage,” and that BuyPositionExists is true.
Examples of IF statements that compare variables
Fig.49 Examples of IF statements that compare variables

Logical operators

Symbol Meaning Example
== Equal to “Equal to” articulates the argument on the left-side to be the same value as the one on the right-side. For instance, x == y means that “if x is equal to y, the condition is true.”
!= Not equal to “Not equal to” considers the argument on the left-side to be different from the value on the right-side argument. For instance, x != y expresses “if x is not equal to y, the condition is true”.
> Bigger than “Bigger than” considers the argument on the left-side to be greater than that on the right-side. For instance, the x > y condition is true “if x is greater than y.”
< Smaller than “Smaller than” expresses the argument on the left-side should be smaller than the one on the right-side to be considered true. For instance, x < y returns true “if x is smaller than y..”.
>= Bigger or equal to “Bigger or equal” is true if the left-side argument value is greater or equal to the right-side argument. For instance, x >= y expresses, “if x is bigger or equal to y, the condition is true.”
<= Smaller or equal to “Smaller or equal” means that the condition will be true if the left-side argument is smaller or equal to the one on the right-side. For instance, x <= y expresses, “If x is smaller or equal to y, then the condition is true.”
&& And “And” considers both arguments, left and right, to be true to meet the condition. For instance, x && y means that “for the condition to be true, x and y must be both true.”
|| Or “Or” expresses that either the arguments on the left or right side both are true or one of them is true to meet the condition. For instance, if x or y is true, the condition is true.
! Not “!” exclamation mark is used for logical negation. The condition is true if its arguments are false. For instance, !x, for the condition to be true, x must be false.

  

Practical examples

To have a better understanding of how logical expressions work, we have compiled some examples in Fig.50 of the most commonly used logical operators:

  1. The exclamation mark is applied in negation. When you have an exclamation in front of an argument, it means that this argument or variable must be false for the condition to be true. In our example, it means that for the condition to be fulfilled, the variable OpenedPositions should be false for the code inside the curly brackets to be executed.
  2. The second example in Fig.50 assembles two arguments. Notice the logical operator (||), which indicates an “or.”  In this IF statement, the variable RSI should be less than 30 OR LastPrice should be greater than MovingAverage for the condition to be true.
  3. On the other hand, in the third example in Fig.50, the two arguments should be true for the condition to be true, as the (&&) “and” logical symbol is stipulated.
  4. The fourth example in Fig.50 demonstrates a combination of multiple arguments into one IF statement. Note the use of parentheses to group arguments. The first argument to be true must comply with RSI less than 30 AND LastPrice greater than MovingAverage

The second argument, to be true, must conform with RSI greater than 70 AND LastPrice less than MovingAverage. And for the condition to be true, either the first or the second argument must be true. The condition must be met to execute the code inside the curly brackets as stipulated by the (||) “or” logical symbol.

Practical examples of logical operators
Fig.50 Practical examples of logical operators
Tips to simplify code (Fig.51):

  • Write multiple arguments in a single statement.
  • Use parentheses to group your arguments.

 

 

Multiple arguments in a single statement vs. nested if statements.
Fig.51 Multiple arguments in a single statement vs. nested if statements.

Leave a Reply

  +  61  =  63