Code Library / Order Management

MQL4 Partial Close

Close 50% at TP1, trail the rest.

Language: MQL4 · Category: Order Management · Tags: partial close, MQL4, take profit, trailing, strategy

Source code

void PartialClose(int ticket,double tp1Pips,double pct) {
   if(!OrderSelect(ticket,SELECT_BY_TICKET)||OrderType()>OP_SELL) return;
   double pt=MarketInfo(OrderSymbol(),MODE_POINT);
   if(OrderType()==OP_BUY && Bid-OrderOpenPrice()>=tp1Pips*pt) {
      double cl=NormalizeDouble(OrderLots()*pct/100,2);
      cl=MathMax(cl,MarketInfo(OrderSymbol(),MODE_MINLOT));
      OrderClose(ticket,cl,Bid,3);
   }
}

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