MT5 Python order_send()
Sends a trade request from the terminal to the trade server. One call covers a market order, a pending order, a change to an order or position, a cancellation and a close: the request's action field says which.
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 a Python function, not an HTTP endpoint. It has no method, no path and no address to send a request to: the package talks to a MetaTrader 5 terminal running on the same computer.
This call can place a real order on whichever account the terminal is logged in to, demo or live. The page says a request goes through several checks on the trade server and that a request being accepted is not the same as the trade being done. The action field takes a value of the TRADE_REQUEST_ACTIONS enumeration. The page for order_check() lists them: TRADE_ACTION_DEAL, TRADE_ACTION_PENDING, TRADE_ACTION_SLTP, TRADE_ACTION_MODIFY, TRADE_ACTION_REMOVE and TRADE_ACTION_CLOSE_BY.
Signature
The form of the call on the vendor's page, with the vendor's comments taken out.
order_send( request )
Arguments
| Name | Type | Description |
|---|---|---|
| requestrequired | MqlTradeRequest structure | The trade request. In Python it is written as a dictionary, as the page's example does. The page lists its fields: action, magic, order, symbol, volume, price, stoplimit, sl, tp, deviation, type, type_filling, type_time, expiration, comment, position and position_by. Which are needed depends on the action. |
Return value
- Type
- MqlTradeResult structure
- What comes back
- The result of the request. Its request field holds the request that was passed in, and its retcode field holds the trade server's return code. The page's example output prints it as OrderSendResult, with retcode, deal, order, volume, price, bid, ask, comment and request_id.
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. No credential is typed into it: where one is needed it is read from an environment variable on your own machine. Nothing is entered on this site.
import MetaTrader5 as mt5
if not mt5.initialize():
raise SystemExit(f"initialize() failed: {mt5.last_error()}")
# Run this only against a demo account. It sends a market order.
symbol = "EURUSD"
tick = mt5.symbol_info_tick(symbol)
request = {
"action": mt5.TRADE_ACTION_DEAL,
"symbol": symbol,
"volume": 0.1,
"type": mt5.ORDER_TYPE_BUY,
"price": tick.ask,
"deviation": 20,
"magic": 234000,
"comment": "sample",
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_RETURN,
}
result = mt5.order_send(request)
if result is None:
print("order_send() failed:", mt5.last_error())
elif result.retcode != mt5.TRADE_RETCODE_DONE:
print("not done, retcode", result.retcode)
else:
print("done, order", result.order)
mt5.shutdown()Return codes documented for this function
The page says the call returns the MqlTradeResult structure, and its example tests that result's retcode field against TRADE_RETCODE_DONE. The values that field can hold are the MetaTrader 5 trade server return codes, each recorded below from MetaQuotes' own list of them. 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".