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.