MQL5 OrderSendAsync()
Sends a trade request to the trade server without waiting for its answer. The page describes it as made for high frequency trading, where waiting for the server is not acceptable.
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 MQL5 function, not an HTTP endpoint. It has no method, no path and no address to send a request to: it runs inside the MetaTrader 5 terminal, in a program written in MQL5.
The outcome of the request arrives later, through the OnTradeTransaction and OnTrade handlers. The page says OnTradeTransaction is called several times for one request.
Signature
The form of the call on the vendor's page, with the vendor's comments taken out.
bool OrderSendAsync( MqlTradeRequest& request, MqlTradeResult& result )
Arguments
| Name | Type | Description |
|---|---|---|
| requestrequired | MqlTradeRequest& | The trade request, an MqlTradeRequest structure passed by reference. |
| resultrequired | MqlTradeResult& | An MqlTradeResult structure passed by reference. After a successful send its return code holds TRADE_RETCODE_PLACED, which is 10008. |
Return value
- Type
- bool
- What comes back
- True when the request was sent, false when it was not. The page says true means only that it was sent: it is no guarantee the request reached the server or was accepted.
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.
// Expert Advisor fragment. Run it only on a demo account.
void SendBuy()
{
MqlTradeRequest request={};
MqlTradeResult result={};
request.action=TRADE_ACTION_DEAL;
request.symbol=_Symbol;
request.volume=0.1;
request.type =ORDER_TYPE_BUY;
request.price =SymbolInfoDouble(_Symbol,SYMBOL_ASK);
if(!OrderSendAsync(request,result))
PrintFormat("OrderSendAsync error %d, retcode=%u",GetLastError(),result.retcode);
}
// The server's answer arrives here, more than once for one request.
void OnTradeTransaction(const MqlTradeTransaction &trans,const MqlTradeRequest &request,const MqlTradeResult &result)
{
Print(EnumToString(trans.type));
}Return codes documented for this function
The page says that after a successful send the response code in the result variable holds TRADE_RETCODE_PLACED, code 10008, and its example prints result.retcode. That field holds a trade server return code, and each one recorded here is below. mql5.comchecked
- TRADE_RETCODE_REQUOTE (10004)The price changed and the server offered a new one.
- TRADE_RETCODE_REJECT (10006)The server rejected the request.
- TRADE_RETCODE_CANCEL (10007)The request was cancelled by the trader.
- TRADE_RETCODE_PLACED (10008)The order was placed.
- TRADE_RETCODE_DONE (10009)The request was completed.
- TRADE_RETCODE_DONE_PARTIAL (10010)Only part of the request was completed.
- TRADE_RETCODE_ERROR (10011)The server reports an error in processing the request.
- TRADE_RETCODE_TIMEOUT (10012)The request was cancelled because it timed out.
- TRADE_RETCODE_INVALID (10013)The trade request is malformed.
- TRADE_RETCODE_INVALID_VOLUME (10014)Volume is outside min, max or step for the symbol.
- TRADE_RETCODE_INVALID_PRICE (10015)The price is not valid.
- TRADE_RETCODE_INVALID_STOPS (10016)Stop loss or take profit is invalid, often too close to price.
- 10017 TRADE_RETCODE_TRADE_DISABLEDTrading is disabled.
- TRADE_RETCODE_MARKET_CLOSED (10018)The market is closed.
- TRADE_RETCODE_NO_MONEY (10019)Insufficient margin.
- TRADE_RETCODE_PRICE_CHANGED (10020)Prices changed while the request was being handled.
- TRADE_RETCODE_PRICE_OFF (10021)There are no quotes to process the request.
- 10022 TRADE_RETCODE_INVALID_EXPIRATIONThe order expiration date in the request is invalid.
- TRADE_RETCODE_ORDER_CHANGED (10023)The state of the order changed.
- 10024 TRADE_RETCODE_TOO_MANY_REQUESTSRequests are being sent too often.
- TRADE_RETCODE_NO_CHANGES (10025)The request contains no changes.
- 10026 TRADE_RETCODE_SERVER_DISABLES_ATAutotrading is disabled by the server.
- TRADE_RETCODE_CLIENT_DISABLES_AT (10027)Algo trading is switched off in the terminal.
- TRADE_RETCODE_LOCKED (10028)The request is locked for processing.
- TRADE_RETCODE_FROZEN (10029)The order or position is frozen.
- 10030Invalid order filling type: order_send asked for a filling mode the symbol does not allow.
- TRADE_RETCODE_CONNECTION (10031)There is no connection with the trade server.
- TRADE_RETCODE_ONLY_REAL (10032)The operation is allowed only for live accounts.
- TRADE_RETCODE_LIMIT_ORDERS (10033)The number of pending orders has reached the limit.
- TRADE_RETCODE_LIMIT_VOLUME (10034)The volume of orders and positions for the symbol has reached the limit.
- TRADE_RETCODE_INVALID_ORDER (10035)The order type is incorrect or prohibited.
- 10036 TRADE_RETCODE_POSITION_CLOSEDThe position with the specified POSITION_IDENTIFIER has already been closed.
- 10038 TRADE_RETCODE_INVALID_CLOSE_VOLUMEThe close volume is larger than the current volume of the position.
- 10039 TRADE_RETCODE_CLOSE_ORDER_EXISTA close order already exists for the position.
- 10040 TRADE_RETCODE_LIMIT_POSITIONSThe number of open positions on the account has reached a limit set on the server.
- TRADE_RETCODE_REJECT_CANCEL (10041)The request to activate a pending order was rejected and the order was cancelled.
- TRADE_RETCODE_LONG_ONLY (10042)The request was rejected because the symbol has the rule "Only long positions are allowed" (POSITION_TYPE_BUY).
- TRADE_RETCODE_SHORT_ONLY (10043)The request was rejected because the symbol has the rule "Only short positions are allowed" (POSITION_TYPE_SELL).
- TRADE_RETCODE_CLOSE_ONLY (10044)The request was rejected because the symbol has the rule "Only position closing is allowed".
- TRADE_RETCODE_FIFO_CLOSE (10045)The request was rejected because the trading account has the flag "Position closing is allowed only by FIFO rule" set (ACCOUNT_FIFO_CLOSE=true).
- 10046 TRADE_RETCODE_HEDGE_PROHIBITEDThe request was rejected because the trading account has the rule "Opposite positions on a single symbol are disabled".