
In this video we are going to talk about a buy stop. You don’t see it here but when you zoom into the chart you will see that we have placed a pending order above the current market price, it will be executed when the price crosses this green line here, so let’s find out how to do that with and MQL5.
Please click on the little button here or press F4 on your keyboard. Now you should see the Metaeditor window and here we click on: “File/ New/ Expert Advisor (template)” from template, “Continue“, I will call this file: “SimpleBuyStop”, click on: “Continue“, “Continue“ and “Finish”.
We remove everything that is above the “OnTick” function and let’s also delete the two comment line. First we need to import the file: “Trade.mqh”, this one comes with MQL5, it contains trading functions and now we use it to create an instance of the class “CTrade” that will be called: “trade” and we are going to use it to open our buy stop later on.
To open a buy stop we need to know the Ask price that is calculated by using “SymbolInfoDouble” for the current symbol, the second parameter is “SYMBOL_ASK” to get the Ask price. According to the Reference “SYMBOL_ASK” will give us the best buy offer. I like to use “NormalizeDouble” and “_Digits” to get the Ask price with either five digits or three digits depending on the currency pair I use.
Let’s add a simple condition here; I want to check if we have no open orders and no open positions. “OrdersTotal” will deliver the number of orders, an order is something you place into the market that can become a position and “PositionsTotal” will deliver the number of existing positions where those orders have been executed, this one is still an order because it wasn’t executed so far but once it reaches the price that we have defined the order will become a position.
Okay. So if my conditions are true I want to place a buy stop and here is how we do that; we use “trade.BuyStop” with a few parameters;
The first parameter is for the lot size, the second one is for the price level. We want to buy when the price rises 200 points from the current Ask price.
This third parameter is for the currency pair and “_Symbol” will deliver the current currency pair on the chart, in my case it’s the Australian against the Canadian Dollar.
This one would be for the stop loss, I did not set a stop loss here, and it could be something like 100 points above the Ask price. I have defined a take profit and I would like to close the position for a profit when the price rises 400 points from the current Ask price that would be 200 points profit because the entry is executed 200 points above the Ask price.
The next parameter is the date when we would like to close the pending order. ”ORDER_TIME_GTC” stands for “good until we cancel the order”, “ORDER_TIME_DAY” would be “good until the day is over” and you could also specify (ORDER_TIME_SPECIFIED) a time and date, in that case you would use the next parameter to set an expiration time, we don’t do that.
The last parameter would be a comment, I don’t use one so I use “NULL”, all capital letters here. Actually I could also change that; let’s say: “My comment”.
Actually we are done, so please click on the “Compile” button and if you don’t get any errors you can click here or press F4 to go back to Metatrader.
And in Metatrader please click on: “View/ Strategy Tester” or press CTRL and R, now you should see the “Strategy Tester” panel and here you want to pick the file: “SimpleBuyStop.ex5”, please enable the visualization here and start a test.
And this is what it should look like, here is actually our new comment, the buy stop was placed but it wasn’t executed so far, it is for 10 micro lot, here is the price that we have defined and the take profit actually is 200 points above the entry price and if you zoom into the chart you will see the buy stop is here, so our little Expert Advisor is working and in this little video you have learned how to automatically place a buy stop above the current Ask price and you have coded it yourself with a few lines of MQL5 code.
Download “CODE - SIMPLE BUY STOP”
SimpleBuyStop.mq5 – Downloaded 88 times – 797.00 B