MQL5 PROGRAMMING – THE FOR LOOP

video
play-sharp-fill

In this video we want to have a look at the For Loop. Down here a loop is counting up until a value of 10,000 is reached. A For Loop is often used to count up or down certain values in a loop. And let’s take a look at how this can be done with MQL5. To do this click on this little button here in Metatrader or press the F4 key. That calls then here the Metaeditor. And here we choose File, New, Expert Advisor from Template, Next. I’ll give the name SimpleForLoop here once. Click on Next, Next and Finish. Now everything above the OnTick function can be deleted. And also the two comment lines are removed. We start by creating a counter. At first it should have the value one. It is an integer variable, because we only need integer values. Then we create a final value. In our case the final value should be 10,000. And in the OnTick function we use the For loop to count up from our start value to our end value. If we mark this command once and press the F1 key, we learn that the loop operator for consists of three expressions. If you have never seen this before, it can be a bit confusing. But it is quite simple. This first expression here is a starting value. In the second part, we check if a certain condition is met. In our case we want to check if the counter is even smaller than the final value. And in the third part our counter is incremented. This expression counter plus, plus means as much as: Add the value one to the current counter and then store the whole thing in the counter variable. And so that we can see something of our For Loop, we add the statement Print in two curly brackets here. This will give us a value in the journal. First we see the text: The counter is equal. And after that the determined value from the current counter variable. That was it so far. When you have all that, you can click on Compile or press the F7 key. We get a warning here, but for our simple example this should be enough. And if you don’t have any errors here, then click here above or press the F4 key to return to the Metatrader. And in the Metatrader we click on View, Strategy Tester or we press the key combination Ctrl+R. Here we now select the newly created file SimpleForLoop.ex5. Here we mark the visualization option and start our test. And here we can already see that our counter is incremented. The current value is incremented by one each time. And at 9.999 the whole thing stops here. So far, so good. But with the For Loop you can not only count up, but also count down. Let’s give the counter a value of 10,000 and let the final value be one. And from our counter value 10,000 we would like to count down as long as the counter is greater than our final value. And we subtract the value one each time. Let’s compile this again, switch back to the metatrader and start a new test. Then we see that this time is counted down. From 9.999 until our final value is reached. In our case this was one. The loop stops here at two to make expenditures. To change this, we could also say: Counter greater than or equal to final value. Let’s recompile that and run it again. Then this time our loop will also count down to one. There is one more special feature. It is also possible to specify values other than one here, for example ten. Let’s compile for the last time. Start a new test. Then our loop here will count down much faster. Because we have a distance of ten in between. And you have learned in this short video how you can use the For loop to count up or down values until a certain condition is reached. And you programmed that yourself, with these few lines, here in MQL5.