In Part 90, we talked about a method of version control. This article we’ll discuss error trapping.
For those of you who are still neophyte programmers, error trapping is exactly what it sounds like: the trapping of program errors. You’ll discover that making a program idiot-proof is impossible. There are always more creative idiots. 😉 Seriously, that’s just programmer humor, but the concept holds. It’s not possible to make a program completely crash-proof. But, we still attempt to do so and error trapping is part of the job.
The level of error trapping you need is really dependent upon your audience. If you are going to be the only one using your EAs, then you may need little or no error trapping. If you’re just sharing your EA among friends, then the level of error trapping is dependent upon how often you want an offer of dinner in exchange for fixing your friend’s computer. But if you’re going to be distributing your EA to the general public, you will want good solid error trapping in addition to good solid diagnostic messages in the experts log. That will significantly reduce your tech support duties.
Errors are part of the landscape when you work with computers. Dealing with anomalies is pretty simple if you’re a human. We’re wired to handle exceptions to the rule. But a computer can only do what it’s told. So you as a programmer must anticipate all possible scenarios under which the computer may have to operate. Plus, no matter how hard you try, you as a tech savvy programmer may be completely unable to anticipate the actions of certain users because they are unbelievably creative in breaking things. That is the reason for beta testing. I always tell my beta testers that their job is to break my program. But I digress…
Your first job is to anticipate where errors are most likely to occur. In the case of MT4, aside from the most obvious place (operator error), you are most likely to see errors when attempting to execute or modify orders. You’ve already seen one of my error trapping techniques where opening orders is concerned:
while(IsTradeContextBusy()) Sleep(100); RefreshRates()
A busy trade context – in other words, another EA is already trying to execute a trade – is a common situation in MT4, especially if you have your EA running on multiple charts/pairs. So we circumvent that error with the while() loop above, which continues to loop until the trade context is no longer busy. And then we finish up with RefreshRates() when the loop ends to be sure we have the latest rate data – which is another common problem when executing trades in MT4.
The next issue you may encounter is ECN brokers who do not accept SL & TP along with the original order. I mentioned before that it’s just a plain good idea to enter all your orders without SL & TP and then modify your order to include the SL & TP. I hate doing that because your order is unprotected for a very short time, but it’s the only way to deal with ECN brokers. There is an alternative for handling orders called OrderReliable.mqh. It’s a free, open source MQL4 source file for inclusion in your EAs. It’s designed to be a drop-in replacement for the OrderSend(), OrderModify() and other order functions. It’s very handy and handles a lot of the error trapping. It’s a staple in my programming toolbox. I’ll discuss it further in a future installment
We’ll continue the discussion of error trapping in our next article. Thanks for your attention and please follow me on Twitter and LinkedIn.
Latest posts by admin (see all)
- Money Management in Forex: More Than Just Trading - February 17, 2018
- Identifying Trends through Synchronization - February 17, 2018
- Using Multiple Trendlines to Identify Better Trades - February 15, 2018
Winner’s Edge Trading, as seen on: