Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2010.02.09 12:45
Libraries

ErrorDescription - library for MetaTrader 5

| English Русский 中文 Español Deutsch 日本語 Português

Views:
5864
Rating:
votes: 33

The library contains the following functions:

//+------------------------------------------------------------------+
//|                                             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:

(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,TradeServerReturnCodeDescription(i));
     }
   Print("-------- Description of runtime error codes ---------");
   for(int i=4001;i<=4014;i++)
     {
      Print("Runtime error code:",i,ErrorDescription(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,ErrorDescriptionExt(GetLastError(),MyErrors));
     }
  }
//+------------------------------------------------------------------+

Translated from Russian by MetaQuotes Software Corp.
Original code: https://www.mql5.com/ru/code/79

LoongClock LoongClock

A very simple sample of clock

MovingAverages MovingAverages

The MovingAverages library contains functions for calculation of different types of moving averages.

TimerClosingPeriod TimerClosingPeriod

The indicator prints the time to close of the current timeframe, if it less than H1, it also prints the time to close of the current hourly bar.

LoongMAx96 LoongMAx96

Draws 96 line MAs with only 100 lines of code (uses CMyBuffer class).