Thursday, January 26, 2012

Robust Expert Advisors

When you want to program an Expert Advisor, it is important to consider how you are likely to use the file. The most common mistake that I see new MQL programmers make is that they rely too heavily on memory. Events like a power failure, a hard drive crash or weak internet connection can wreak havoc when trading with an Expert Advisor. The EA should ideally be able to pick up and run whenever you are able to restore conditions to normal.

Memory issues


Problems sometimes result from something mundane like changing the settings. I learned that the hard way when a client came to our Dallas office for two weeks to order a very complex EA. Everything worked perfectly in the backtester. The overnight tests, however, never lasted for more than a few hours before something went haywire. I instructed the client not to change the settings; just turn it on and leave it alone. For whatever reason, the importance of those instruction never sank in. It took me ages to realize that his definition of "leaving it alone" didn't quite mesh with mine. The little tweaks that he made during trading messed with the settings that I stored in memory.

I can instruct a client to use software in a certain manner. More often than not, they deviate from the instructions to some degree. It's human nature. I learned from those challenges, like any professional should. Our programming templates changed in structure to minimize the need for special instructions.

Global variables


Global variables are a common tool to avoid the issue of relying on memory. When an EA is removed from the chart, the global variables that it created are accessible for you, the EA or any other EA to read from anywhere inside of MetaTrader.

Although the use global variables sounds sophisticated, it's actually a very simplistic approach. MetaTrader maintains a hidden text file in its installation directory with the variable name, the value that it is storing and the time it was last modified. Whenever more than 6 weeks elapsed since the last time MQL used the global variable, MetaTrader automatically deletes it.

A Martingale system might opt to use a global variable to track a simple statistic. Martingales are wild with their approach to risk, so a trader could realistically want to know at a glance the maximum level traded. Global variables make a trivial task out of that need. If the next level to trade is greater than the global variable, then the MQL code should update the global variable.

The only item to remember is that the global variables expire after six weeks. The EA might need to refresh the information by resetting the global value to its current value. Doing so would keep the variable fresh from MetaTrader's perspective.

A screen shot of global variables in MetaTrader 4

Read information instead of storing it



I prefer to rely on MQL's OrderSelect() command to recreate whatever information I need. Although it is more complex, the advantage is that the EA can function any time, any place, exactly as designed. If the hard drive crashes, it's not a problem from a recovery perspective. You are still in trouble during the down time, though. You can load the same EA on your MT4 account and have it immediately reconstruct the information.

Taking the above Martingale scenario, we could reconstruct the number of levels traded by comparing the open and close times of trades. If trade #1 has a close time of 2011 Dec. 5 00:00 and trade #2 also has a close time of 2011 Dec. 5 00:00, it tells us that they are part of the same trade group. So far, we have 2 levels traded. If trade #5 closed at 2011 Dec. 5 00:00 but no trades did thereafter, then we know that we traded 5 levels total. The EA can then loop through all possible combinations and pick out the highest number from that.

The approach of reading from the order history is a bit of overkill for this simple example, but it comes in handy when you need to calculate more complicated information dynamically and with a minimal risk of error.