Trader’s Tech – Writing Your Own EA Part 92 – 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 91, we began a discussion of Error Trapping. We’ll continue that discussion in this installment.

As I mentioned earlier, the level to which you trap errors is dependent upon your EA’s audience. I usually “rough in” the error trapping when I originally code the EA, but disable it while the EA is still in testing. Error trapping can often disguise errors and make debugging more difficult. I like to see my errors in all their raw glory while I’m testing so I don’t have to figure out what maybe hiding them. It’s like a roofer trying to find a roof leak by looking at where the water is appearing inside the house. The water can run along beams and studs and come out in a totally different place  far away from the original roof leak.

In some programming languages (read – most programming languages) there is an overall error trapping statement that can be executed at the beginning of the program that will redirect to an error trapping function automatically when an anomaly occurs.  MQL4 doesn’t have that luxury. You have to anticipate the code segments that could possible generate error conditions and make allowance for them. For example, if you have a mathematical formula to calculate that includes division, you must make allowance for the divisor to be equal to zero (division by zero is undefined.) If there is a possibility that your divisor will be zero (and I ALWAYS assume that possibility), you should code the formula so that it can’t happen:

double quotient,dividend,divisor;

if(divisor != 0)
   quotient = dividend / divisor;
else
   quotient = 0;

 

This is one of those rare situations where I’m OK with the else statement. Remember my Zero variable for allowing for infinitesimally small price values? That’s not even necessary to use here because even an infinitesimally small value for the divisor would keep the Divide By Zero error from occurring.

Next time we’ll discuss other situations where error trapping will be necessary in programming your EA. 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