MQL4 OrderModify()
Changes an open or pending order: its stop loss and take profit, and for a pending order its price and expiry too.
Risk warning: CFDs and spread bets are complex instruments and come with a high risk of losing money rapidly due to leverage. You can lose money on these products, and each provider publishes its own loss figure under FCA rules. You should consider whether you understand how they work and whether you can afford to take the high risk of losing your money. Automated and API trading can amplify losses through bugs, connectivity failures and runaway orders.
This is an MQL4 function, not an HTTP endpoint. It has no method, no path and no address to send a request to: it runs inside the MetaTrader 4 terminal, in a program written in MQL4.
The page says the open price and the expiry can be changed only on a pending order.
Signature
The form of the call on the vendor's page, with the vendor's comments taken out.
bool OrderModify( int ticket, double price, double stoploss, double takeprofit, datetime expiration, color arrow_color )
Arguments
| Name | Type | Description |
|---|---|---|
| ticketrequired | int | The order's ticket number. |
| pricerequired | double | The new open price, for a pending order. |
| stoplossrequired | double | The new stop loss level. |
| takeprofitrequired | double | The new take profit level. |
| expirationrequired | datetime | The pending order's expiry time. |
| arrow_color | color | The colour of the arrows drawn on the chart for the stop loss and take profit change. Left out or set to CLR_NONE, none is drawn. |
Return value
- Type
- bool
- What comes back
- True on success, false otherwise. The page points to GetLastError() for the reason.
Sample
Written by this site from the signature and arguments recorded on this page. It has not been run against a terminal, a server or an account, and the values in it are placeholders. It holds no credential and needs none: the program runs inside a terminal that is already logged in to an account. Nothing is entered on this site.
// Run it only on a demo account: it moves the stop loss of one order.
void MoveStop(const int ticket,const double new_stop)
{
if(!OrderSelect(ticket,SELECT_BY_TICKET))
return;
if(!OrderModify(ticket,OrderOpenPrice(),new_stop,OrderTakeProfit(),0,clrNONE))
Print("OrderModify failed with error #",GetLastError());
}Return codes documented for this function
The page says GetLastError() gives the detail of a failure and names two of the codes: 1, raised when the values passed are unchanged, and 147, raised where the trade server does not allow expiry times. They are among MetaTrader 4's trade server return codes, each of which recorded here is below. docs.mql4.comchecked
- ERR_NO_ERROR (0)No error was returned.
- ERR_NO_RESULT (1)No error was returned, but the result is unknown.
- ERR_COMMON_ERROR (2)"Common error" is MetaQuotes' whole description of this code.
- ERR_INVALID_TRADE_PARAMETERS (3)"Invalid trade parameters" is MetaQuotes' whole description of this code.
- ERR_SERVER_BUSY (4)"Trade server is busy" is MetaQuotes' whole description of this code.
- ERR_OLD_VERSION (5)"Old version of the client terminal" is MetaQuotes' whole description of this code.
- ERR_NO_CONNECTION (6)"No connection with trade server" is MetaQuotes' whole description of this code.
- ERR_NOT_ENOUGH_RIGHTS (7)"Not enough rights" is MetaQuotes' whole description of this code.
- ERR_TOO_FREQUENT_REQUESTS (8)"Too frequent requests" is MetaQuotes' whole description of this code.
- ERR_MALFUNCTIONAL_TRADE (9)"Malfunctional trade operation" is MetaQuotes' whole description of this code.
- ERR_ACCOUNT_DISABLED (64)"Account disabled" is MetaQuotes' whole description of this code.
- ERR_INVALID_ACCOUNT (65)"Invalid account" is MetaQuotes' whole description of this code.
- ERR_TRADE_TIMEOUT (128)"Trade timeout" is MetaQuotes' whole description of this code.
- ERR_INVALID_PRICE (129)"Invalid price" is MetaQuotes' whole description of this code.
- ERR_INVALID_STOPS (130)"Invalid stops" is MetaQuotes' whole description of this code.
- ERR_INVALID_TRADE_VOLUME (131)"Invalid trade volume" is MetaQuotes' whole description of this code.
- ERR_MARKET_CLOSED (132)"Market is closed" is MetaQuotes' whole description of this code.
- ERR_TRADE_DISABLED (133)"Trade is disabled" is MetaQuotes' whole description of this code.
- ERR_NOT_ENOUGH_MONEY (134)"Not enough money" is MetaQuotes' whole description of this code.
- ERR_PRICE_CHANGED (135)"Price changed" is MetaQuotes' whole description of this code.
- ERR_OFF_QUOTES (136)"Off quotes" is MetaQuotes' whole description of this code.
- ERR_BROKER_BUSY (137)"Broker is busy" is MetaQuotes' whole description of this code.
- ERR_REQUOTE (138)"Requote" is MetaQuotes' whole description of this code.
- ERR_ORDER_LOCKED (139)"Order is locked" is MetaQuotes' whole description of this code.
- 140 ERR_LONG_POSITIONS_ONLY_ALLOWEDOnly buy orders are allowed.
- ERR_TOO_MANY_REQUESTS (141)"Too many requests" is MetaQuotes' whole description of this code.
- ERR_TRADE_MODIFY_DENIED (145)The modification was denied because the order is too close to the market.
- ERR_TRADE_CONTEXT_BUSY (146)"Trade context is busy" is MetaQuotes' whole description of this code.
- ERR_TRADE_EXPIRATION_DENIED (147)Expirations are denied by the broker.
- ERR_TRADE_TOO_MANY_ORDERS (148)The number of open and pending orders has reached the limit set by the broker.
- ERR_TRADE_HEDGE_PROHIBITED (149)The code tried to open an order opposite to an existing one while hedging is disabled.
- ERR_TRADE_PROHIBITED_BY_FIFO (150)The code tried to close an order in a way that breaks the FIFO rule.