MQL5 OrderCheck()
Checks whether there is enough money for a trade request without sending it, and writes what the account would look like into a result structure.
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.
Signature
The form of the call on the vendor's page, with the vendor's comments taken out.
bool OrderCheck( MqlTradeRequest& request, MqlTradeCheckResult& result )
Arguments
| Name | Type | Description |
|---|---|---|
| requestrequired | MqlTradeRequest& | The trade request to check, an MqlTradeRequest structure passed by reference. |
| resultrequired | MqlTradeCheckResult& | An MqlTradeCheckResult structure passed by reference, which receives the result of the check. |
Return value
- Type
- bool
- What comes back
- False when funds are not enough or the request is filled in wrongly. True when the basic check of the structures passes, which the page says is still no promise that the trade would go through. 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.
void OnStart()
{
MqlTradeRequest request={};
MqlTradeCheckResult check={};
request.action=TRADE_ACTION_DEAL;
request.symbol=_Symbol;
request.volume=0.1;
request.type =ORDER_TYPE_BUY;
request.price =SymbolInfoDouble(_Symbol,SYMBOL_ASK);
// Nothing is sent to the trade server by this call.
if(!OrderCheck(request,check))
PrintFormat("OrderCheck error %d, retcode=%u, %s",GetLastError(),check.retcode,check.comment);
else
PrintFormat("margin=%.2f free margin=%.2f",check.margin,check.margin_free);
}Return codes documented for this function
The page's example prints check.retcode as the server retcode, with outputs such as 10027 and 10018. Those are trade server return codes, 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".