MQL4 TUTORIAL BASICS – 9 HOW TO USE A FOR LOOP

In this video we are going to talk about the “for” loop, this “counter” is increased within a “for” loop, basically a “for” loop is used to start at a certain value and count until another value is reached and in this video we are going to find out how to do that.
So please click on the little icon here or press F4 on your keyboard and now you should see the Metaeditor window and here we want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleForLoop”, now I click on “Continue”, “Continue” and “Finish”. Let’s delete everything above the “OnTick” function and the two comment lines here.
First we need to create a start value, I will call this one: “counter”, it’s an integer variable (int) and in the initialization we will assign the value: 1 here, let’s also create an end value (endvalue) – in our case we want the “counter” to count until the value: 10,000 is reached – and inside of the “OnTick” function we are going to use the “for” loop, it has three expressions.
If you have never seen a “for” loop before it might look a little bit weird but it’s not so hard to understand. The first parameter here is the starting value, that’s our “counter”, in our case the “counter” is 1. The second parameter is the expression that should be checked, so if the condition: “counter is below endvalue” is true the loop will continue, and the third parameter here is what should happen after this condition has been checked. In our case we are going to increase the “counter” by 1.
“counter++” means that we take the current value of the “counter” and add 1. We could also use this kind of expression (counter=counter+1) and within two curly braces we want to do something; in our case we use the “Print” statement to output the words: ”the counter equals” and the current value of the “counter”.
So let’s compile the code and if you don’t see any errors here you can click on the little symbol here or press F4 to go back to Metatrader and in Metatrader we want to click on: “View/ Strategy Tester” or we could press CTRL and R, here we pick the new file: “SimpleForLoop.ex4”, please enable the visualization here and start a test.
And now we see that our “counter” is working, it’s counting up until it reaches 9,999 and the “for” loop is also able to count down, so actually we could switch the values here, now the starting value of our “counter” is 10,000, the “endvalue” is 1 and this time we want to count down from the “counter” value 10,000 as long as the “counter is bigger than the endvalue” and we want to subtract 1 for each time we count.
So let’s recompile the code, go back to Metatrader and start a new test and this time we have a countdown. The “counter” will be diminished until this condition is no longer true; our “endvalue” is 1 that’s why the last entry here says that: “Counter equals 2”.
We could also use the expression: “is greater or equals endvalue” (counter>=endvaue), now recompile, start a new test and this time it should say: “the last counter equals 1”.
What’s interesting is that we could also take other values here, for example we could diminish the “counter” by 10, let’s recompile that one and this time our “counter” will be diminished by the factor of 10 and now you know how to use a “for” loop, a “counter” and an “endvalue” to count upwards or downwards and you have coded it yourself with a few lines of MQL4 code.