MQL5 TUTORIAL BASICS – 14 HOW TO PLACE A BUY LIMIT

video

In this video we want to find out of how to set a buy limit, right now you don’t see it on the chart, but if you zoom in you can see that we have this buy limit here below the current price and now we are going to find out how to do that with MQL5.
To do that 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: “SimpleBuyLimit”, click on “Continue”, “Continue” and “Finish“.
Now we can delete everything above the “OnTick” function, lets also remove the two comment lines, we start by importing the file: “Trade.mqh”. This file comes with MQL5 and it includes a few trading functions and we used it to create an instance of “CTrade” that will be called: “trade” and we are going to use it later on to open our buy limit.
Inside of the “OnTick” function we start by getting the Ask price that is done by using “SymbolInfoDouble” for the current symbol on the chart we use “SYMBOL_ASK” all in capital letters and I like to use “NormalizeDouble” and “_Digits” to automatically calculate the right number of digits behind the dot because some currency pairs only have three digits behind the dot while others have five digits.
For this simple example we are going to only check if we have open orders or open positions. In MQL5 we do that by using two different functions; the first one is “OrdersTotal”, that one will deliver the number of open orders that could become a position, the second one is for the positions (PositionsTotal) that we have created and only if both conditions are true we are going to set our buy limit. That’s done by using: “trade.BuyLimit”, we need to pass a few parameters here in round braces.
I always do both braces at the same time so I don’t forget one and if you mark one of the braces here you will see that the other one is highlighted in grey, that’s a good way to find out if you have any errors here.
The first parameter is for the lot size, we want to set the buy limit for a 10 micro lot trade.
Parameter 2 is used to tell Metatrader the price for the buy limit, we would like to get the current Ask price and place our buy limit 200 points below the Ask price, we use “_Symbol” to automatically get the symbol on the chart, the next parameter would be for the stop loss, I don’t use it here but if you would like to use it you can say something like: Ask minus 400 points, but I do use a value for the take profit. I would like to take the profit when the price rises 200 points above the current Ask price.
The next parameter: “ORDER_TIME_GTC” all in capital letters, is used for the expiration date, “GTC” stands for: “Good to cancel” – we don’t have any expiration date here – so let’s set the next parameter to 0 (zero), that would be the expiration time.
Finally we have one last parameter, which is set to “NULL” all capital letters, we could use it to pass a comment, which might be helpful if you are going to use several Expert Advisors on one and the same chart, but for this simple example that’s it.
Let’s click on the “Compile” button or press F7…
Ooh… we missed a semicolon that needs to be placed behind the closing round brace here because you could write all these code lines here in a single line and the semicolon is the end of the statement, so let’s recompile and this time it worked without any errors and we also don’t have any warnings 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, please pick the new file: “SimpleBuyLimit.ex5”, enable the visualization here and start a test.
And now you should see that we have an entry for a buy limit for 10 micro lots and if you click on the chart and press the minus key on your keyboard you should see the buy limit here, let’s speed the process up and we should see a buy trade.
Right here our buy limit was executed, we now have a buy trade, it wasn’t executed here, that’s because of the spread and now you know how to create an Expert Advisor to place an automated buy limit below the current Ask price and you have coded it yourself with a few lines of MQl5 code.

Download “CODE - SIMPLE BUY LIMIT” SimpleBuyLimit.mq5 – Downloaded 4 times – 840 B