Code Library / Order Management

MQL4 Pending Order Manager

Place and manage pending orders with expiration.

Language: MQL4 · Category: Order Management · Tags: pending orders, MQL4, buy stop, sell limit, order management

Source code

int PlaceBuyStop(double price,double sl,double tp,double lots,int expH) {
   datetime exp=expH>0?TimeCurrent()+expH*3600:0;
   return OrderSend(Symbol(),OP_BUYSTOP,lots,price,3,sl,tp,"BS",0,exp);
}
void CleanExpired(int maxH) {
   for(int i=OrdersTotal()-1;i>=0;i--)
      if(OrderSelect(i,SELECT_BY_POS)&&OrderType()>OP_SELL)
         if(TimeCurrent()-OrderOpenTime()>maxH*3600) OrderDelete(OrderTicket());
}

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