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

For the MT4 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 enter or modify trades in MT5 I use the CTrade class which is supplied with MT5. Include the Trade.mqh file and declare a global scope variable of the CTrade type

#include <Trade/Trade.mqh>
CTrade	Trade;

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;
	return(INIT_SUCCEEDED);
}

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				=	PositionsTotal();
	for (int i=count-1; i>=0; i--) {
		ulong ticket	=	PositionGetTicket(i);
		if (ticket>0) {
			if (PositionGetString(POSITION_SYMBOL)==symbol && PositionGetInteger(POSITION_MAGIC)==magicNumber) {
				if (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY && buyStopLoss>PositionGetDouble(POSITION_PRICE_OPEN) && (PositionGetDouble(POSITION_SL)==0 || buyStopLoss>PositionGetDouble(POSITION_SL))) {
					Trade.PositionModify(ticket, buyStopLoss, PositionGetDouble(POSITION_TP));
				} else
				if (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL && sellStopLoss<PositionGetDouble(POSITION_PRICE_OPEN) && (PositionGetDouble(POSITION_SL)==0 || sellStopLoss<PositionGetDouble(POSITION_SL))) {
					Trade.PositionModify(ticket, sellStopLoss, PositionGetDouble(POSITION_TP));
				}
			}
		}
	}
	
}

This Post Has 5 Comments

  1. Samit Dar

    It is giving me compile error –

    #include
    CTrade Trade;

    input int InpTrailingStopPoints = 500; // Trailing stop points

    input int InpMagicNumber = 212121;
    input string InpTradeComment = “just testing”;
    input double InpVolume = 0.01;

    double StopLoss;
    int OnInit()
    {

    StopLoss = SymbolInfoDouble(Symbol(), SYMBOL_POINT)*InpTrailingStopPoints;

    Trade.SetExpertMagicNumber(InpMagicNumber);

    //—

    //—
    return(INIT_SUCCEEDED);
    }
    //+——————————————————————+
    //| Expert deinitialization function |
    //+——————————————————————+
    void OnDeinit(const int reason)
    {
    //—

    }
    //+——————————————————————+
    //| Expert tick function |
    //+——————————————————————+
    void OnTick() {

    createTrades(Symbol(),InpVolume,InpMagicNumber,InpTradeComment);

    ApplyTrailingStop(Symbol(),InpMagicNumber,StopLoss);

    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 = PositionsTotal();
    for (int i=count-1; i>0; i–1) {
    ulong ticket = PositionGetTicket(i);
    if (ticket>0) {

    if (positionGetString(POSITION_SYMBOL)==symbol
    && PositionGetInteger(POSITION_MAGIC)==magicNumber){

    if (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY
    && buyStopLoss>PositionGetDouble(POSITION_PRICE_OPEN)
    && (PositionGetDouble(POSITION_SL)==0 || buyStopLoss>PositionGetDouble(POSITION_SL))) {
    Trade.PositionModify(ticket, buyStopLoss, PositionGetDouble(POSITION_TP));
    } else
    if (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL
    && sellStopLoss<PositionGetDouble(POSITION_PRICE_OPEN)
    && (PositionGetDouble(POSITION_SL)==0 || sellStopLoss=0; i–) {
    ulong ticket = PositionGetTicket(i);
    if (ticket>0) {
    if (PositionGetString(POSITION_SYMBOL)==symbol && PositionGetInteger(POSITION_MAGIC)==magicNumber) {
    if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY) buyCount++;
    if (positionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL) sellCount++;
    }

    }

    }

    if (buycount==0) {
    if (Trade.positionOpen(symbol, ORDER_TYPE_BUY, volume, SymbolInfoDouble(symbol, SYMBOL_ASK),0,0,tradecomment)){}
    }
    if (sellcount==0) {
    if (Trade.PositionOpen(symbol,ORDER_TYPE_SELL, volume, SymbolInfoDouble(symbol, SYMBOL_BID),0,0,tradecomment)){}
    }

    }

    1. ajm

      You don’t say what the error is but your #include line is missing a file name

  2. Samit Dar

    Thanks – it does include
    #include
    CTrade Trade;

    The errors are below –
    ‘}’ – unexpected end of program Trailingstop.mq5 115 14
    ‘{‘ – unbalanced parentheses Trailingstop.mq5 47 15
    2 errors, 0 warnings 3 1

    1. ajm

      Then that is the answer. You have a missing } or an extra { somewhere. Maybe the other way around. The editor can highlight matching {} to help tracking down the problem.

Leave a Reply