Download MetaTrader 5

Check pending order existence

To add comments, please log in or register
Store your source codes in MQL5 Storage. It is secure!
Pablo Rego
1651
Pablo Rego 2016.02.26 16:30 
How can I check if there exists a pending order (in my case, a Buy Stop order) for a specific symbol?
 
Alain Verleyen
Moderator
30722
Alain Verleyen 2016.02.26 17:42  
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.
Osama Shaban
1446
Osama Shaban 2016.02.26 20:22  
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. 

Osama Shaban
1446
Osama Shaban 2016.02.26 20:28  

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);
}
Alain Verleyen
Moderator
30722
Alain Verleyen 2016.02.26 21:22  
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
Pablo Rego
1651
Pablo Rego 2016.02.28 14:23  
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);
   }      
}

 ?

Pablo Rego
1651
Pablo Rego 2016.02.28 14:23  
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.
To add comments, please log in or register