错误检查:last_error

last_error 函数返回关于上一个 Python 错误的信息。

int last_error()

整数错误代码不同于分配给 MQL5 错误并由标准 GetLastError 函数返回的代码。在下表中,缩写 IPC 是指术语“进程间通信”。

常量

意义

说明

RES_S_OK

1

成功

RES_E_FAIL

-1

常见错误

RES_E_INVALID_PARAMS

-2

自变量/参数无效

RES_E_NO_MEMORY

-3

内存分配错误

RES_E_NOT_FOUND

-4

未找到请求的历史

RES_E_INVALID_VERSION

-5

不支持的版本

RES_E_AUTH_FAILED

-6

授权错误

RES_E_UNSUPPORTED

-7

不支持的方法

RES_E_AUTO_TRADING_DISABLED

-8

算法交易被禁用

RES_E_INTERNAL_FAIL

-10000

一般内部 IPC 错误

RES_E_INTERNAL_FAIL_SEND

-10001

发送 IPC 数据时出现内部错误

RES_E_INTERNAL_FAIL_RECEIVE

-10002

发送 IPC 数据时出现内部错误

RES_E_INTERNAL_FAIL_INIT

-10003

IPC 内部初始化错误

RES_E_INTERNAL_FAIL_CONNECT

-10003

无 IPC

RES_E_INTERNAL_FAIL_TIMEOUT

-10005

IPC 超时

在以下脚本 (MQL5/Scripts/MQL5Book/Python/init.py) 中,在连接到终端时出现错误的情况下,显示错误代码并退出。

import MetaTrader5 as mt5
# show MetaTrader5 package version
print("MetaTrader5 package version: ", mt5.__version__)  #  5.0.37
   
# let's try to establish a connection or launch the MetaTrader 5 terminal
if not mt5.initialize():
   print("initialize() failed, error code =", mt5.last_error()) 
   quit()
... # the working part of the script will be here
# terminate the connection to the terminal
mt5.shutdown()