151 Order Functions
151 Order Functions
OrderSelect()
What it does: It selects a position or order1 for further processing (calculations, order manipulation
etc).
This function is slightly less intuitive. It returns a value, and it does an action (selects an order). We
are more interested in the action. The word “select” may be confusing from a programming point of
view. Think of it this way, when an order is selected, something happens in the background that
loads up the order. We don’t see it directly. But if we use certain order functions when the order is
loaded up, these functions will be applied to the order.
Example:
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true) {
OrderClose(OrderTicket(),OrderLots(),Price,Slip*K,arrow_color);
The next question is “How do we know which order are we selecting?” When we use OrderSelect(),
we are selecting an order in our order pool. Our order pool refers to the list of opened and pending
order (see fig 1 below2).
Selecting Orders:
There are 2 ways to select orders (See 2nd input of OrderSelect()). First, is to select by the order
position in the list. Second, is to select by order ticket (1st column of fig 1). The first way is more
common as it is more convenient to select by position. The second way requires us to keep a log of
all order ticket values and this is cumbersome.
1
Definitions: Position describes an opened trade. Order is a pending trade.
2
http://book.mql4.com/trading/orderclose
blackalgotechnologies.com
Black Algo Technologies
If you are still unclear, don’t worry. In the upcoming Lecture “Order Management”, you will get a
clearer picture on how OrderSelect() is being used.
Function:
bool OrderSelect(
int index, // index or order ticket
int select, // flag
int pool=MODE_TRADES // mode
);
Parameters:
ticket
[in] Order index (order index starts from 0 not 1) or order ticket depending on the second
parameter.
select
[in] Selecting flags. It can be any of the following values:
pool=MODE_TRADES
[in] Optional order pool index. Used when the selected parameter is SELECT_BY_POS. It can be any
of the following values:
MODE_TRADES (default)- order selected from trading pool(opened and pending orders),
MODE_HISTORY - order selected from history pool (closed and canceled order).
Returned value
It returns true if the function succeeds, otherwise falses. To get the error information, one has to call
the GetLastError() function.
The OrderSelect() function is used to retrieve values for the following order functions (list not
exhaustive):
Documentation: http://docs.mql4.com/trading/orderselect
blackalgotechnologies.com
Black Algo Technologies
OrderModify()
We will use this to modify entry pending place, take profit levels, stop loss levels and order
expiration3. This is the function we use to create trailing stops. See BelindaSizing_Complete line 184
(Trailing Stops).
Function:
bool OrderModify(
int ticket, // ticket
double price, // price
double stoploss, // stop loss
double takeprofit, // take profit
datetime expiration, // expiration
color arrow_color // color
);
Parameters:
ticket
[in] Unique number of the order ticket.
price
[in] New open price of the pending order.
stoploss
[in] New StopLoss level.
takeprofit
[in] New TakeProfit level.
expiration
[in] Pending order expiration time.
arrow_color
[in] Arrow color for StopLoss/TakeProfit modifications in the chart. If the parameter is missing or has
CLR_NONE value, the arrows will not be shown in the chart.
Returned value
If the function succeeds, it returns true, otherwise false. To get the detailed error information, call
the GetLastError() function.
Documentation: http://docs.mql4.com/trading/ordermodify
3
Expiration date and time for a pending order. See: http://docs.mql4.com/trading/ordersend (10th input) and
http://docs.mql4.com/trading/orderexpiration
blackalgotechnologies.com
Black Algo Technologies
OrderDelete()
This function is important when we deal with pending orders. A common mistake beginners make
when implementing pending orders is to not include OrderDelete() or order expiration. Without these,
the order will be there perpetually.
OrderDelete() is preferred when we choose to delete orders based on certain conditions. For
instance, we implement a breakout strategy and submit 2 orders, one buy stop and one sell stop.
When either of the order gets filled, we delete the other one. In this case, order expiration is not
useful. OrderDelete() will be our tool of choice.
Function:
bool OrderDelete(
int ticket, // ticket
color arrow_color // color
);
Parameters:
Parameters
ticket
[in] Unique number of the order ticket.
arrow_color
[in] Color of the arrow on the chart. If the parameter is missing or has CLR_NONE value arrow will
not be drawn on the chart.
Returned value
If the function succeeds, it returns true, otherwise false. To get the detailed error information, call
the GetLastError() function.
Documentation: http://docs.mql4.com/trading/orderdelete
blackalgotechnologies.com