MQL4 OrderSend()
Opens a market order or places a pending order. The page calls it the main function for doing either.
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.
This function can place a real order on whichever account the terminal is logged in to, demo or live. The page says a market order can be opened only at the latest Bid, for a sell, or Ask, for a buy, that prices have to be normalised to the symbol's digits, that stop levels cannot sit closer to the market than the distance MarketInfo() returns for MODE_STOPLEVEL, and that some trade servers switch off expiry times or cap the number of open and pending orders.
Signature
The form of the call on the vendor's page, with the vendor's comments taken out.
int OrderSend( string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment=NULL, int magic=0, datetime expiration=0, color arrow_color=clrNONE )
Arguments
| Name | Type | Description |
|---|---|---|
| symbolrequired | string | The symbol to trade. |
| cmdrequired | int | The kind of operation, as one of the trade operation values, for example OP_BUY or OP_SELL. |
| volumerequired | double | The number of lots. |
| pricerequired | double | The order price. |
| slippagerequired | int | The most price slippage to accept on a buy or sell order. |
| stoplossrequired | double | The stop loss level. |
| takeprofitrequired | double | The take profit level. |
| comment | string | A comment on the order. The page says the server can change its last part. Default: NULL. |
| magic | int | The order's magic number, which a program can use as its own identifier. Default: 0. |
| expiration | datetime | When the order expires. For pending orders only. Default: 0. |
| arrow_color | color | The colour of the opening arrow on the chart. Left out or set to CLR_NONE, no arrow is drawn. Default: clrNONE. |
Return value
- Type
- int
- What comes back
- The ticket number the trade server gave the order, or -1 on failure. 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.
// Script. Run it only on a demo account: it sends a market order.
void OnStart()
{
int ticket=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0,"sample",123456,0,clrNONE);
if(ticket<0)
Print("OrderSend failed with error #",GetLastError());
else
Print("OrderSend placed order #",ticket);
}Return codes documented for this function
The page says the function returns -1 on failure and that GetLastError() gives the code. It names four of those codes in its notes, 129, 130, 147 and 148, and describes 148 as returned by the trade server. 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.