• No results found

When placing, modifying or closing orders, errors can occur due to invalid trade parameters, requotes, or server issues. We've done our best to make sure that the trade parameters we use are valid and have been checked to prevent common, preventable errors. But when errors do occur, we need to alert the user of the error and log any relevant information for troubleshooting.

We check for possible errors by examining the output of functions such as OrderSend(),

OrderModify() and OrderClose(). If the function did not complete successfully, the function will return -1 for OrderSend(), or false for OrderModify() and OrderClose().

In this section, we will create an error handling routine for the OrderSend() function. If the return value of OrderSend() is -1, we will run an error handling routine to display an alert to the user, and print relevant trade parameter and price information to the log.

First, we must first retrieve the error code. This is done using the GetLastError() function. We need to store the return value of GetLastError() in a variable, because once GetLastError() has been called, the error code will be cleared and the next call of GetLastError() will return 0. We'll declare a global variable called ErrorCode and use it to store the value of GetLastError(). Next, we'll need to get some descriptive information on the error. The include file stdlib.mqh contains a function called ErrorDescription(). This function returns a string with a description of the error. It's actually not very descriptive, but it's better than nothing. We'll need to add an

#include statement for stdlib.mqh at the top of our file.

Then we'll print an alert to the user's screen using the built-in Alert() function. This information will also be printed to the log. The alert will include the error code, the error description, and a short description of the operation we just attempted to carry out. This way you'll know exactly which section in your program generated the error.

Finally, we will print relevant price information to the log using the Print() function. Along with the current Bid & Ask prices, we will include trade parameters such as the lot size and the order price.

// Preprocessor section

#include <stdlib.mqh>

// Global variable int ErrorCode;

// Order placement

int Ticket = OrderSend(Symbol(),OP_BUYSTOP,LotSize,PendingPrice,UseSlippage,0,0,

"Buy Stop Order",MagicNumber,0,Green);

if(Ticket == -1) {

ErrorCode = GetLastError();

string ErrDesc = ErrorDescription(ErrorCode);

string ErrAlert = StringConcatenate("Open Buy Stop Order - Error ", ErrorCode,": ",ErrDesc);

Alert(ErrAlert);

string ErrLog = StringConcatenate("Bid: ",Bid," Ask: ",Ask," Price: ", PendingPrice," Lots: ",LotSize);

Print(ErrLog);

}

At the top, we include the stdlib.mqh file. We add the ErrorCode global variable to store our error code. The OrderSend() places a buy stop order. If the function is not successful, our error handling code is run.

First, we store the value of GetLastError() in ErrorCode. Then we call the ErrorDescription() function, using ErrorCode as the argument. Next, we use the StringConcatenate() function to create our alert message, which is stored in the string variable ErrAlert.

StringConcatenate() is an MQL function that allows you to create complex strings using variables and constants. Each string element to be joined (or "concatenated") together is separated by a comma. Try typing the examples above into MetaEditor to view it with syntax highlighting.

You can also concatenate strings by combining them with a plus sign (+). Using

StringConcatenate() is clearer and more efficient, but if you want to simply concatenate a short string, use the plus sign to combine string constants and variables:

string PlusCat = "The current Ask price is "+Ask;

// Sample output: The current Ask price is 1.4320

The Alert() function displays a pop-up on the user's desktop, containing the contents of the ErrAlert variable. Figure 3.2 displays the output of the Alert() function.

We construct another string with our price and trade parameters, and store it in the ErrLog variable, which we pass to the Print() function. Print() prints the contents of the function argument to the experts log. The experts log can be viewed from the Experts tab inside the Terminal window, or from the Journal tab in the Tester window if you're using the Strategy Tester.

Fig. 3.2 – Alert message

Here are the log contents. The first line is the output from the Alert() function. The second line is the output of the Print() function. Notice the error, "invalid trade volume", and the fact that the lot size reported in the log is 0. In this case, the problem is that the lot size is invalid.

16:47:54 Profit Buster EURUSD,H1: Alert: Open Buy Stop Order - Error 131:

invalid trade volume

16:47:54 Profit Buster EURUSD,H1: Bid: 1.5046, Ask: 1.5048, Lots: 0

You can create similar error handling routines for other functions as well, especially for the

OrderModify() and OrderClose() functions. You can also create more sophisticated error handling routines that provide custom error messages based on the error code, or perform other actions.

For example, if you receive error 130: "invalid stops", you could display a message such as "The stop loss or take profit price is invalid." Here's an example of how you can do this:

ErrorCode = GetLastError();

string ErrDesc;

if(ErrorCode == 129) ErrDesc = "Order opening price is invalid!";

if(ErrorCode == 130) ErrDesc = "Stop loss or take profit is invalid!";

if(ErrorCode == 131) ErrDesc = "Lot size is invalid!";

string ErrAlert = StringConcatenate("Open Buy Order - Error ",ErrorCode,": ",ErrDesc);

Alert(ErrAlert);

Related documents