MT5 Python last_error()
Returns the code and description of the last error raised by a call to the package. The codes are the package's own, not the terminal's.
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.
Signature
The form of the call on the vendor's page, with the vendor's comments taken out.
last_error()
Arguments
The function takes no arguments.
Return value
- Type
- tuple
- What comes back
- The last error's code and its description. The page says it works like GetLastError() in MQL5 but uses codes of its own, and lists them: RES_S_OK is 1 and the failures are negative numbers.
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():
code, description = mt5.last_error()
raise SystemExit(f"initialize() failed: {code} {description}")
mt5.shutdown()Return codes documented for this function
The page for last_error() lists the package's own codes with their constants, and those are what the first value of the returned tuple holds. Each one recorded here is below. mql5.comchecked
- RES_E_INTERNAL_FAIL_TIMEOUT (-10005)The terminal did not respond in time.
- RES_E_INTERNAL_FAIL_INIT (-10003)IPC initialisation failed; the docs also list RES_E_INTERNAL_FAIL_CONNECT (internal IPC no ipc) under -10003.
- RES_E_INTERNAL_FAIL_CONNECT (-10003)No interprocess link (IPC) between the Python package and the terminal.
- RES_E_INTERNAL_FAIL_RECEIVE (-10002)Receiving from the terminal failed.
- RES_E_INTERNAL_FAIL_SEND (-10001)Sending to the terminal failed.
- RES_E_INTERNAL_FAIL (-10000)General failure talking to the terminal.
- RES_E_AUTO_TRADING_DISABLED (-8)Algo trading is disabled in the terminal.
- RES_E_UNSUPPORTED (-7)The method is not supported.
- RES_E_AUTH_FAILED (-6)Login to the trade account failed.
- RES_E_INVALID_VERSION (-5)Version mismatch.
- RES_E_NOT_FOUND (-4)No history was found for the request.
- RES_E_NO_MEMORY (-3)The terminal ran out of memory.
- RES_E_INVALID_PARAMS (-2)The function was called with invalid arguments.
- RES_E_FAIL (-1)A generic failure.
- RES_S_OK (1)Generic success: the last call to the MetaTrader5 Python package worked.