MQL4 TUTORIAL BASICS – 8 HOW TO USE A WHILE LOOP

video

In this video we are going to find out how to use the “while” loop to execute a piece of code again and again.
To do that please click on the little button here or press F4 in your Metatrader 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: “SimpleWhileLoop”, I click on “Continue”, “Continue” and “Finish” and now you can delete everything that is above the “OnTick” function and let’s also remove the two comment lines here.
So for this simple example we will create a variable called: delay counter (DelayCounter), this is an integer value and we have another value, this is also an integer, the minimal value that we want to count until we continue with our program and that will be 10 million (10,000,000).
Now we want to use the “while” statement to check if our delay counter (DelayCounter) is below the minimum number (MinNumber) value and when that is true everything between these two curly brackets here will be executed and to show it we use the “Print” statement to output the text: “DelayCounter:” and the current value of the delay counter. Afterwards we increase the delay counter by 1 that’s done by using the statement: “DelayCounter” equals “DelayCounter” plus 1 and that’s about it.
So let’s click on “Compile”, we didn’t get any errors here and we also have no warning so now we can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, here we pick the new file: “SimpleWhileLoop.ex4”, let’s make that a little bit larger and enable the visual mode here and now you can start a test.
In the “Journal” tab we see that our counter is working, it’s executing the “while” loop and nothing else.
After the value of the delay counter has reached the 10 million mark our test is started, while you might noticed that also the output in the “Journey” is a little bit delayed and that is because printing out something is very time consuming, so once you are going to trade your own system please make sure that you don’t use “Print” too often because it will slow down the whole system.
That’s almost it, but there is a catch; if we take out this part and paste it below the “while” loop we can still compile the program, we still get no errors but when we start a new test our delay counter is never increased.
We have just created an infinite loop, nothing is happening, we don’t see any syntactical errors here but as this line is never able to increase the counter this expression here will stay true forever.
Infinite loops like this one can cause big confusion, let’s replace the “while” loop with an “if” statement that will be executed only once every tick and when I recompile the code, stop the current test and start a new one, our counter will continue to work but this time it will only be increased for each time when the price changes, it was much faster with the “while” loop, so let’s change that back but keep in mind that you need to put a piece of code inside of the “while” loop that is able to create a situation when this condition here becomes fails otherwise your “while” loop might run forever, so let’s recompile it one more time and this time it works as expected, we should approach 10 million in a few seconds, here we are and now you have learned how to create a simple “while” loop and you have coded it yourself with a few lines of MQL4 code.

Download “CODE - SIMPLE WHILE LOOP” SimpleWhileLoop.mq4 – Downloaded 9 times – 409 B