Code Library / Trailing Stop

MQL4 Breakeven Stop

Move stop loss to breakeven after specified pips profit. Essential risk management.

Language: MQL4 · Category: Trailing Stop · Tags: breakeven, stop loss, MQL4, risk management, EA

Source code

void MoveToBreakeven(int ticket, double bePips, double lockPips) {
   if(OrderSelect(ticket, SELECT_BY_TICKET)) {
      double pt = MarketInfo(OrderSymbol(), MODE_POINT);
      if(OrderType()==OP_BUY && Bid-OrderOpenPrice()>=bePips*pt)
         if(OrderStopLoss()<OrderOpenPrice()+lockPips*pt)
            OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice()+lockPips*pt,OrderTakeProfit(),0);
      if(OrderType()==OP_SELL && OrderOpenPrice()-Ask>=bePips*pt)
         if(OrderStopLoss()>OrderOpenPrice()-lockPips*pt||OrderStopLoss()==0)
            OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice()-lockPips*pt,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