How to check if order is opened?

 

Hi, to check if a order is opened I use OrderSelect and if OrderType() == OP_BUY I assume it's open. I don't know if this is how to check if a order is opened though :P Here's the code I got, that closes any long open position:

   for (int i = 1; i <= OrdersTotal(); i++)
   {
      if ( OrderSelect(i-1, SELECT_BY_POS) )
      {
         if (OrderType() == OP_BUY)
           OrderClose(i-1, 0.1, Bid, 3);
      }
   }
 
Jasus5457:

Hi, to check if a order is opened I use OrderSelect and if OrderType() == OP_BUY I assume it's open. I don't know if this is how to check if a order is opened though :P Here's the code I got, that closes any long open position:


   for (int i = OrdersTotal()-1; i > 0; i--)
   {
      if ( OrderSelect(i, SELECT_BY_POS) )
      {
         if (OrderType() == OP_BUY)
           OrderClose(i, 0.1, Bid, 3);
      }
   }

You need to go from the last to the first order when you close because order Pos is recalculate each time.

and yes if you can select an order...it's an open one if is type is OP_BUY or OP_SELL. It can be a pending one also.

 

Couple of comments...

The order pool index (as with all mq4 indexes) starts at 0. You start at i=1 and would therefore miss the first order.

When closing orders, you need to count backwards as closing an order changes the index of higher indexed orders. The next order up replaces the index space you just made by closing, but i++ means you will skip this order. If you count backwards, the order that fills the gap has already been checked and i-- takes you to the correct next order to check...

for(int i=OrdersTotal()-1; i>=0; i--)  

Yes, OP_BUY is an open order... as is OP_SELL.... an open short.

https://docs.mql4.com/trading/OrderType

hth

V


 

thank you for your fast responses :). Just one newbie question, in the 2nd for term you put i >= 0. Since 'for' statement will execute until that condition is true, shouldn't it stop in the first one, since i is bigger than 0 right from the beggining (assuming there are some orders).

 
Jasus5457:
thank you for your fast responses :). Just one newbie question, in the 2nd for term you put i >= 0. Since 'for' statement will execute until that condition is true, shouldn't it stop in the first one, since i is bigger than 0 right from the beggining (assuming there are some orders).


You have to re read help Cycle operator for

Expression1 describes the cycle initialization. Expression2 is the conditional test for the cycle termination. If it is true, the loop body for operator will be executed. The cycle repeats until Expression2 becomes false. If it is false, the cycle will be terminated, and the control will be given to the next operator. Expression3 is calculated after each iteration.

 

Oh I got it all wrong then, thanks.

And another thing. In the EA I made I'm getting this error: "OrderClose error 4051, invalid ticket for OrderClose function". This is all the code I have (just testing, so nevermind the absurd conditions):

int start()
  {
  if (Volume[1] < Volume[2])
  {   
   for (int i = OrdersTotal() - 1; i >= 0; i--)
   {
      if (OrderSelect(i, SELECT_BY_POS))
      {
         if (OrderType() == OP_BUY)
          OrderClose(i, 0.1, Bid, 3);
      }
   }
   return;
  }
  if (Volume[1] > Volume[2] && OrdersTotal() == 0)
  {
   int ticket = OrderSend(Symbol(),OP_BUY,0.1,Ask,3,NormalizeDouble(Bid-100*Point,Digits),NormalizeDouble(Bid+100*Point,Digits));  
   if (ticket == -1)
      Print("Order failed - ",GetLastError());
   else  
      Print("Order sucessful.");
  }
  return;
}
 

hi to check if a order is closed or still open i use

IF (OrderCloseTime()!=0)

//Order is closed

ELSE

//order is open

also this section in your code is wrong

  if (OrderSelect(i, SELECT_BY_POS))
      {
         if (OrderType() == OP_BUY)
          OrderClose(i, 0.1, Bid, 3);
      }
   }

you try to close the order with the positionnumber the order has in your terminal. instead you have to close by OrderTicket().

general function for closing order is:

OrderClose(OrderTicket(),OrderLotSize(),OrderClosePrice(),i_slippage);
 
zzuegg:

hi to check if a order is closed or still open i use

IF (OrderCloseTime()!=0)

//Order is closed

ELSE

//order is open

also this section in your code is wrong

you try to close the order with the positionnumber the order has in your terminal. instead you have to close by OrderTicket().

general function for closing order is:


Thanks, that answered my questions :)

 
  if((OrderSelect(i,SELECT_BY_POS)==true) && (OrderType() == OP_BUY))
     {
      ticket=OrderTicket();
      lots=OrderLots();
      OrderClose(ticket,lots,Bid,3);
     }
another way to track if order exists or not is to assign magic number when you open orders, then you can check if an order with that magic number exists