MQL5 TUTORIAL – SIMPLE WHILE LOOP

video
play-sharp-fill

In this video we are going to talk about the “while” statement. Actually “while” is a loop, while “if” is a statement, both are very similar but we want to find out what the differences are and how to create an Expert Advisor that is going to use the “while” or the “if” statement to open a trade.
To do that please click on the little button here or press F4 on your keyboard and 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”, click on ”Continue”, ”Continue” and “Finish” and now you can delete everything above the “OnTick” function and let’s also remove the two comment lines here.
First we need to create an instance of “CTrade” so we include the file: “Trade.mqh” and create an instance of “CTrade” that will be called: “trade” and now we need to check if we have any open positions, that’s done by using: “PositionsTotal” and we want to know if “PositionsTotal” is equal to zero and in that case we want to open a buy order so let’s create a function that will be called: “OpenBuyOrder.”
First we need to get the Ask price that is done by using: “SymbolInfoDouble” and we use “NormalizeDouble” and “_Digits” to get the right amount of digits behind the dot because as you can see here we have currency pairs with 5 digits behind the dot and this one only has 3 digits behind the dot and “_Digits” stores the number of digits after the decimal point for the current symbol on the chart and the Ask price for that symbol and the result will be stored in the variable called: “Ask”.
Now that we know the Ask price we want to buy 10 micro lot by using “trade.Buy”, if you want to understand all the parameter here just take a look at the video called: Simple Buy Trade. In our case it’s about the “while” statement here so now we close the second function here and click on the “Compile” button or press F7; that went very well so we can now click on a little button here or press F4 to go back to Metatrader.
In Metatrader we click on “View/ Strategy Tester” or press CTRL + R, please select the file: “SimpleWhileLoop.ex5” that we have just created; I will disable the visualization here, pick the whole year 2017 and start a test.
…and here is our result, we made a net profit of: 1.080 4 Dollars and 59 Cents, let’s replace the “while” statement with an “if” statement, recompile the code, go back to Metatrader and restart the same test again. This will take a few seconds, here is our result: once again we made a profit 1.084 Dollars and 59 Cents but now we are going to change the statement here.
We want to find out if we have less than 10 open positions, compile the code once again, restart the test and this time we made a net profit of: 10.872 Dollars with the “if” statement and now we are going to change that and use the “while loop”, recompile again and start the test, and in this case we made a net profit of: 10.845 Dollars and 90 Cents, but what’s the reason?
We only changed the amount of positions to check. To find the answer please mark the visualization button here and start a new test and you will see that we only see one line here because the “while” statement opened all the positions here at exactly the same time. The reason is that within the “OnTick” function the “while” statement will run as long as this expression here is true while the “if” statement behaves differently so let’s do a quick compilation here, stop the test for the last time and now you will see that only one position is opened after the other, so we have opened buy positions at different price levels here and over time this makes a big difference.
Okay, in this video you have learned how to open positions by using the “while” statement and you have coded it yourself with a few lines of MQL5 code.

Download “MQL5 TUTORIAL - SIMPLE WHILE LOOP”

SimpleWhileLoop.txt – Downloaded 368 times – 423.00 B