MQL5 TUTORIAL BASICS – 17 HOW TO PLACE A SELL STOP

video
play-sharp-fill

 

  1. Introduction to Setting a Sell Stop in MQL5 (00:00 – 00:11)
    • Introduction to the tutorial on setting a sell stop far below the current price using MQL5.
  2. Opening MetaEditor and Creating a New File (00:11 – 00:37)
    • Instructions on opening MetaEditor and creating a new Expert Advisor file named “SimpleSellStop”.
  3. Setting Up the Code Structure and Importing Trade.mqh (00:37 – 01:01)
    • Removing unnecessary code and importing the ‘Trade.mqh’ file for trading functions.
  4. Calculating the Bid Price and Normalizing It (01:01 – 01:26)
    • Using ‘SymbolInfoDouble’ to calculate the Bid price and normalizing it for currency pair precision.
  5. Checking for Open Orders and Positions (01:26 – 01:55)
    • Ensuring there are no open orders or positions before placing a sell stop.
  6. Placing a Sell Stop with trade.SellStop (01:55 – 03:19)
    • Using ‘trade.SellStop’ to place a sell stop order with parameters for lot size, price, and currency pair.
  7. Configuring Stop Loss and Take Profit Levels (03:19 – 03:30)
    • Setting take profit levels for the sell stop order and using ‘ORDER_TIME_GTC’ for order expiration.
  8. Setting Order Expiration and Adding a Comment (03:30 – 04:04)
    • Configuring the order expiration to ‘ORDER_TIME_GTC’ and adding a comment with ‘NULL’.
  9. Compiling the Code and Testing in MetaTrader (04:04 – 04:22)
    • Compiling the code and testing the Expert Advisor in MetaTrader using the Strategy Tester.
  10. Observing the Sell Stop Order on the Chart (04:22 – 04:55)
  • Watching the sell stop order placed on the chart and its details.

 

In this video we are going set a sell stop that is a pending order. You don’t see it right now but when we zoom into the chart you see that we have a pending order far below the current price, so let’s find out how to do that with MQL5.

We start by clicking on the little button here, you can also 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: “SimpleSellStop”, click on: “Continue“, “Continue“ and “Finish”. 

Now we can delete everything above the “OnTick” function and the two comment line here.
We are stating by importing the file: “Trade.mqh”, this one comes with MQL5 and it provides several trading functions, so let’s create an instance of “CTrade” that will be called: “trade” and we are going to use it later on to open our sell stop.

First we need to get the Bid price, that is done by using “SymbolInfoDouble” for the current symbol on the chart, we use “SYMBOL_BID” – all in capital letters – and I also like to use “NormalizeDouble” and “_Digits” that will give us the right number of digits behind the dot, in this case it would be 3 digits while other currency pairs have 5 digits and for this simple example I just want to check one thing and that’s if we have no open orders or positions, so we use “OrdersTotal” and if that equals 0 (zero) that means that we have no pending orders and “PositionsTotal” will deliver us the number of open positions and if that is also 0 (zero) we have no orders and no positions. An order is something that you place into the market and once it is executed it becomes a position.  

And now we want to use “trade.SellStop” to place our pending order, we have to pass several parameters, so let’s go through them one by one;
The first one here is for the lot size, we are going to open a pending order for 10 micro lot.
The second one is for the entry price, we want to enter as soon as the price is 200 points below the current Bid price – that’s where we place the pending order .
“_Symbol” will give us the currency pair that is on our chart, in my case it’s the Euro-US Dollar.
This parameter here could be used to set a stop loss, I dint use that one but I have a take profit, so when the price is 200 points below the current Bid price we will enter and when it goes to 400 points below the current Bid price we will take our profit.
“ORDER_TIME_GTC” stands for the expiration date, GTC means that “it is good till cancel”, so the order will not expire. You could specify that it is only valid for the current day (ORDER_TIME_DAY) and you  could also set a time and date (ORDER_TIME_SPECIFIED) but we don’t do that so the next parameter for the expiration time is also 0 (zero).
And this one is actually wrong, it would be correct to write “NULL” – all capital letters – because the last parameter is a string value, if you want to set a comment to identify the Expert Advisor on your chart you could use something like: “my comment” here and that’s about it.

So let’s click on the “Compile” button or press F7, we don’t see any errors here and we have no warning and if that is the case you can click on the little button here or press F4 to go back to Metatrader. 

In Metatrader you want to click on: “View/ Strategy Tester” or you can press CTRL and R. Inside of the “Strategy Tester” please pick the new file: “SimpleSellStop.ex5”, mark the visualization option here and start a test.

Here we are…
This is our pending order, here is our comment string so let’s zoom in and here is the pending order and in this little video you have learned how to place an automated sell stop with MQL5 and you have coded it yourself with a few lines of MQL5 code.

 

Download “CODE - SIMPLE SELL STOP”

SimpleSellStop.mq5 – Downloaded 76 times – 821.00 B