- Installing Python and the MetaTrader5 package
- Overview of functions of the MetaTrader5 package for Python
- Connecting a Python script to the terminal and account
- Error checking: last_error
- Getting information about a trading account
- Getting information about the terminal
- Getting information about financial instruments
- Subscribing to order book changes
- Reading quotes
- Reading tick history
- Calculating margin requirements and evaluating profits
- Checking and sending a trade order
- Getting the number and list of active orders
- Getting the number and list of open positions
- Reading the history of orders and deals
Error checking: last_error
The last_error function returns information about the last Python error.
int last_error()
Integer error codes differ from the codes that are allocated for MQL5 errors and returned by the standard GetLastError function. In the following table, the abbreviation IPC refers to the term "Inter-Process Communication".
Constant |
Meaning |
Description |
---|---|---|
RES_S_OK |
1 |
Success |
RES_E_FAIL |
-1 |
Common error |
RES_E_INVALID_PARAMS |
-2 |
Invalid arguments/parameters |
RES_E_NO_MEMORY |
-3 |
Memory allocation error |
RES_E_NOT_FOUND |
-4 |
Requested history not found |
RES_E_INVALID_VERSION |
-5 |
Version not supported |
RES_E_AUTH_FAILED |
-6 |
Authorization error |
RES_E_UNSUPPORTED |
-7 |
Method not supported |
RES_E_AUTO_TRADING_DISABLED |
-8 |
Algo trading is disabled |
RES_E_INTERNAL_FAIL |
-10000 |
General internal IPC error |
RES_E_INTERNAL_FAIL_SEND |
-10001 |
Internal error sending IPC data |
RES_E_INTERNAL_FAIL_RECEIVE |
-10002 |
Internal error sending IPC data |
RES_E_INTERNAL_FAIL_INIT |
-10003 |
IPC internal initialization error |
RES_E_INTERNAL_FAIL_CONNECT |
-10003 |
No IPC |
RES_E_INTERNAL_FAIL_TIMEOUT |
-10005 |
IPC timeout |
In the following script (MQL5/Scripts/MQL5Book/Python/init.py), in the case of an error when connecting to the terminal, we display the error code and exit.
import MetaTrader5 as mt5
|