Trailing Stop in MQL4
This post contains affiliate links. If you use these links to register at one of the trusted brokers, I may earn a commission. This helps me to create more free content for you. Thanks!
Trailing Stop is a widely used function among a lot of traders. MT4 has a built-in Trailing Stop function that can be manually set, as shown below. By right-clicking on any open position in MT4 Terminal, a menu will pop up.
When working with automated strategies, it is necessary to create a custom function to care about the Trailing Stop. Here is an example how the function may look like:
// Trailing Stop
void TrailingStop(int TrailingOffsetPoints) {
// Iterate over all the trades beginning from the last one to prevent reindexing
for (int i = OrdersTotal() - 1; i >= 0; i--) {
// Select trade by its position and check Magic Number
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == UniqueStrategyNumber) {
// Adjust the stoploss if the offset is exceeded
if ((OrderType() == OP_BUY) && (NormPrice(Bid - OrderStopLoss()) > NormPrice(TrailingOffsetPoints * Point))) {
if (OrderModify(OrderTicket(), OrderOpenPrice(), NormPrice(Bid - TrailingOffsetPoints * Point), OrderTakeProfit(), OrderExpiration(), clrNONE) == false) {
Alert("Cannot modify the SL due to: ", ErrorDescription(GetLastError()));
}
} else if ((OrderType() == OP_SELL) && (NormPrice(OrderStopLoss() - Ask) > NormPrice(TrailingOffsetPoints * Point))) {
if (OrderModify(OrderTicket(), OrderOpenPrice(), NormPrice(Ask + TrailingOffsetPoints * Point), OrderTakeProfit(), OrderExpiration(), clrNONE) == false) {
Alert("Cannot modify the SL due to: ", ErrorDescription(GetLastError()));
}
}
}
}
}
// Normalize the Price value
double NormPrice(double Price) {
return NormalizeDouble(Price, Digits);
}
To better understand how the trades are correctly processed, see the post: Basics of Trade Management in MQL4. Here we will focus just on the part where the Stop Loss is adjusted.
The first task is to separate the trades based on their orientation. After that, we need to check the price difference from the currently set Stop Loss. If the difference is greater than the preset Trailing Stop offset, the Stop Loss is adjusted. If the difference is smaller or equal, the Stop Loss remains on the same level. Below are the conditions needed for the Stop Loss to be adjusted:
if (OrderType() == OP_BUY && Bid - OrderStopLoss()) > TrailingOffsetPoints * Point)
if (OrderType() == OP_SELL && OrderStopLoss() - Ask > TrailingOffsetPoints * Point)
To properly modify the SL, it is needed to use the OrderModify() function and set a new Stop Loss price. You can see that all the other trade parameters (OrderTicket(), OrderOpenPrice(), OrderTakeProfit(), OrderExpiration()) remain unchanged. For more information about the built-in trade functions, see the list here.
OrderModify(OrderTicket(), OrderOpenPrice(), NewSLPrice, OrderTakeProfit(), OrderExpiration(), clrNONE)
Here is the behaviour of the Trailing Stop viusualized:
You can see that the Stop Loss is modified only if the market price goes in favor of your trade. This means you are locking the profits until there is a correction in the opposite direction. If the correction is greater than the Trailing Stop Offset, the Stop Loss will be hit.
If you experienced any problems with the Trailing Stop or have some practical feedback, let me know in the comments.
Still, have no trading account yet? Open an account at one of my trusted brokers suitable for algorithmic trading completely for free and start testing today!
This post contains affiliate links. If you use these links to register at one of the trusted brokers, I may earn a commission. This helps me to create more free content for you. Thanks!
Thank you for the insights, helped a lot.. Btw, just found this book on amazon, make sure all of you will check it out: Forex Brokers Unmasked: The Truth Behind Deceptive Tactics
Short, sweet, to the point, FREE-exactly as information should be!
I really like the design and contents of your web page
Everything is very open with a really clear explanation of the issues. It was definitely informative. Your site is extremely helpful. Thank you for sharing.
You have made some good points there. Tracey
My brother recommended I might like this website. He was totally right.
This post actually made my day. You can not imagine simply how much
time I had spent for this information! Thanks!
I do believe all of the ideas you have presented to your post.
They’re really convincing and will definitely work.
Nonetheless, the posts are very brief for newbies. Could you please extend them a
bit from next time? Thanks for the post.
Surely, I will cover the absolute basics in more detail soon. Stay tuned!
Hi there friends, its impressive piece of writing
about education and fully defined, keep it up all the time.
I will right away seize your rss as I can’t find your
e-mail subscription hyperlink or newsletter service. Do you
have any? Please allow me realize in order that I could subscribe.
Thanks.
It’s nearly impossible to find experienced people for this subject, however,
you sound like you know what you’re talking about!
Thanks