Home | MQL4 Tutorial For Dummies | Blogs | EA Builder Blogs
Memberships Login | MQL4 Course | Community | Contact Us

How to Compare Multiple Conditions using AND OR in Forex Trading Strategy

How to Compare Multiple Conditions using AND OR in Forex Trading Strategy

In previous post, you have learned how to use the relational operators to build a single criteria check.

  • more than,
  • lesser than,
  • more or equal than,
  • lesser or equal than,
  • equal
  • not equal

How about if you want to design multiple criteria check?

In this post, you will learn on Logical Operator 

  • How could Logical Operator do

  • How to use AND operator

  • How to use OR operator

  • How to check more than 2 conditions?

 


How could Logical Operator do

Logical operator allows you to check multiple criteria in a same time.

During your design of trading strategy, you might come across the scenario as below

Scenario #1

  • I want to do a pending buy stop when
    • When 5 moving average is > 13 moving average
    • When RSI < 70
    • When RSI > 30
    • When MACD signal value is > 0 (Positive)

In this scenario, you want all conditions to be met in order to perform your buy stop

Scenario #2

  • I want to close the trade
    • When the 5 moving average is < 13 moving average  OR
    • When RSI > 70

In this scenario, you want to close the trade when either one of the condition is met , whether is the 5 moving average is < 13 moving average or RSI is > 70.

Let's reveal how to cover the scenario 1 and scenario 2 using logical operator.


How to use AND operator

AND operator or && (double ampersand) is a operator that allow you to compare A condition and B condition where it will only be true when A condition is matched AND B conditions is matched.

In this example, I will use two conditions to check

  • Condition A:
    • 5 moving average is > 13 moving average
  • Condition B:
    • RSI < 70

Case #1 is the only one is matched when condition A and condition B is matched.

Only any of the condition is fail, then the overall criteria check in AND operator will be fail (Case #2, Case #3 and Case #4)

This is suitable for strategy that need an absolute match on the criteria to do on the action. If anyone of the condition checking fail, then action will not be proceeded.




How to use OR operator

OR operator or || (double vertical splash) is a operator that allow you to compare A condition and B condition where it will be true when either A condition is matched OR B conditions is matched.

Similarly, I am using the same example from above

  • Condition A:
    • 5 moving average is > 13 moving average
  • Condition B:
    • RSI < 70

If using OR operator, any of the condition is true, then the overall result will be true.  This result is true for Case #1, Case #2, and Case #3.

It will only be fail, when both condition A and condition B is failed (Case #4).


How to check more than 2 conditions?

Now you should have know how to  compare 2 conditions concurrently. How about 3 conditions ? 4 ? or 5 ?

Same rules logical condition applied to more conditions check even if is 3 conditions or 4 conditions or 5 conditions.

Lets say you have a condition as below to create a new trade and you are using AND to check on 4 conditions

  • Condition 1: I want to have 3 continuous bullish bar
  • Condition 2: I want to check RSI is between 70 and between 30
  • Condition 3: I want to check 5 moving average is above 13 moving average
  • Condition 4: I only want to trade in between 3pm to 10pm

So if you are using AND condition, anyone of the condition is not matched, then your new trade will be executed


That's end of the topic. This concludes the condition checking lessons:

  • You should have know how to do a simple condition check
  • You should have know how to make multiple condition check through AND or OR operator

I hope you have learned something from this post. Thank you for reading and happy learning.



Leave a Reply

Your email address will not be published.


*