Check pending order existence

 
How can I check if there exists a pending order (in my case, a Buy Stop order) for a specific symbol?
 
 
Pablo Rego:
How can I check if there exists a pending order (in my case, a Buy Stop order) for a specific symbol?
 

You can start by reading the documentation about how to work with orders : https://www.mql5.com/en/articles/211

Orders, Positions and Deals in MetaTrader 5
Orders, Positions and Deals in MetaTrader 5
  • 2011.02.01
  • MetaQuotes Software Corp.
  • www.mql5.com
Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.
 
Pablo Rego:
How can I check if there exists a pending order (in my case, a Buy Stop order) for a specific symbol?
 

If you're talking about mql4, the following two functions are fair enough to give you what you're looking for.

I use them - mostly for Buy and Sell - heavily in my codes. 

 

Something happen makes code is not attached ! ...

...
...
...
if(!ExistPositionBuyStop() && ... && ...)
   OpenBuyStop();

if(!ExistPositionSellStop() && ... && ...)
   OpenSellStop();
...
...
...

bool ExistPositionBuyStop() {
   for (int i=0; i<OrdersTotal(); i++) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol()==Symbol() && OrderType()==OP_BUYSTOP && OrderMagicNumber()==MagicNo) {
            return(true);
         }
      } 
   } 
   return(false);
}

bool ExistPositionSellStop() {
   for (int i=0; i<OrdersTotal(); i++) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol()==Symbol() && OrderType()==OP_SELLSTOP && OrderMagicNumber()==MagicNo) {
            return(true);
         }
      } 
   } 
   return(false);
}
 
Osama Shaban:

If you're talking about mql4, the following two functions are fair enough to give you what you're looking for.

I use them - mostly for Buy and Sell - heavily in my codes. 

He is talking about MT5/mql5
 
Alain Verleyen:

You can start by reading the documentation about how to work with orders : https://www.mql5.com/en/articles/211

Thanks! I read it. 

Based on it I, (question) do you think it's reasonable to use

 

if (cPosition == 0){
   bool openedOrder = false;
   ulong ticket = 0;
   
   for (int i=0; i < OrdersTotal(); i++){
      ticket = OrderGetTicket();
      if (OrderGetString(ORDER_COMMENT) == "myKeyOrder" && ticket != 0){
         openedOrder = true;
         break;
      }
   }

   if (conditionsApply){
      posPr = hRates[1].high; // Just example
      posSl = hRates[1].low; // Just example
      posTp = 5 * hRates[1].high; // Just example
      
      if (!openedOrder)
         trade.BuyStop(cLots,posPr,_Symbol,posSl,posTp,ORDER_TIME_GTC,0,"myKeyOrder");
      else
         trade.OrderModify(ticket, posPr,posSl,posTp,ORDER_TIME_GTC,0)
         
      return;
   }
   else{
      if (openedOrder)
         trade.OrderDelete(ticket);
   }      
}

 ?

 
Osama Shaban:

If you're talking about mql4, the following two functions are fair enough to give you what you're looking for.

I use them - mostly for Buy and Sell - heavily in my codes. 

Thank you Osama! But I am using mql5.
 
pvrego:
Thank you Osama! But I am using mql5.

Working code for MQL5:

I use multiple EA and some of them trade on same _Symbol.

bool hasPendingOrder() {
   for (int i=0; i < OrdersTotal(); i++) {
      ulong ticket = OrderGetTicket(i);
      if(OrderSelect(ticket)) {
         if (OrderGetInteger(ORDER_MAGIC) == Expert_MagicNumber && OrderGetString(ORDER_SYMBOL) == _Symbol && ticket != 0){
            return true;
            break;
         }
      }
   }
   return false;
}
 
Henrique Demarco #:

Working code for MQL5:

I use multiple EA and some of them trade on same _Symbol.

You saved my life. I have searched for weeks. Thanks a lot.
 
   for (int i=0; i < OrdersTotal(); i++) {
      ulong ticket = OrderGetTicket(i);

MT4: You can not use any Trade Functions until you first select an order.
MT5: first select a position via CPositionInfo, directly, or by 'MT4Orders' library (2016)

 
Henrique Demarco #:
bool hasPendingOrder() {    for (int i=0; i < OrdersTotal(); i++) {       ulong ticket = OrderGetTicket(i);       if(OrderSelect(ticket)) {          if (OrderGetInteger(ORDER_MAGIC) == Expert_MagicNumber && OrderGetString(ORDER_SYMBOL) == _Symbol && ticket != 0){             return true;             break;          }       }    }    return false; }

Thanks!

Reason: