Trader’s Tech – Writing Your Own EA Part 94 – Error Trapping

Forex Robot If you’re new to this series and want to check it out from the beginning, you can find that here. And look here for a list of all the programming articles.

In Part 93, we continued our discussion of Error Trapping. We’ll wrap that up with a discussion of order error trapping in this installment.

There are a several reasons a trade error may occur. We discussed one previously: Busy Trade Context. As I mentioned, we can protect against that error with our simple while() loop. In addition, there are other potential error conditions in opening orders; ERR_INVALID_PRICE, ERR_INVALID_STOPS, ERR_MARKET_CLOSED, ERR_INVALID_ACCOUNT, ERR_NOT_ENOUGH_MONEY, ERR_PRICE_CHANGED, ERR_MARKET_CLOSED are a few examples. You can see them all here. These errors can occur during any trading operation (OrderSend(), OrderClose(), OrderModify(), etc.) These error codes are generated by MT4 and your first clue an error has occurred will be the returned value of the trading function. As I’ve mentioned before, the OrderSend() function returns the ticket number if the order has been entered and returns a -1 if there was an error. To retrieve the error code, you must use the GetLastError() function.

I’ve said it before and you must remember the GetLastError() function clears the last error when you call it, so you should start by assigning the value to a variable:

int Ticket,LastError;

Ticket = OrderSend("EURUSD"...
if(Ticket == -1)
   {
   LastError = GetLastError();
   //Error Checking Code
   }  //if(Ticket == -1)

 

You may want to set up your order entry in a loop so it retries in the case of an error situation. Your error checking code would then include breaks so the loop would stop in the case of an unrecoverable error (e.g. ERR_NOT_ENOUGH_MONEY or ERR_OLD_VERSION.) Also, be sure you put a counter in the loop to be sure it doesn’t lock your program up.

int Ticket, LastError, i;
int MaxCntr = 3;

i = 0;
Ticket = -1;
while(Ticket == -1 && i < MaxCntr)
   {
   Ticket = OrderSend("EURUSD"...
   if(Ticket == -1)
      {
      LastError = GetLastError();
      if(LastError = ERR_NOT_ENOUGH_MONEY)
         break;
      //additional error checking code
      }  //if(Ticket == -1)
   i++;
   }  //while(Ticket == -1 && i < MaxCntr)

 

Errors like ERR_INVALID_PRICE and ERR_INVALID_STOPS shouldn’t happen, so I don’t think I would trap them. If they happen, you probably haven’t checked your numbers properly before attempting to open the trade. I’ll discuss how to validate the order prices in a future installment.

I would like to reiterate something important. Certain errors should never occur at runtime. Divide by zero is a perfect example of this. Any division operation that could potentially create a divide by zero error should be handled in the code so it can’t happen at runtime – even if you think it could never happen. Obviously, since we’re human, we’re prone to making a mistake (I’ve demonstrated that several times during the course of this series – even though I’ve been a programmer for about – ahem – 40 years or so), that’s why I suggest you disable the error trapping code until you’ve thoroughly tested the program. You may end up disguising  a stupid programming error and cause yourself a lot of grief searching for it.

Next article we’ll discuss, as promised, the OrderReliable.mqh group of functions. Thanks for your attention and please follow me on Twitter and LinkedIn.

Tim

The following two tabs change content below.
Winners Edge Trading was founded in 2009 and is working to create the most current and useful Forex information and training available on the internet.

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news