Trailing Stop in MQL4

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.

Activate Custom Trailing Stop

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!