Code Library / Risk Management

Simple Trailing Stop

A basic trailing stop implementation for MQL4 Expert Advisors.

Language: MQL4 · Category: Risk Management · Tags: trailing stop, risk management, MQL4

Source code

void TrailingStop(int ticket, double trailPoints) {
   if(OrderSelect(ticket, SELECT_BY_TICKET)) {
      double point = MarketInfo(OrderSymbol(), MODE_POINT);
      if(OrderType() == OP_BUY) {
         double newSL = Bid - trailPoints * point;
         if(newSL > OrderStopLoss() && newSL > OrderOpenPrice())
            OrderModify(ticket, OrderOpenPrice(), newSL, OrderTakeProfit(), 0);
      }
   }
}

How to use this snippet

This is a building block, not a finished trading system. Paste it into your own MQL4 project, compile it, and test it on a demo account with your broker's real spreads before relying on it. If MetaEditor reports errors, the compilation error fixer can help.

Related snippets