Broker API HubMenu

MT5 Python order_send()

functionorder_send()MetaTrader5 Python packageOrders

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.

Independent and unofficialmql5.comchecked

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

NameTypeDescription
requestrequiredMqlTradeRequest structureThe 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