MQL4 TUTORIAL BASICS – 27 HOW TO CODE A SIMPLE SELL TRAILING STOP

video

In this video, we want to create a trailing stop for sell trades, you can see here the red line is the railing stop and when the price falls the trailing stop will follow, so let’s find out how to code something like this in MQL4.
To do that please click on the little button here or press F4 in your Metatrader, 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: “SimpleSellTrailingStop”, click on “Continue”, “Continue” and “Finish”.
Now we can delete everything that is above the “OnTick” function and let’s also remove the two comment lines here.
Inside of the “OnTick” function we first want to check if we have no open orders that is done by checking if the function “OrdersTotal” delivers a value below 1, and if this is the case we want to open a demo sell trade, so let’s actually call that “sellticket” because if we don’t have open positions we will not find out if the trailing stop works.
Obviously, you wouldn’t do that on a real account without any entry signal, so let’s use “OrderSend” to open a demo trade (OP_SELL) for 10 micro lot.
Now we want to go through all the open orders, we use “OrdersTotal” to give us the current number of orders and we use a “for” loop to count down from the number of orders we have.
Inside of the “for” loop we use “OrderSelect”, the number of the current order, we want to select by position (SELECT_BY_POS) and we use “MODE_TRADES” as we want to find out everything about the open orders. In our case we want to check if the order belongs to the current chart, that is done by comparing the return value for the function “OrderSymbol” with the return value of the function “Symbol()” and I would also like to find out if the current order type equals “OP_SELL” because this is what we need to find the sell orders.
In the next step we need to find out if the order stop loss (OrderStopLoss) equals 0 (zero) or if the order stop loss (OrderStopLoss) is too high and is bigger than the current Bid price plus 150 points, and if this is the case we use “OrderModify” for the current ticket number (OrderTicket) that has the order open price (OrderOpenPrice), we now want to set the current stop loss to 150 points above the Bid price, the order take profit value (OrderTakeProfit) will not be changed, we don’t need any expiration date (0) and we don’t need a color (CLR_NONE).
Please don’t forget to close all the braces here and that’s about it.
If this is too complicated for you right now you maybe want to watch the other videos in this basic series or if you are interested you could also become a member of the premium course.
For now please click on the “Compile” button here or press F7, we get one warning that we should check the “OrderModify” return value but for this simple example we don’t need to do that so if you don’t have any errors here you can now click on the little button here or press F4 to go back to Metatrader.
In Metatrader we just pick a clean chart, right-click into the chart, select “Template/ Save Template” and we save it as “tester.tpl” as this is the template that is going to be used in the Strategy Tester. You can override the current one and when you are done you can click on “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleSellTrailingStop.ex4”, mark the option for the visual mode here and start a test.
And here we are, this is our sell trade, and we should see the trailing stop coming down.
There it is! And in this little video, you have learned how to create an automated trailing stop for sell trades and you have coded it yourself with a few lines of MQL4 code.

MQL4 Tutorial searches: