MQL5 PROGRAMMING – HOW TO USE THE IF CONDITION

video
play-sharp-fill

This video is about the query of conditions. Whenever you act automatically, you have to check for certain conditions. For example, whether the stochastic is above or below this dotted line, or whether the price is moving above or below this moving average. And in this video we will see how to do that. To do this, we click on this small symbol in the Meta Trader up here or press the F4 key to open the Meta Editor. And here we click on File, New, Expert Advisor from template, Next. I’ll give the name simple If query here once. Click on Next, Next and Finish. Now we should see this framework here and everything above the OnTick function can be removed. The two comment lines are also deleted. And we start here with a simple condition. With two integer variables. A is 5 and b should be 3. And if a is greater than 5, then we would like the output a is greater than 5. Otherwise we use else. Output the text: no condition applies. There is still a t missing. When you are ready, click on compile above or press F7. This should work here without an error message. And in that case, we click on the small icon up here or press F4 to return to the Meta Trader. And in the Meta Trader we click on View, Strategy Tester or press the key combination CTRL+R. Here we now call the new program and start our test. We now get the output here: no condition applies. Let’s extend our query by copying and pasting this part here and ask if a is equal to 5. Now we compile again. Start a new test and this time we get the output a is equal to 5. Our Expert Advisor has checked the upper part. He found that this part does not apply and jumps to the next part. Let’s insert the whole thing here again and check if b has the value 3. Then in our next test we get the output b is equal to 3. The upper part here, our program doesn’t output any more. But what we can do now is, we just use another if statement. Nest this one. But we have to check for equality. And if that is true, then this text should be output. And that actually worked. You can nest such if queries multiple times and only if all if conditions are true, the statement will be executed. If we use more than one line after our if queries, then we have to use brackets. Because if we omit these brackets here and compile, then we get error messages here. Therefore it is basically a good idea to get used to using these brackets from the beginning. Then you can compile the code. And if we now start a new test, this time we get the output c is equal to 8. In this short video you have learned how to check certain conditions in the program with the if query. And you programmed this yourself with these few lines here in MQL5.