MQL4 Book  Creation of a Normal Program  Error Processing Function

Error Processing Function

The errors that appear during trade orders execution can be divided into two groups - overcomable (non-critical) errors and critical errors. Overcomable errors are those occurring at server faults. After they have been eliminated, you can continue trading. For example, a request can be rejected by the broker, if there is no information about the current quotes at the moment. This kind of situation may appear in a slow market, i.e., when ticks don't income frequently. Or, on the contrary, the broker cannot always execute a plenty of requests from traders in an active market where too many quotes are coming. Then the pause appears before the order is executed or - sometimes - a denial. In such cases, the Expert Advisor may continue its working and, for example, repeat the request a bit later after the execution of some code related to the error code.

Critical errors include all errors that alert about serious troubles. For example, if an account is blocked, then there is no point in sending trade requests. In such case, the EA should display the corresponding message and shouldn't repeat the request. The error processing function must be used indispensably in a normal EA.

Custom Error Processing Function Errors()

bool Errors( int Error )

The function returns TRUE, if the error is overcomable. Otherwise, it returns FALSE.
The Error parameter can have any value and correspond with any error code that occurred while trying to make a trade.

The user-defined error processing function Errors() is designed as the include file Errors.mqh:

// Errors.mqh
// The code should be used for educational purpose only.
//--------------------------------------------------------------- 1 --
// Error processing function.
// Returned values:
// true - if the error is overcomable (i.e. work can be continued)
// false - if the error is critical (i.e. trading is impossible)
//--------------------------------------------------------------- 2 --
bool Errors(int Error) // Custom function
// Error // Error number
return(false); // No error
Inform(15,Error); // Message
//--------------------------------------------------------------- 3 --
{ // Overcomable errors:
case 129: // Wrong price
case 135: // Price changed
RefreshRates(); // Renew data
return(true); // Error is overcomable
case 136: // No quotes. Waiting for the tick to come
while(RefreshRates()==false) // Before new tick
Sleep(1); // Delay in the cycle
return(true); // Error is overcomable
case 146: // The trade subsystem is busy
Sleep(500); // Simple solution
RefreshRates(); // Renew data
return(true); // Error is overcomable
// Critical errors:
case 2 : // Common error
case 5 : // Old version of the client terminal
case 64: // Account blocked
case 133: // Trading is prohibited
default: // Other variants
return(false); // Critical error
//--------------------------------------------------------------- 4 --

One of the questions that arise when composing the algorithms of error processing function Errors() is: What should the function return, if the value of a given parameter is 0 (i.e., there are no errors). This kind of situation should not appear in a correctly coded EA. However, the code can be variously modified while enhancing the program, so sometimes the value of an error can be equal to 0. So, it would be reasonable to add some lines to the function at the primal stage of developing (block 2-3), for the situations where Error is equal to 0.

The reaction of the Errors() function to the zero value of the Error variable depends on the algorithm used for processing the values returned by the function. The value returned by the function is taken into consideration in the executable trade function of the above EA. If the Errors() function returns 'true' (error is overcomable), then the program will retry to make a trade. If it returns 'false', then the trade function stops and the control is sequentially passed to the calling function, then to the start() function, and then to the client terminal. If the choice is between these two alternatives, then the situation when there are no errors (Error=0) corresponds with the second alternative, namely, with the 'false' value returned. It guaranties that a once executed request will not be repeated.

Once the message about the error is displayed by the Inform() function, the control is passed to block 3-4, to the operator 'switch'. The specific variant case is involved for every considered error code. For example, if error 136 occurs, it means that the broker does not have current quotes for making a proper decision. This means that the situation won't change unless a new tick comes, so there is no need to repeat sending the same trade request because it won't be executed, anyway. The right solution in this situation is the pause - the absence of any initiative from the EA. A simple method to detect a new tick is used for this purpose - the analysis of the value returned by the RefreshRates() function. The control will be returned to the called function, in which the trade request is repeated (after the corresponding analysis, if necessary), as soon as the new tick comes.

If there is an error the programmer considers to be a critical one, the function returns 'false'. The request will not be repeated, in such case, so there is no need to do anything in the Errors() function. All errors not processed are considered as critical by default. You can expand the list of processable errors (see Error Codes).