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. Post Views: 4,759
How to use more than , lesser than, equal or not equal in Forex Trading Strategy As a part of how to design your forex trading strategy, you need to know what is the condition that you could check for ? In general, you know you could compare the price, buw how to compare the price? For example, you want to check Whether the price has moved more than the 5 moving average price ? Whether the price has moved lesser than the 5 moving average price? This post is going to reveal the type of mathematic condition that you could use to check your criteria, they are known as relational operators. This knowledge will help you when you’re designing your condition using the EA builder to design your condition for custom indicator custom EA. What you’ll learn in this post What does relational operators do? More than > Lesser than < Equal == More or equal than >= Lesser or equal than <= Not equal != These are the relational operators that are commonly used when you want to design your trading strategy. What does relational operator do? From the example below, I am using two variables to make comparison A and B . A and B can either be a price Ask, Bid, Open Price, High Price, Low price, or Close price an indicator price (i.e. Moving average price , Bollinger band price) Time Volume or etc A reminder, the comparison will ONLY give you TWO results in True / Matched / Condition passed False / Mismatched / Condition failed For both of the results, you could decide to do the actions that you want. For example, if the condition checking is passed, then you do a buy trade, on the other hand, if the condition checking is failed, then you do a sell trade. On both results, whether is true or fail, you could decide to do something on each of the result, or you could just do nothing. For example, I only want to execute sell trade when the result is fail. I will do nothing if the checking is true. I only want to close the trade when the condition checking is true. More than > This operator more than > is to check whether A is more than B. For example, Examples Condition Check IF Result Graph Examples A = 102.00 B = 101.00 A > B Condition Matched. A = Blue Line B = Red Line A = 101.00 B = 102.00 A > B Condition Failed. A = Blue Line B = Red Line Lesser than < This operator lesser than < is to check whether A lesser more than B. For example, Examples Condition Check IF Result Graph Examples A = 102.00 B = 101.99 A < B Condition Failed. A = Blue Line B = Red Line A = 101.99 B = 102.00 A < B Condition Matched. A = Blue Line B = Red Line Equal == This equal operator is to check the whether A is exactly the same with B. For example, Examples Condition Check IF Result Graph Examples A = 70.00 B = 70.00 A == B Condition Matched. A = Blue Line B = Red Line A = 70 B = 71.01 A == B Condition Failed. A = Blue Line B = Red Line A = 70 B = 69.99 A == B Condition Failed. A = Blue Line B = Red Line More or Equal than >= More or equal than operator is to check whether the price is more than OR equal with. Unlike the more than > comparison, if the price is exactly the same, it is still not true. However, in this comparison, the result is true when the price is exactly the same. For example, Examples Condition Check IF Result Graph Examples A = 70.00 B = 70.00 A >= B Condition Matched. A = Blue Line B = Red Line A = 70 B = 71.01 A >= B Condition Matched. A = Blue Line B = Red Line A = 70 B = 69.99 A >= B Condition Failed. A = Blue Line B = Red Line Lesser or Equal than <= This is the opposite of the more or equal than operator. It is to check if the price is lesser or equal than . The condition is true when the price is exact the same or lesser. For example, Examples Condition Check IF Result Graph Examples A = 70.00 B = 70.00 A <= B Condition Matched. A = Blue Line B = Red Line A = 70 B = 71.01 A <= B Condition Failed. A = Blue Line B = Red Line A = 70 B = 69.99 A <= B Condition Matched. A = Blue Line B = Red Line Not Equal != Not equal is the opposite sign of equal. Anything that is not equal then will result in true. For example, Examples Condition Check IF Result Graph Examples A = 70.00 B = 70.00 A != B Condition Failed. A = Blue Line B = Red Line A = 70 B = 71.01 A != B Condition Matched. A = Blue Line B = Red Line A = 70 B = 69.99 A != B Condition Matched. A = Blue Line B = Red Line So summary, in this post, you should have learned how to use the relational operator to make your comparison. You should know the basic relational operator like > more than, < lesser than, >= more or equal than, <= lesser or equal than, == equal operator, != not equal operator. With this, you should able to create your own trading strategy. Nevertheless, in this post, it only mentions how to create a single criteria check. How about you want to combine 2 criteria check? For example, I want to check both 5 moving average is > 13 moving average RSI price is < 70 You will learn this in next post. Stay tune. I wish you happy learning and success in creating your EA robot.
Before you start any programming, you should know the origin of MQL4. What You’ll Learn In This Post What is MQL4 What MQL4 can do or cannot do What are the MQL4 applications that can build MQL4 or known as MetaQuotes Languages 4 is a script based command written by MetaQuotes Software corporation. Below are the feature of MQL4: It is to help forex trader to create own robot to manage the trade, which includes create trade, modifying trade, and as well as closing the trade. MQL4 embeds all the features that you can used in MT4, such as indicators (moving average, RSI (relative strength indicator), MACD) It can also helps to automatically update MT4 objects such as (fibonacci, trend line and more) It inherits all the behaviors and limitations from MT4: Account lot size limitation, buy stop and sell stop limitation from the price, and buy limit and sell limit limitation from the price. MQL4 is just a set of English words to form a series of commands. Imagine that, you learn a set of hand gesture commands to command your dogs to stand up. In MQL4, MQL4 are the special commands that are written in English, a set of MT4 commands to order MT4 to work accordingly. Sound simple right ? You just need to learn the commands to give order to MT4. MQL5 is a accessor of MQL4. Nevertheless many of the Forex broker is still using MQL4 and it is popular among the forex broker due to stability. MQL4 has a similar concept of C++ programming which is using object oriented. It is easy to be used and written as all the syntax and functions are well defined and documented in the MQL4 documentation. Having says that if you are a C++ programmer, then this will be a piece of cake for you. You can go ahead to program your EA. Nevertheless, for most of the Forex traders, you have no programming background. Before you start program, you might have fierce to learn how to write. But I can guarantee you, MQL4 is not that hard to learn. You do not need to learn all the functions / syntaxs / object oriented in MQL4 feature. From my 10 years programming experience, you just need to know the core of the programming and learn what you need to do. For example, if you want to learn how to analyse the different symbols of the markets, then you should learn the indicators. On the other hand, if you want to learn how to create your on bot for trading, then you should learn the skill to create an EA (Expert advisor). In short, if assume that there is 100% of the knowledge of MQL4, but you just need to learn 50% of it than you are fine to create your own robot. You do not need to learn all the technical terms. Just to be keep in mind of it. Learn what you need. Same set of MQL4 commands can be built for 3 main purposes: Expert Advisor or EA or Robot Expert Advisor is a sets of commands that will check on your trading criteria on each of the price changes. In MQL4, the price changes is called Tick changes. On each tick changes, then your trading criteria will be checked if it is matched and perform actions. So, your task is to rovide a set of criteria on the EA in order to perform actions on your trade, i.e. open a new trades. Indicator For trader who want to write indicators to draw own lines and objects on the chart to ease the analysis work. Then indicator is the feature you should look into. Take an example of moving average, if you want to create own line like moving average with your own mathematic algorithm, then you should write your own indicator. Then indicator could be shown in the chart. Script Script is a one time used command execution. Unlike EA / Expert advisor, the checking will not be repeated on each price changes. Script will only be executed once on appended to the chart. In the scenario where you want to check all of the symbol for each of the day and generate report, then you should use script to help you execute the tasks. Or if you have a script to help you close all of the active trade in your list without any hassle to close it one by one, especially during the critical time, then you should use script. So these are the introduction of the MQL4 for a MQL4 newbie. Have a look in other post to learn and understand more on MQL4. I have described this in video as well
In my teaching style, I will teach you slowly and make sure you have strong understanding on the programming fundamental. Although I know that you will not need to be a MQL4 programming master, but at least you need to know how to do coding before jump into the MQL4 for coding. Spend a little bit more of times and read through this articles then you will know more on the fundamental of programming. In this article, you will learn What is program flow How a program flow How MQL4 program flow is In previous topic on introduction of MT4, it covers what is MQL4, and what can you do with MQL4. Before a newbie like you start to programming, there are few concepts that you must understand. That is the core of the programming. Programming is just like mathematic. It is very logic. If you are strong in mathematic, then programming is not a problem for you, however if you are weak in logic thinking, then you should think twice before continue on the journey of programming. First concept of programming is you should know the program flow. What is program flow? A program flow is the order of the program will be executed. There is a commander in all the programs. The commander will execute the task one by one. In an example, the commander in a country will be the president. President will give order what to do first, what to do next. So the program flow is the flow what will be executed first, what will be executed next. In MT4, the commander will be MT4 itself. MT4 will give controls to the applications (EAs/Indicators/Scripts) to execute tasks Then, next question, how the program will flow? A program flow is just like a water stream. Water will flow from a higher stream to lower stream. Same with programming, program flows from top task to bottom task. There is no way that water will flow from bottom stream to top stream, same like programming, there is no way that a program flow from bottom task and return back to the first task. In MQL4, the control will be passed over to the type of applications in different scenarios as below For an EA (Expert Advisor), the EA will be executed on every price changes / on tick. The control from the MT4 will be passed over to EA on each of the price changes. In MQL4, the price changes is called tick change. On each of the tick, then EA will be executed. This is the place all the criteria checking and the trade management will be taken in places. For an Indicator, the indicator will also be executed on every price changes. Nevertheless, it is based on the chart event changes. Unlike EA, indicator couldn’t trade. Indicator couldn’t initiate/modify or close a trade. Indicator will just act as the existing indicator in the MT4 to show lines, objects to indicate on status of the chart of the symbol. For a script, the script will only be executed once. It is neither depend on the price nor the chart event change. The script will be executed instantly when the script is dragged into the chart/ or the script is executed by double click on the script in the MT4. (I will show how to execute an EA in another topic) An example from the Hello World script above, you can see that I alert out 2 messages. First is “Hello World”, second is “I can code”. From the concept of water flow, “Hello World” will be executed first, then followed by “I can code”. Remember, code always be executed from top to bottom. Let’s look at the result of the script. As you can see from the popup screen in MT4, first message on “Hello World” is shown first, then second message “I can code” is shown next. Just take note that the popup screen appends new alert messages on the first line of popup dialog. Look at the timestamp, then it will be cleared. So, you have learned the program flow, you know how even know how to read the program now. You are about ready to start to do coding. 🙂 Stay tune for up coming free tutorial. Thank you for reading and happy coding!