Code Library / Trailing Stop

MQL4 Step Trailing Stop

Step-based trailing stop moving SL in fixed increments for volatile markets.

Language: MQL4 · Category: Trailing Stop · Tags: step trailing, MQL4, trailing stop, volatility, stable

Source code

void StepTrailingStop(int ticket, double stepPips, double trailPips) {
   if(OrderSelect(ticket, SELECT_BY_TICKET)) {
      double pt=MarketInfo(OrderSymbol(),MODE_POINT);
      double step=stepPips*pt, trail=trailPips*pt;
      if(OrderType()==OP_BUY && Bid-OrderOpenPrice()>=trail) {
         double newSL=MathFloor((Bid-trail)/step)*step;
         if(newSL>OrderStopLoss()+step/2)
            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