MQL5 TUTORIAL BASICS – 8 HOW TO USE THE WHILE LOOP

video
play-sharp-fill

In this video we are going to find out how to use the “while” statement to wait until something is happening like now; we have waited until the delay counter reached 500,000, so let’s find out how to use the “while” loop in MQL5.

To do that please click on the little button here or press F4 in your Metatrader, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleWhileLoop”, now I click on “Continue”, “Continue” and “Finish”.

We can delete everything above the “OnTick” function and the two comment lines here.

So let’s start by creating a delay counter (DelayCounter), we will use an integer variable because we only need whole numbers, we start with the value of 1 and we are going to increase the delay counter in the “OnTick” function, let’s say we have a minimum value of 500,000 and only if our delay counter is above our minimum value of 500,000 we want to do something. 

Now we can use the “while” statement within the “OnTick” function to check if our delay counter is still below our minimal value and if that is the case we want to do something, first we want to create an output that will show us the text: “DelayCounter:” followed by the calculated value of the current delay counter and afterwards we are going to increase the delay counter by the value of 1 that is done by using “DelayCounter” equals “DelayCounter” plus 1, so let’s compile the code here, this should work without any errors and now we click on the little button here or press F4 to go back to Metatrader.

Let’s open the Strategy Tester by clicking on: “View/ Strategy Tester” or you could also press CTRL and R, here we pick the new file: “SimpleWhileLoop.ex5”, please enable the visualization mode here and start your test.

And now you see that our counter is working and when it reaches 500,000 this is going to move so everything is fine, but there is a catch; let’s say you would like to increase the delay counter outside the while loop, we still can compile the code without any errors but when we start the next test nothing happens. The whole Strategy Tester is frozen, so why is that?

If you highlight the “while” statement and press F1 you should see the “Help” file but nothing happens because everything still is frozen and that’s because we have created an endless loop here. When the first tick comes in and this expression here is true the “while” statement will cause an infinite loop that will never end! We wanted to increase the delay counter here but as we are doing it outside the “while” loop this is never going to happen and so this expression will be true forever and this is one of the reasons why I don’t like to use “while” very often, you can check most of the conditions when you use “if”, so let’s recompile the code here, restart a test and this time we see that our delay counter is working because the “if” statement will only be executed once every time a tick comes in while the “while” statement would run forever.

Okay, so if you would like to use the “while” statement you need to make sure that inside of your “while” loop something is going to interrupt the execution or else your “while” loop would run forever.

So let’s restart the test and this time it counts like it is supposed to do and in this little video you have learned how to use the “while” statement and how to avoid never-ending loops and you have coded it yourself with a few lines of MQL5 code.

Download “CODE - SIMPLE WHILE LOOP”

SimpleWhileLoop.mq5 – Downloaded 58 times – 400.00 B