MQL4 OrderSelect Function

Once we have successfully placed an order, we will need to gather information about the order, particularly if we want to modify it. Learn in this MQL4 OrderSelect Function article how to get the order information and how to use this function if you want the EA to modify or close it.

All this is done through the OrderSelect() function. To use the OrderSelect(), we can ether use the ticket number of the order, or we can loop through the pool of open orders and select each of them in order. 

Here is the syntax for the OrderSelect() function:

bool OrderSelect(int Index, int Select, int Pool=MODE_TRADES)

For a handy reference, a description of the above parameters can be found in the table below:

ParamsDescription
IndexEither the ticket number of the order we want to select, or the position in the order pool. The Select parameter indicates which one. 
SelectA constant indicating whether the Index parameter is a ticket number or an oder pool position: 
  • SELECT_BY_TICKET–  The value of the Index parameter is an order ticket number
  • SELECT_BY_POS–  The value of the Index parameter is an order pool position
PoolAn optional constant indicating the order pool: pending/open orders, or closed orders. 
  • MODE_POOL–  By default, refers to the pool of currently opened orders
  • MODE_HISTORY– Examines the closed order pool (the order history)

Here is an example of an OrderSelect() function using the an order ticket number. It is set up for the modification of the of stop loss and take profit after a buy order has taken place:

if (OrdersTotalMagicOpen()==0 && OpenBuy==true)
{
ticket = OrderSend(Symbol(),OP_BUY,NormalizeDouble(Lots,LotDigits),
Ask,vSlippage,0,0,EAName, MagicNumber, 0, Green);
return (ticket);
if(ticket>0)
{
OrderSelect(ticket,SELECT_BY_TICKET);
OrderModify(OrderTicket(),OrderOpenPrice(),Bid – Stop_Loss * vPoint, Ask+TakeProfit * vPoint,0,Green);
}
}

In the above example, we used the OrderSelect() to select by ticket number, and then conjoined it with the OrderModify() function, so we could modify the StopLoss and TakeProfit. This example is particularly useful for ECN brokers. In an ECN broker you cannot place your stoploss and takeprofit values in their corresponding parameters within the OrderSend() function. Instead, these parameters must remain as 0. Only after the trade has been placed, can the order stoploss and takeprofit be modified via the OrderSelect() and OrderModify() functions, as in the illustration above. 

While the above OrderSelect() conjoins with the OrderModify() function, there is actually a range of order information functions that one can deploy to retrieve information about an order. There is a complete listing of these functions in the MLQ Reference. Here is a list of the commonly used order information functions: 

FunctionsDescription
OrderSymbol()The symbol of the instrument that the order was placed on. 
OrderType()The type of order: buy or sell; market, stop or limit.
OrderOpenPrice()The opening price of the selected order.
OrderLots()The lot size of the selected order.
OrderStopLoss()The stop loss price of the selected order. 
OrderTakeProfit()The take profit of the selected order. 
OrderTicket()The ticket number of the selected order. 
OrderMagicNumber()The magic number of the selected order.