MQL4 TUTORIAL BASICS - 26 HOW TO CODE A SIMPLE BUY TRAILING STOP

Опубликовано: 01 Январь 1970
на канале: MQL4Tutorial
34,500
445

https://mql4tutorial.com/?s=stop

With MQL4 for Metatrader4 we create an automated Expert Advisor to set a trailing stop for buy trades. This EA will adjust the trailing stop for all buy positions on the Forex Chat that belong to the current currency pair...

In this video, we are going to create a trailing stop that will rise whenever the price rises, so let's find out how to calculate something like this in MQL4.

To do that please click on the little button here or 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: “SimpleBuyTrailingStop”, click on “Continue”, “Continue” and “Finish”.

Now we can delete everything above the “OnTick” function and the two comment lines here.

First, we want to check if “OrdersTotal” is below 1, if that is the case we have no open orders and for this example, we are going to open a demo buy trade because we need something for the trailing stop so we use “OrderSend” to open a 10 micro lot trade here.

You wouldn't do that on a real account without any entry signal and because it is possible to have more than one position we now want to count all the orders and we use “OrdersTotal” and a “for” loop to go through each and every order that we have.

First we want to check if we can select an order for the current counter in the loop, so we use “OrderSelect”, the counter number, we want to select by position (SELECT_BY_POS), we use “MODE_TRADES”, that's for currently open positions and for orders, if you would like to see closed orders and positions you could also use “MODE_HISTORY”.

Now we need to find out if the symbol for the order (OrderSymbol) equals the current symbol (Symbol()) on the chart, afterwards we use “OrderType” to find out if it's a buy position (OP_BUY) and if all these here is true we check if the current order stop loss (OrderStopLoss) is below 150 points and if this is true we use “OrderModify” for the current order (OrderTicket) with the order open price (OrderOpenPrice) and we set the stop loss to 150 points below the Ask price, we leave the order take profit value (OrderTakeProfit) as it is, we don't need an expiration date (0) and we don't need to use any color (CLR_NONE).

That’s about it.

Please don't forget to close the braces here.

Here we close the “for” loop.

Well, that might be a little bit complicated and if it was too fast for you or if you don't understand what all the code here does maybe you want to take part in the premium course or maybe you want to watch the other videos in this basic video series. For now please click on the “Compile” button, we have one warning here but no errors and if that is the case please click on the little button here or press F4 to go back to Metatrader.

In Metatrader we use an empty chart, right click, select “Template/ Save Template” and save it as “tester.tpl”. You can replace the current one and we do it to have a clean chart because we don't need any Indicators for this Trailing Stop Expert Advisor.

If you don't see the Strategy Tester here, please click on “View/ Strategy Tester” or CTRL and R, pick the new file: “SimpleBuyTrailingStop.ex4”, mark the visual mode here and start a test.

And here is our first position, this is the trailing stop and as the price rises the trailing stop will follow and in this little video you have learned how to automate a trailing stop for a buy position and you have coded it yourself with a few lines of MQL4 code.