The OrderModify() function.
bool OrderModify(int ticket, double price, double stoploss, double takeprofit,
datetime expiration, color arrow_color=CLR_NONE)
This function allows to modify parameters of pending orders and open positions. In case of success it returns true, in case of a failure it returns false. the code of the error can be received with the help of the GetLastError() function.
The parameters of the function:
- ticket — ticket of a pending order or an open position;
- price — new level of a pending order (for open positions it can’t be changed);
- stoploss — new level of Stop Loss;
- takeprofit — new level of Take Profit;
- expiration — new expiration date and time of a pending order (if the order doesn’t trigger by this date and time it will be deleted — refer to the article «OrderExpiration() — expiration date of a pending order»);
- arrow_color — the color of the opening arrow on the chart. If this parameter lacks or its value is equal to CLR_NONE, the opening arrow isn’t displayed on the chart.
In case of incorrect parameters the function returns the following codes of errors:
- 1 (ERR_NO_RESULT) — if none of the parameters was modified;
- 147 (ERR_TRADE_EXPIRATION_DENIED) — if it is prohibited in the trading server settings to set the expiration date of an order. In this case parameter expiration must always be equal to zero).
As a practical example of this function usage we’ll consider how to place and manage a trailing stop.
Let me remind You that a Trailing Stop is an algorithm of managing the level of a Stop Loss order. After a trailing stop is placed (for example at X pips) the following happens:
- MetaTrader doesn’t take any actions until a profit of X pips forms in the open position. After that MetaTrader places Stop Loss order at the distance of Х from the current price (in this case at the breakeven level).
- On performing the first step MetaTrader send a command to change the level of Stop Loss order by the distance of Х pips from the current quote each time when the distance between it and the old level of the order exceeds X pips. As a result the Stop Loss order «trails» to the current price.
We will realize this principle in the MetaQuotes Language 4. We will assume that an open position is selected and we know exactly that this position is open in the instrument to which the expert advisor is attached. Also we will assume that the value of the trailing stop in pips is contained in variable TrailingStop.
int err;
if (OrderType() == OP_BUY)
{
// long position
if ((Bid-OrderOpenPrice())>=(TrailingStop*Point))
{
// we place Stop Loss
if (OrderModify(OrderTicket(), OrderOpenPrice(), Bid-TrailingStop*Point,
OrderTakeProfit(), 0))
Print("#", OrderTicket(),": trailing stop ", Bid-TrailingStop*Point);
else
{
err = GetLastError();
Print("#", OrderTicket(),": trailing stop error ", err);
}
}
}
else
{
// short position
if ((OrderOpenPrice()-Ask)>=(TrailingStop*Point))
{
// we place Stop Loss
if (OrderModify(OrderTicket(), OrderOpenPrice(), Ask+TrailingStop*Point,
OrderTakeProfit(), 0))
Print("#", OrderTicket(),": trailing stop ", Ask+TrailingStop*Point);
else
{
err = GetLastError();
Print("#", OrderTicket(),": trailing stop error ", err);
}
}
}
Next article: "
OrderDelete function"