MQL5 TUTORIAL BASICS – 15 HOW TO PLACE A SIMPLE BUY STOP

video
play-sharp-fill

 

 

  1. Introduction to Creating a Buy Stop in MQL5 (00:00 – 00:25)
    • Introduction to the tutorial’s objective: placing a pending buy stop order above the current market price using MQL5.
  2. Opening MetaEditor and Creating a New File (00:25 – 00:45)
    • Instructions on opening MetaEditor and creating a new Expert Advisor file named “SimpleBuyStop”.
  3. Setting Up the Code Structure (00:45 – 00:52)
    • Steps to clean up the code by removing unnecessary parts above the OnTick function.
  4. Importing Trade.mqh and Creating an Instance of CTrade (00:52 – 01:07)
    • Importing the ‘Trade.mqh’ file and creating an instance of the CTrade class for trading operations.
  5. Calculating the Ask Price and Normalizing It (01:07 – 01:45)
    • Using ‘SymbolInfoDouble’ to calculate the Ask price and normalizing it with ‘_Digits’ for correct digit formatting.
  6. Checking for Open Orders and Positions (01:45 – 02:28)
    • Using ‘OrdersTotal’ and ‘PositionsTotal’ to check if there are no open orders or positions.
  7. Placing a Buy Stop Order (02:28 – 03:49)
    • Using ‘trade.BuyStop’ to place a buy stop order with parameters for lot size, price level, currency pair, stop loss, and take profit.
  8. Setting Expiration Time and Comment for the Order (03:49 – 04:19)
    • Configuring the expiration time and adding a comment to the buy stop order.
  9. Compiling and Testing the Expert Advisor in MetaTrader (04:19 – 04:53)
    • Compiling the code and testing the Expert Advisor in MetaTrader using the Strategy Tester.
  10. Observing the Buy Stop Order on the Chart (04:53 – 05:21)
  • Visualizing the buy stop order on the chart and confirming its placement and parameters.

 

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 95 times – 797.00 B