Very often a trader needs to sort out all the open positions or pending orders and perform some operations with the positions or orders placed in the instrument to which the expert advisor is attached. Let’s suppose we are interested only in pending orders.
By means of the OrderSymbol() function, we can receive information of the order or position selected by the OrderSelect() function.
string OrderSymbol()
We will write a portion of the code that looks through all our pending orders which weren’t deleted and performs some operations with orders placed in the instrument to which the expert advisor is attached.
int pos;
for (pos=0; pos=OP_BUYLIMIT)
{
// order proves to be pending; we will check the instrument
if (OrderSymbol()==Symbol())
{
// perform some actions with the order
// ...
}
}
}
else
Print("Error ", GetLastError(), " at selecting order number ", pos);
Two moments can be unclear for You in this code.
First, I define the type of order somewhat oddly at the first sight: if (OrderType()>=OP_BUYLIMIT). In fact condition OrderType()>=OP_BUYLIMIT will be true in the cases when an order is selected and will be false when a position is selected. The point is that constants defining the type of order have the following numerical values:
| Constant | Value | Description |
| OP_BUY | 0 | Long position |
OP_SELL | 1 | Selling |
| OP_BUYLIMIT | 2 | Pending order BUY LIMIT |
| OP_SELLLIMIT | 3 | Pending order SELL LIMIT |
| OP_BUYSTOP | 4 | Pending order BUY STOP |
| OP_SELLSTOP | 5 | Pending order SELL STOP |
As we can see in all cases when a pending order is selected the value of the OrderType() function will be greater than or equal to OP_BUYLIMIT (i.e. greater or equal to 2).
The second unclear moment in the code is the Symbol() function. Thsi function returns a text line with the name of the financial instrument to which the expert advisor is attached:
string Symbol()
Next article: "
OrderComment function"