MetaTrader5 Python functions: the full mt5 reference
All 32 functions of the MetaTrader5 Python package. Each page says what the
function does, its parameters, what it returns, the mistakes people make with it and how to do the same
with Tickwire's REST API.
Three things catch most people out. Times from the terminal are the broker's server clock, not UTC.
A symbol must be in Market Watch before you read its prices. And an order_send() that
fails without a clear answer may still have placed the order, so check positions and history before
you send again. See time zones and
idempotency.
Connect, log in and check errors
Start the link to the terminal, log in to an account, read the terminal's version and the last error.
| Function | What it does |
|---|---|
mt5.initialize() | Opens the link between your Python script and a MetaTrader 5 terminal, and can log in to an account in the same call. Returns True or False. |
mt5.login() | Logs the connected terminal in to a trading account, or switches it to another one. Returns True when the login worked and False when it did not. |
mt5.shutdown() | Closes the link that initialize() opened between your script and the MetaTrader 5 terminal. It takes no arguments and returns None. |
mt5.version() | Returns the version, build number and release date of the MetaTrader 5 terminal your script is linked to, or None when the call fails. |
mt5.last_error() | Returns the code and description of the last error the MetaTrader5 package recorded. Call it right after a function returns None or False. |
Account and terminal
Read the trading account and the terminal it runs in.
| Function | What it does |
|---|---|
mt5.account_info() | Returns the logged-in trading account as a named tuple: login, balance, equity, margin, free margin, leverage, currency and more. None if it fails. |
mt5.terminal_info() | Returns the state and settings of the MetaTrader 5 terminal as a named tuple, including whether it is connected to the broker and allows algo trading. |
Symbols and prices
List symbols, read their settings and last price, and add them to Market Watch.
| Function | What it does |
|---|---|
mt5.symbols_total() | Returns how many symbols the MetaTrader 5 terminal knows, including custom symbols and ones hidden from Market Watch. Returns an integer. |
mt5.symbols_get() | Returns the terminal's symbols as a tuple of named tuples, all of them or only those that match a group filter such as "*USD*". None on failure. |
mt5.symbol_info() | Returns everything the terminal knows about one symbol as a named tuple: digits, point, contract size, volume limits, filling modes and more. None if unknown. |
mt5.symbol_info_tick() | Returns the last tick of a symbol: time, bid, ask, last price and volume. Use it for the current price. Returns None when there is no tick. |
mt5.symbol_select() | Adds a symbol to the terminal's Market Watch, or removes it. Most price and data functions need the symbol selected first. Returns True or False. |
Market depth
Subscribe to the order book of a symbol, read it and let it go.
| Function | What it does |
|---|---|
mt5.market_book_add() | Subscribes the terminal to market depth, the order book, for one symbol. You need it before market_book_get() returns anything. Returns True or False. |
mt5.market_book_get() | Returns the current market depth of a symbol as a tuple of levels, each with a type, a price and a volume. Needs market_book_add() first. None on failure. |
mt5.market_book_release() | Ends the market depth subscription that market_book_add() started for a symbol, so the terminal stops collecting its order book. Returns True or False. |
Bars and ticks
Copy price history into numpy arrays: bars by date, by position or by range, and ticks.
| Function | What it does |
|---|---|
mt5.copy_rates_from() | Copies a number of bars of one symbol and timeframe, ending at a date you give, into a numpy array. Returns None when the bars can't be read. |
mt5.copy_rates_from_pos() | Copies bars of one symbol and timeframe counted from a bar position, where 0 is the bar that is still forming. Returns a numpy array, or None on failure. |
mt5.copy_rates_range() | Copies every bar of one symbol and timeframe that opened between two dates into a numpy array. Returns None when the bars can't be read. |
mt5.copy_ticks_from() | Copies a number of ticks of one symbol, starting at a date you give and going forward, into a numpy array. Returns None when the ticks can't be read. |
mt5.copy_ticks_range() | Copies every tick of one symbol between two dates into a numpy array, with flags to choose price ticks or trade ticks. Returns None on failure. |
Open orders and positions
Count and read the pending orders and open positions on the account.
| Function | What it does |
|---|---|
mt5.orders_total() | Returns how many pending orders, such as limits and stops, are open on the account right now. Returns an integer. |
mt5.orders_get() | Returns the account's pending orders as a tuple of named tuples, all of them or filtered by symbol, group or ticket. Returns None on failure. |
mt5.positions_total() | Returns how many positions are open on the account right now. Returns an integer. Use positions_get() to read them. |
mt5.positions_get() | Returns the account's open positions as a tuple of named tuples, all of them or filtered by symbol, group or ticket. Returns None on failure. |
Trading
Work out margin and profit, check a request and send it.
| Function | What it does |
|---|---|
mt5.order_calc_margin() | Works out the margin a buy or sell of a given volume and price would need, in the account currency, without placing anything. Returns a float or None. |
mt5.order_calc_profit() | Works out the profit or loss of a buy or sell of a given volume from an open price to a close price, in the account currency. Returns a float or None. |
mt5.order_check() | Checks whether a trade request would go through, and shows the balance, equity and margin after it, without sending it. Returns a result or None. |
mt5.order_send() | Sends a trade request to the broker: open a position, place a pending order, change stops, or close. Returns a result with a retcode, or None. |
Trade history
Count and read closed orders and deals over a date range, or by ticket or position.
| Function | What it does |
|---|---|
mt5.history_orders_total() | Returns how many orders in the account's history fall between two dates: filled, canceled and expired orders. Returns an integer. |
mt5.history_orders_get() | Returns orders from the account's history as a tuple of named tuples: by date range, with an optional symbol filter, or by order or position ticket. |
mt5.history_deals_total() | Returns how many deals, the actual fills plus balance operations, are in the account's history between two dates. Returns an integer. |
mt5.history_deals_get() | Returns deals from the account's history as a tuple of named tuples, with price, volume, profit, commission and swap. By date range, order or position. |
The package runs only on Windows, next to a running terminal. To call MT5 from Linux, macOS or a server, see the MT5 Python API page. For the codes these functions return, see the MetaTrader error codes.