In this video we want to find out how to check if a condition is true or false.
Download “CODE - SIMPLE IF CONDITION”
Obviously whenever you trade with an automated Expert Advisor like this one you need to find out if something is true or false and now we are going to find out how to do that with MQL4.
First please click on a little icon here or press F4 on your keyboard and now you should see the Metaeditor and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file “SimpleIfCondition”, click on “Continue”, “Continue” and “Finish”, and now you can delete everything above the “OnTick” function here.
Okay, we want to start by using two integer values; “int a should be 5” and “int b should be 3”, and if “a” is bigger than 5 I would like to use the “Comment” function to output the text: “a is greater than 5” otherwise we use the “else” statement – “else” is what is going to be executed whenever none of the “if” statements above is true – and in that case we want to have the output: “Conditions not true”, that’s about it!
Now we can click on the “Compile” button here or press F7 and the compiler says we don’t have any errors here, so now we can click on the little icon here or press F4 to go back to Metatrader.
In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, let’s pick our new file: “SimpleIfCondition.ex4”, please mark the visual mode here and start a test.
Here is our output: “Conditions not true” because this condition is not true, so let’s copy that, paste it here and this time we are going to check if “a” equals 5 and in that case we want to output: “a is equal to 5”.
Please remember a single equal sign will assign the value 5 to the variable “a” and two equal signs will check if the value 5 is equal to the variable “a”.
Okay, let’s go back to Metatrader and start another test, and this time our output is: “a equals 5” and that’s because this “if” statement is true, so let’s copy that and add another one.
This time I want to know of “b” equals 3, if that is true I want to have the text: “b equals 3” on my chart.
Let’s recompile, restart the test and yes, “b equals 3”, but I am seeing the output for the former condition where “a equals 5”, so what I also can do is I can use two “if” statements and if “a equals 5” and “b equals 3” I want to have the text: “a equals 5 and b equals 3” on my chart, so now MQL4 is going to check this expression and if that is true it is going to check the next expression, and only if both are true the code here is executed.
Let’s recompile, restart the test and now we get “a equals 5 and b equals 3”.
“if” only one way to check if something is true, there are more possibilities that we are going to check out in other videos.
If you use several lines behind the “if” statement, for example “int c could be a plus b” and I would like to have the output for c… I would get an error when I click on the “Compile” button because now MQL4 tells me that I’m using an illegal “else” statement without a matching “if”. That can be confusing but it just means we have to use curly braces here because we execute more than one line in our “if” statement, so let’s recompile and now the error is gone and this time we should get the output: “c equals 8”.
Okay, in this little video you have learned how to use the “if” statement to check if some condition is true or false and you have coded it yourself with a few lines of MQL4 code.
Download “CODE - SIMPLE IF CONDITION”SimpleIfCondition.mq4 – Downloaded 141 times – 461.00 B