How to get Error Descriptions of GetLastError() and Example of how to use Return Codes of the Trade Server

 

Hello,

as stated in the Subject is there a way to receive the error descriptions (https://www.mql5.com/en/docs/constants/errorswarnings/errorcodes) of GetLastError()?

I also would like to use Trade Server Return Codes but I have no idea how to use/receive them in the code. Is anyone having an example for this?

Thank you!

Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Runtime Errors
Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Runtime Errors
  • www.mql5.com
Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

You can find a library that has some error descriptions on Github.


Or you can always code them manually...


string errorDescription(int error){

  switch (error) {

    case 0:     return("Error 0:  No error returned.");

    ...
    ...

}


If you pressed "Ctrl" and selected all the codes and description from the table separately then you can easily get the complete switch function with all the error codes and their description...

 

Hello Amine, thanks for your feedback.

Yes, I was thinking of coding it like you suggested but thought maybe there is a more sufficient way or already implemented function to receive the descriptions.

Do you have any idead how to receive the "Return Codes of the Trade Server". From the description "All requests to execute trade operations are sent as a structure of a trade request MqlTradeRequest using function OrderSend(). The function execution result is placed to structure MqlTradeResult, whose retcode field contains the trade server return code." it is not clear how to receive it. As far as I understood are this the error codes thrown back by the broker when you try to open, close or modifiy a position. Thus, in case any errors occur, it would of course be very helpful to receive the error code.

Thank you.

 

Error description MQL header for use with MT5 is in the codebase: https://www.mql5.com/en/code/79.

The trade result will be submitted to you in OnTradeTransaction() as stated in the documentation.

ErrorDescription
ErrorDescription
  • www.mql5.com
The library contains functions that returns description of runtime error codes and trade server return codes.
 
lippmaje:

Error description MQL header for use with MT5 is in the codebase: https://www.mql5.com/en/code/79.

The trade result will be submitted to you in OnTradeTransaction() as stated in the documentation.

Hi lippmaje, thanks for the hint! that is basically what I wanted. I just had a brief look at the .mqh and I do not understand how the functions know which description to return as - as shown in the example - you enter an error code but in the functions they use TRADE_* and ERR_*. How is the integer code mapped to these definitions? As I can see, the .mqh file does not inlcude other libraries where this mapping could happen. It probably works, I just would like to understand how. Any idea? - Thank you!
 
ammer.jens I do not understand how the functions know which description to return as -
You pass the error code, and you get the description.
 Print("Runtime error code:",i,ErrorDescription(i));

What part of that is unclear?

 

Hello William,

yes, that is not the part which is unclear. The passed param i is an integer such as 10004 as stated in the example BUT within the function ErrorDescription() the switch statement uses no integer but for the case part TRADE_* and ERR_* and as return the description. So I am wondering how 10004 is mapped to any of the ERR_ values?! Maybe because the are predefined mql5 variables mql5 knows that 10004 belongs to - I don't know - ERR_INTERNAL_ERROR or whatever. But this mapping between 10004 and any of the ERR_ variables is not clear to me. Thanks

 
MQL maps this for you. Consider ERR_... and TRADE_RETCODE_... to be constants of integer type.
 

TRADE* and ERR* are predefined constant integers in mql5

 
Ok, thanks for clarification everyone!
Reason: