Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

Metatrader 5 Libraries | ErrorDescription | Trading Tool Update

Fresh MQL5 code release – perfect for enhancing your MT4/MT5 strategies.

Image for ErrorDescription

The library contains the following functions:

  • TradeServerReturnCodeDescription - returns description of trade server return codes;
  • ErrorDescription - returns description of runtime errors.
//+------------------------------------------------------------------+
//|                                             ErrorDescription.mqh |
//|                        Copyright 2010, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| returns trade server return code description                     |
//+------------------------------------------------------------------+
string TradeServerReturnCodeDescription(int return_code)
//+------------------------------------------------------------------+
//| returns runtime error code description                           |
//+------------------------------------------------------------------+
string ErrorDescription(int err_code)

Example:

Reduce lag and improve accuracy with the NonLagMA Expert Advisor for MT4/MT5. Discover it.

(Don't forget to copy the file ErrorDescription.mq5 to the folder \MetaTrader 5\MQL5\Include)

//+------------------------------------------------------------------+
//|                                               ErrorDescrTest.mq5 |
//|                        Copyright 2010, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#include <ErrorDescription.mqh>
//+------------------------------------------------------------------+
//| Example of use of the ErrorDescription.mqh library               |
//+------------------------------------------------------------------+
void OnStart()
  {
   Print("----- Description of trade server return codes -----");
   for(int i=10004;i<=10034;i++)
     {
      Print("Trade server return code:",i,Print("-------- Description of runtime error codes ---------");
   for(int i=4001;i<=4014;i++)
     {
      Print("Runtime error code:",i,//+------------------------------------------------------------------+

In some cases it's necessary to work with user defined errors. In MQL5 there is a function SetUserError, what sets the predefined variable _LastError into the value equal to ERR_USER_ERROR_FIRST + user_error.

The user defined error codes starts from code ERR_USER_ERROR_FIRST. In such cases you can use the function ErrorDescriptionExt to return description of errors, including the user defined errors:

//+------------------------------------------------------------------+
//|                                               UserErrorDescr.mq5 |
//|                        Copyright 2010, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#include <ErrorDescription.mqh>
//+------------------------------------------------------------------+
//|  returns runtime error code description,                         |
//|  with user defined errors                                        |
//+------------------------------------------------------------------+
string ErrorDescriptionExt(int err_code,string&user_errors[])
  {
   if(err_code>=0 && err_code<ERR_USER_ERROR_FIRST) return(ErrorDescription(err_code));
//--- user defined runtime errors
   err_code-=ERR_USER_ERROR_FIRST;
   if(err_code<=ArraySize(user_errors)) return(user_errors[err_code]);
//---
   return("Unknown error");
  };

// an array with description of the user defined runtime errors
string MyErrors[]=
  {
   "User error №1",
   "User error №2",
   "User error №3"
  };
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   for(int i=0;i<=2;i++)
     {
      SetUserError(i);
      Print("User defined error code:",i,GetLastError(),MyErrors));
     }
  }
//+------------------------------------------------------------------+

Build better strategies with RobotFX professional tools – check them out.

79

Best MetaTrader Indicators + Profitable Expert Advisors