Comprobación de errores: last_error

La función last_error devuelve información sobre el último error de Python.

int last_error()

Los códigos de error de números enteros difieren de los códigos asignados a los errores MQL5 y devueltos por la función GetLastError estándar. En la siguiente tabla, la abreviatura IPC significa «Comunicación entre procesos».

Constante

Significado

Descripción

RES_S_OK

1

Éxito

RES_E_FAIL

-1

Error común

RES_E_INVALID_PARAMS

-2

Argumentos/parámetros no válidos

RES_E_NO_MEMORY

-3

Error de asignación de memoria

RES_E_NOT_FOUND

-4

No se ha encontrado el historial solicitado

RES_E_INVALID_VERSION

-5

Versión no compatible

RES_E_AUTH_FAILED

-6

Error de autorización

RES_E_UNSUPPORTED

-7

Método no admitido

RES_E_AUTO_TRADING_DISABLED

-8

La negociación Algo está desactivada

RES_E_INTERNAL_FAIL

-10000

Error general interno de IPC

RES_E_INTERNAL_FAIL_SEND

-10001

Error interno al enviar datos de IPC

RES_E_INTERNAL_FAIL_RECEIVE

-10002

Error interno al enviar datos de IPC

RES_E_INTERNAL_FAIL_INIT

-10003

Error de inicialización interna de IPC

RES_E_INTERNAL_FAIL_CONNECT

-10003

No IPC

RES_E_INTERNAL_FAIL_TIMEOUT

-10005

Tiempo de espera de IPC

En el siguiente script (MQL5/Scripts/MQL5Book/Python/init.py), en caso de error al conectar con el terminal, mostramos el código de error y salimos.

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()