In this video we are talking about the “while” loop in MQL4; we used it here to open an order. Especially I want to talk about the difference between a “while” loop and an “if” statement because those things seem to be the same but there is a difference.
To create an Expert Advisor for our “while” loop please click on the little button here or press F4 on your keyboard.
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 remove everything above the “OnTick” function and let’s also delete the two comment lines.
Okay. That’s it.
First we want to find out if we have any open orders. That is done by checking if “OrdersTotal” equals zero and in that case we want to call a function called: “OpenBuyOrder.”
I’ll use a copy of the version that I have used for the Simple Buy Trade video so if you are interested in all the parameters you can check out that video. Right now it’s about “if” or “while” so let’s start with the “if” statement.
If “OrdersTotal” is zero we don’t have open orders and now we want to buy.
Let’s compile the code by clicking on the button here or pressing F7; that worked without any errors so now we can click here or press F4 to go back to Metatrader.
In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, in the Strategy Tester please select the: “SimpleWhileLoop.ex4” file, I will disable the visual mode here to speed things up and start a test.
This takes only a few seconds and now we made a loss of: 39 Dollars and 43 Cents by using the ”if” statement.
Now we simply replace the “if” statement with a “while” statement here, recompile the code and start another test. A few seconds later here is our result; it’s exactly the same, we lost 39 Dollars and 43 Cents.
Okay, so far so good. We don’t see any difference here.
Now let’s see what happens when we change the statement.
Now we want to find out if we have less than 10 orders. Let’s compile the code; we start the same test with the same parameters here, it takes a few more seconds this time but here is our result; we made a loss of: 394 Dollars and 29 Cents.
Let’s see what happens if I use the “if” statement, once again I recompile the code, start exactly the same test and what result would you expect?
It’s a total net profit of: 173 Dollars and 95 Cents.
Now how can it be possible that the “if” statement and the “while” statement do something different when the only thing we changed is the number of the orders?
To get the answer please mark the visual mode here, we will slow down the test and start a new one and you see that the orders are opened one after the other.
Let’s stop the test, once again we use “while” here, recompile, now we test again and this time you only see one line for the buy orders and you can’t read the text here because all those orders have been opened at exactly the same time.
And that’s the difference! The “while” statement will be executed 10 times until this condition is no longer true so it will open 10 buy orders at exactly the same time. The “if” statement will only be executed once each time we have a price change so it is executed only once until the next “OnTick” function is called when the price changes again.
Over time this makes a big difference in the result and now you know the difference between the “while and the “if” statement and how to create an Expert Advisor that is using the “while” loop and you have coded it yourself with a few lines of MQL4 code.