MT4 Trailing Stop Expert

This is a function that you can use in your MT4 expert advisor to apply a standard points based trailing stop

For the MT5 verison click here

A trailing stop is nothing more than a stop loss that moves as price changes. For this standard trailing stop the rules are simple:

  • No stop loss is applied if the stop loss is worse than the trade opening price
  • The top loss only moves in a positive direction relative to the trade, staying within a maximum set number of points from the current price.

To use the trailing stop you will need to specify the points distance of the trailing stop from the current price. I captured this with an input.

input	int		InpTrailingStopPoints		=	500;		//	Trailing stop points

The input is in points but to use this ofr a trailing stop it must be converted to a price decimal type. I declared a global scope variable to hold the converted value and perform the conversion once in the initialisation section

double			StopLoss;
int OnInit() {
	StopLoss	=	SymbolInfoDouble(Symbol(), SYMBOL_POINT)*InpTrailingStopPoints;

In the OnTick section place a call to the ApplyTrailingStop function. This call passes all information needed by the function, although all of this information is available through global scope variables it is better practice to pass this information in the function call.

	ApplyTrailingStop(Symbol(), InpMagicNumber, StopLoss);

Then the ApplyTrailingStop function:

  • Calculate the actual price where buy or sell trailing stops will be placed using the stop loss amount and the appropriate close price.
  • Loop through all open trades
  • Check that the trade matches the symbol and magic number for the expert
  • Depending on buy or sell check that the rules above are met, the new stop loss is better than the opening price and the trade either has no current stop loss or the new stop loss is a better price than the existing stop loss
  • Finally just modify the trade to apply the new stop loss
void	ApplyTrailingStop(string symbol, int magicNumber, double stopLoss) {
	static int		digits	=	(int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
	//	Trailing from the close prices
	double	buyStopLoss		=	NormalizeDouble(SymbolInfoDouble(symbol, SYMBOL_BID)-stopLoss, digits);
	double	sellStopLoss	=	NormalizeDouble(SymbolInfoDouble(symbol, SYMBOL_ASK)+stopLoss, digits);;
	int		count				=	OrdersTotal();
	for (int i=count-1; i>=0; i--) {
		if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
			if (OrderSymbol()==symbol && OrderMagicNumber()==magicNumber) {
				if (OrderType()==ORDER_TYPE_BUY && buyStopLoss>OrderOpenPrice() && (OrderStopLoss()==0 || buyStopLoss>OrderStopLoss())) {
					if (OrderModify(OrderTicket(), OrderOpenPrice(), buyStopLoss, OrderTakeProfit(), OrderExpiration())) {}
				} else
				if (OrderType()==ORDER_TYPE_SELL && sellStopLoss<OrderOpenPrice() && (OrderStopLoss()==0 || sellStopLoss<OrderStopLoss())) {
					if (OrderModify(OrderTicket(), OrderOpenPrice(), sellStopLoss, OrderTakeProfit(), OrderExpiration())) {}

3 thoughts on “MT4 Trailing Stop Expert”

  1. After Creation as you type this part of code give a error -function not defind.
    ” CreateTrade(Symbol(), InpVolume, InpMagicNumber, InpTradeComment); ”
    any advise please

  2. I don’t have an answer to that unfortunately. I can add this to the proposed videos but not sure when it would get approval to go ahead.

  3. Mofo Setloboko

    Hi, I totally appreciate the tutorials, they have been very helpful, I only wish i could add the trailing stop to the MA crossover Expert. How soon can you post that one?

Leave a Comment

Scroll to Top