# MT5 Python last_error()

Independent and unofficial. Not affiliated with or endorsed by any broker or platform named here. Educational only: nothing here is advice, and nothing here recommends a broker or a trade.

This is the markdown version of https://openbrokerapi.com/platforms/metatrader-5/functions/python-last-error, written from the same records as that page.

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.

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.

- Function: `last_error()`
- Interface: MetaTrader5 Python package
- Group: Connection
- Status: verified. Source: [mql5.com](https://www.mql5.com/en/docs/python_metatrader5/mt5lasterror_py), checked 2026-10-12.

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. It is in no OpenAPI file on this site and the request builder does not list it.

## Signature

The form of the call on the vendor's page, with the vendor's comments taken out.

```python
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.

```python
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. Status: verified. Source: [mql5.com](https://www.mql5.com/en/docs/python_metatrader5/mt5lasterror_py), checked 2026-10-12.

- [RES_E_INTERNAL_FAIL_TIMEOUT (-10005)](https://openbrokerapi.com/errors/mt5/res-e-internal-fail-timeout): The terminal did not respond in time.
- [RES_E_INTERNAL_FAIL_INIT (-10003)](https://openbrokerapi.com/errors/mt5/minus-10003-res-e-internal-fail-init): IPC initialisation failed; the docs also list RES_E_INTERNAL_FAIL_CONNECT (internal IPC no ipc) under -10003.
- [RES_E_INTERNAL_FAIL_CONNECT (-10003)](https://openbrokerapi.com/errors/mt5/res-e-internal-fail-connect): No interprocess link (IPC) between the Python package and the terminal.
- [RES_E_INTERNAL_FAIL_RECEIVE (-10002)](https://openbrokerapi.com/errors/mt5/res-e-internal-fail-receive): Receiving from the terminal failed.
- [RES_E_INTERNAL_FAIL_SEND (-10001)](https://openbrokerapi.com/errors/mt5/res-e-internal-fail-send): Sending to the terminal failed.
- [RES_E_INTERNAL_FAIL (-10000)](https://openbrokerapi.com/errors/mt5/res-e-internal-fail): General failure talking to the terminal.
- [RES_E_AUTO_TRADING_DISABLED (-8)](https://openbrokerapi.com/errors/mt5/minus-8-res-e-auto-trading-disabled): Algo trading is disabled in the terminal.
- [RES_E_UNSUPPORTED (-7)](https://openbrokerapi.com/errors/mt5/res-e-unsupported): The method is not supported.
- [RES_E_AUTH_FAILED (-6)](https://openbrokerapi.com/errors/mt5/res-e-auth-failed): Login to the trade account failed.
- [RES_E_INVALID_VERSION (-5)](https://openbrokerapi.com/errors/mt5/res-e-invalid-version): Version mismatch.
- [RES_E_NOT_FOUND (-4)](https://openbrokerapi.com/errors/mt5/res-e-not-found): No history was found for the request.
- [RES_E_NO_MEMORY (-3)](https://openbrokerapi.com/errors/mt5/res-e-no-memory): The terminal ran out of memory.
- [RES_E_INVALID_PARAMS (-2)](https://openbrokerapi.com/errors/mt5/res-e-invalid-params): The function was called with invalid arguments.
- [RES_E_FAIL (-1)](https://openbrokerapi.com/errors/mt5/res-e-fail): A generic failure.
- [RES_S_OK (1)](https://openbrokerapi.com/errors/mt5/1-res-s-ok): Generic success: the last call to the MetaTrader5 Python package worked.

## Related MT5 Python functions: Connection

- [initialize()](https://openbrokerapi.com/platforms/metatrader-5/functions/python-initialize): Connects the Python package to a MetaTrader 5 terminal on the same computer, starting the terminal if it is not running, and can log in to a trading account in the same call.
- [login()](https://openbrokerapi.com/platforms/metatrader-5/functions/python-login): Switches the connected terminal to a trading account, by account number, with the password and server optional.
- [shutdown()](https://openbrokerapi.com/platforms/metatrader-5/functions/python-shutdown): Closes the connection that initialize() opened between the Python package and the terminal.
- [terminal_info()](https://openbrokerapi.com/platforms/metatrader-5/functions/python-terminal-info): Returns the status and settings of the connected terminal in one call.
- [version()](https://openbrokerapi.com/platforms/metatrader-5/functions/python-version): Returns the version of the connected MetaTrader 5 terminal, with its build number and the date of that build.

## More on MetaTrader 5

- [All MetaTrader 5 functions](https://openbrokerapi.com/platforms/metatrader-5/functions): 78 documented here, grouped by what they do.
- [MetaTrader 5 API quickstart](https://openbrokerapi.com/platforms/metatrader-5/quickstart)
- [All MetaTrader 5 error codes](https://openbrokerapi.com/errors/mt5)
- [MetaTrader 5 API page](https://openbrokerapi.com/platforms/metatrader-5): The full record: access, authentication, limits and the source for each.

## Licence and how to cite

The records behind this page are all rights reserved. This file is the page itself, at its own address, in another format. It is not part of the CC BY 4.0 downloads listed at https://openbrokerapi.com/data. Quote from it with a link to the page.

Cite as: MT5 Python last_error(). Broker API Hub, https://openbrokerapi.com/platforms/metatrader-5/functions/python-last-error, newest check 2026-10-12.

Where you rely on a single figure, cite the source and the checked date printed beside it as well. Where this page and a broker's own documentation differ, the documentation wins.
