MetaTrader Expert Advisor
Convert to MetaTrader 5
Alpari’s recent launch of MetaTrader 5 triggered a small wave of MQL5 translation requests. Most traders assume that MT5 is about to take over the world. Perhaps it’s better to front run any potential problems. I assure you, though, that there’s no need to panic.
The launch mainly signifies that the larger forex firms will start rolling out their own installations of MT5 within the next six to twelve months. Rumor has it that Alpari’s owners are very close to the owners of MetaQuotes. Perhaps this is hearsay, but it’s my impression that Alpari is the first among equals when it comes to MetaQuotes’ clientele. Alpari did pay up the wazoo, however, for their license. Maybe they’re just getting rewarded for adopting the new platform so quickly.
Most brokerages, especially the large ones, are not chomping at the bit to adopt the new release. In fact, most of them hate MetaTrader with a passion. The back office is written largely for brokerages that exclusively want to use MetaTrader. The larger brokers, all of whom invariably offer their own proprietary platforms, have to jump through a lot of hoops to get all the moving parts between separate back office systems working in sync. The rollout will likely embroil their IT staff in problems for months on end. I seriously doubt most CEOs are looking forward to the switch.
Also, offering MT5 as the primary platform does not mean that your brokerage is going to flip the off switch on MT4. They depend on MetaTrader 4 for their cash flow. Brokerages will not sabotage themselves by preventing all of their customers from trading.
Rollouts of new technologies usually occur over a period of 9 months or more. When I worked with FXCM, I remember the handful of clients that refused to switch from Trading Station I to II. It wasn’t until 2 years after the initial release of the new version where the company decided to drag the stragglers kicking and screaming onto version II.
The switch from MetaTrader 3 to 4 worked in much the same way at the brokerages offering it at the time. It wasn’t until two years or so after its initial adoption that version 3 went by the wayside.
You have little to worry about as a retail trader considering the switch over to MT5. If you want to program a brand new EA and your broker already supports MetaTrader 5, then you should definitely program it in MQL5. Otherwise, stick with MetaTrader 4. It still has years of shelf life.
MetaTrader Data
It’s hard to keep track of places where you can download historical price data for MetaTrader. I decided to post a few links here on the blog to collect them all in one place.
FXDD offers historical price data for all the major currency pairs. The prices extend all the way back to 2005. This option is far and away the easiest to use. The files download in .hst format, which means that you don’t have to worry about formatting. The data resolution is at the M1 level. http://www.fxdd.com/en/mt1m-data.html
Alpari used to offer M1 data on its web site. They fluctuated between taking the links down and putting it back up eventually. Their download links, however, have been down for about two years now. It looks like they finally threw in the towel on offering free price histories.
FXCM offers the ability to export historical prices through their Strategy Trader platform. Obtaining the data is simple. The formatting is a little off, so you would need to write a simple program to convert this into an .hst file. http://www.fxcm.com/strategy-trader.jsp
GAIN offers free data, but they seem to make it intentionally difficult to find and work with. They split the data into one week intervals, and the price histories don’t appear to be even based on my quick glance over their csv file. I wouldn’t recommend going to all the trouble of downloading and formatting their data without a compelling reason. http://ratedata.gaincapital.com/
Dukascopy offers extensive price history down to the tick level. If you want tick prices going back quite a way back in time without paying for data, this is the only option that I know of. I have not messed around with it personally. I only know about it from internet chatter for people who seem to feel that they need tick data for their backtesting. The formatting is not designed for MetaTrader. Once again, you would have to write a conversion program to move it into the MetaTrader history center. http://www.dukascopy.com/swiss/english/data_feed/csv_data_export/
Toxic Trade Flow in Forex
I work with a number of expert advisor vendors and CTAs that want to sell their products. Whenever they approach a forex brokerage about partnerships, the inevitable first question the broker asks is, “Is the trade flow toxic?”.
Not all expert advisors are cut from the same cloth. Many trade infrequently. Strategies that trend trade or that trade long term ranges are broker favorites. The flow is predictable. The brokerage’s order book does not shift quickly.
Many large brokerages today still run dealing desks. When an order comes in to buy EUR/USD, the broker acts as counter-party to the trade. The customer is long and the brokerage is now short. Dealing desks handle this situation differently depending on their market perspective. If they do not have an opinion on market direction, then they will simply offset their trade by going long with their liquidity provider, which is usually a top-tier or group of top-tier banks. That relieves the broker of his exposure. He also keeps the mark up in the spread or commission for his efforts.
When the broker expects the market to move a certain direction, the dealing desk may elect to maintain the exposure. The trader’s loss is the broker’s profit and vice versa. The dealing desk wants to ensure that it is not overleveraged, but also to ensure that it maintains the desired level of exposure (say, for example, 100 standard lots long EURUSD). Traders that shift their positions rapidly force the dealing desk to constantly and quickly update its own position size.
Brokers with market positions hate scalping EAs. Trades that enter and exit quickly interfere with the broker’s goal of maintaining a position of a certain size. The amount of work required to maintain the exposure generally does not offset the amount of profit earned from the scalping trader. It’s business they don’t want. The industry term for unwanted business is toxic order flow or toxic trade flow.
Toxic orders and low liquidity
Another other kind of toxic expert advisor is one that takes advantage of price movements stemming from low liquidity. FAP Turbo is most infamous of this type. Whenever the price of EURGBP or EURCHF spiked quickly, the FAP Turbo expert advisor attempted to enter quickly on the movement. More often than not, the price quickly retraced to the take profit level of 3-5 pips.
Traders loved it. It was the closest thing to near instant profit. Brokers, on the other hand, despised FAP Turbo. Most brokers don’t care if individual traders make money off of them. When 10% of their customer base earns off of them and does so all at the same time, it’s a massive problem. The order flow was toxic to their businesses or to those of their liquidity providers.
The EURGBP typically offers market depth of 2-3 million euros, which is a pittance compared to the majors. Now consider that there were several thousand FAP Turbo users at the height of its popularity. Although most only traded mini lots, a substantial portion of them traded standard lots. Think about the combined order size of 2,000 traders while there is only 3 million available in liquidity. It just doesn’t work. There are too many dogs chasing a single piece of meat. The first ones to get their orders across got filled, but the back of the pack gets stuck either with scraps or nothing at all.
The traders that did win get on the brokers’ nerves for two reasons. It’s not that the traders won. It’s that they won and it only took them seconds to do so. Brokerages that offset their risk do not have sufficient time to do so. Brokerages that don’t offset their risk do not have sufficient time to enjoy the exposure that they are after.
The liquidity providers that filled the first wave of orders are not happy, either. This is probably the biggest problem of all. All forex brokerages rely on the dealing desks of major banks (i.e., liquidity providers) to some degree. There are only 10-15 major banks worth dealing with. It’s a small, almost incestuous community. If you get a bad name with one of them, you get a bad name with all of them.
The liquidity providers go back to the brokerages and dictate that they either shut out the toxic order flow or threaten to sever the liquidity relationship. When the choice is between a critical supplier or a single customer, it’s always the customer that gets the boot.
Optimize an Expert Advisor
One of the lesser known features of the MetaTrader backtester is the optimization feature. It’s so small that you could be forgiven for overlooking it.
Optimization is the process to maximize a certain outcome. In this case, it’s profit. Any EA developer wants to maximize the amount of profit made over a given period of time. The MetaTrader optimizer allows the trader to search for the combination of inputs that yielded the maximum profit over a given period of time.
The process is identical to running a backtest, except that MT4 runs multiple backtests at the same time. It then organizes the results and offers up the best combination.
Telling the backtester to run in optimization mode is easy. Simply put a check next to the word Optimization. MetaTrader will then sort through the combinations that you tell it to consider.
The next step is to click on the Expert properties button to the right. A new window appears that contains three tabs: Testing, Inputs and Optimization. These screens allow the trader to inform MetaTrader which variables to consider for testing and how to weight the results.
Testing
The top of the testing section applies to every type of backtest. Here you can select the starting balance. MetaTrader defaults the option to $10,000, although you can make this any amount of your choosing.
The second default option allows the trader to restrict the direction of trades. It’s a frequent expert advisor programming request. It’s also one that is unnecessary. Both the backtester and expert advisor options screen allow the trader the option of restricting trades to long only or short only without additional programming. If the EA is not well programmed, this setting may cause errors 4110 or 4100 to appear all over the trading journal. It’s harmless. The only effect should be that the backtester slows down. It’s the result of writing to the journal hundreds of times or more.
A groupbox appears underneath these options that inexplicably relates to the optimization process. You’d think it would make more sense to place it in its namesake tab. That’s typical MetaQuotes logic at work.
The first line contains numerous parameters for choosing the best option. User overwhelmingly select for the largest account balance, but other options include the profit factor, expected payoff, maximum drawdown and drawdown percent.
The last line automatically uses a genetic algorithm. Optimization processes use either brute force methods or genetic algorithms. Brute force strikes most people as intuitive although obviously exhausting. The software tests every combination possible. Genetic algorithm’s attempt to make the process more intelligent. When the software sees that certain parameters almost inevitably lead to a losing performance, the algorithm skips similar tests where it expects to lose.
This is a great idea if you have a quality genetic algorithm. My opinion of the MetaTrader backtester is less than stellar. I don’t feel very confident about the algorithm at all. If you don’t mind spending extra time waiting for test results then I suggest unchecking this option. You don’t want to miss a potentially important combination.
Inputs
Most people find this screen confusing. The first column, called value, strictly controls inputs for simple backtests. The Value column is totally ignored during an optimization run.
The important columns for this task are Start, Step and Stop. Start is the lowest number that the backtester will consider. Step refers to the interval between the lowest value and the highest value. Tightly controlling this setting allows the user to gain quick insights into how changing the variable values affects performance without dragging the tests out for a full week. Stop is the highest number that the expert advisor will use.
The most obvious candidate for testing in this example is the Take Profit value. The default setting is listed at 50. If you trade the majors, you might want to consider settings ranging between 10 pips and 200 pips. That means that you set Take Profit row to 10 for the Start column and 200 for the Stop column. The real trick here is selecting the Step. If you choose Step = 1, then MetaTrader will run a separate test for every value between 10 and 200. That’s 190 tests, which is overkill. A step of 10 cuts the total number of tests down to 19.
Optimization
This section is the nit-picky part. If a trader feels it’s unacceptable to have 10 consecutive losses in a row, he can place a check next the the Consecutive wins box. MT4 automatically discards any tests which yield a result that contains anything checked off.
When you finish going through each of the tabs, push OK in the bottom right corner. It’s time to launch the tests.
Curve fitting in the MT4 Optimizer
A word of warning: my personal opinion is that optimizing an expert advisor is usually a very bad idea. The unique settings that yield the most profit in 2012 are unlikely to yield the most profit in 2013. If you don’t control for random chance, there’s a good probability that the 2012 best combination may result in catastrophic losses in 2013.
I recommend that traders pursue any strategy development work in NinjaTrader. I don’t like the idea of optimizing at all. Instead, I always focus on testing strategies for entry and exit efficiency. I know from years of experience that these values never fundamentally change on instruments of the charts traded. Entry and exit efficiencies make wonderful metrics for automated trading because they are so stable.
Volatility & Divergence Commentary
This week has been an ideas week. An unusual number of clients are asking for my opinion on the ideas that they want to program into an expert advisor. Divergence and volatility keep popping up as themes for the week.
Simple Volatility Filter
Volatility is one of those factors that you cannot ignore in trading. It highlights the overall risk context of the market and says something about the likelihood for a trade to get some wheels.
The number of tools that we have to study volatility is unfortunately very limited. Almost everyone uses ATR, which is the average true range. The calculation for it is very basic. The true range is simply the high minus the low. The ATR is simply the average of all the true ranges over a certain period. Most traders use a 14 period ATR by convention.
I sent the chart below to a client in Australia yesterday who asked if I had any ideas for a volatility filter. It compares a fast and slow volatility window using ATR. The red line represents the 14 period ATR, which I call the fast line. The blue line represents the 300 period ATR, which I call the slow line. I suggested that period he could use the fast line appearing above the slow line as an indicator of high volatility. The opposite indication would indicate low volatility.
I created the above chart by dragging and dropping the ATR custom indicator onto a chart. I then dragged and second ATR indicator onto the first ATR indicator. Doing that way overlaps the lines. 0therwise, you would see two lines in separate windows.
When I opened MetaTrader again this morning, the same chart was left open. I immediately noticed that the line crossings appeared to match up with some of the longer term trends. Although it would not indicate the direction of a trend, the ATR crossings might prove useful as a trend detection indicator. If you decide to research this idea, please leave your comments and observation on the blog page below. I enjoy hearing from my readers.
Divergence
I buy into the idea that the market contains price points that are more relevant than others. A lot of the math that I work with involves autocorrelation, which many refer to as the long term memory function. It’s a mathematical tool that allows nerds like myself to find hidden statistical patterns among a bunch of noise in a signal.
Divergence takes a similar idea and applies it to indicators, the most common of which are the MACD, RSI and stochastics. When the price rises above a previous critical point and the indicator does not exceed its previous critical point, then divergence exists. Most traders claim that divergence signals the potential end of a trend.
My biggest gripe with divergence is that the length of trends exhibit random periods. I’ve done plenty of independent research on this topic. Regardless of the method that you use to pick market tops and bottoms or how you define a trend, the time period of the measured trend is always random. It has a probability density, but it definitely does not have a set number.
Divergence completely fails to address this concern. There’s no reason why you can’t have 2 divergences or even 5 divergences in a trend. Divergence does not help the trader distinguish between the end of a trend or a continuing trend. You could use divergence as a trend detection tool, but by that point some traders are already calling for it to end. My personal opinion is that it’s not very useful.
My other complaint with divergence is that the method for picking critical points is totally arbitrary. If you put 10 traders in a room and ask them to draw a trend line, you will get 10 different answers. The absence of consensus on such a basic concept ought to say a great deal about the value of subjective interpretation.
Traders also attempt to draw the points between swing highs and lows. That task should be obvious, but it’s not. I always recommend using the zig zag indicator when customers want to go down the swing trading route. They quickly discover the same problem – how sensitive should the settings be. Again, we circle back to the issue of period length. The swing high that Bob’s Zig Zag settings draw looks like market noise to the swing highs that Alex draws.
My opinion is to stay away from divergence and look for other techniques.
Trading Time in Programming
The major automated trading platforms such as MetaTrader 4, NinjaTrader and TradeStation all count time in the same way. This makes it quite convenient for ordering trading strategies and expert advisors; you don’t have to do any mental gymnastics to describe the strategy in different platforms. The consistent arrangement of time makes it easy for us to translate trading strategies across multiple platforms.
We tend to think of time as moving in the same direction as when we read. English speakers, who read from left to right, think of time as moving the same direction. If you speak a language like Arabic that reads right to left, you tend to think of time as marching to the left.
All of these charting platforms are written by speakers of left to right languages. The past is anything that’s not on the far right side. The present is the square on the far right. Each square represents an equal time interval. Traders know these as bars.
What tends to confuse everyone ordering expert advisors is that even though time marches to the right, trading programmers count the bars to the left. What makes things more confusing is that programmers always start counting from 0 instead of 1.
If you want to trade a moving average cross strategy that waits for the bars to close, what you’re looking at is “bar 2″ and “bar 1″. The way I describe this in the scope of work is the value at two closed bars ago and the value at the last closed bar, respectively.
An expert advisor that uses closed bars ignores bar 0 because it is still open, which causes the moving average values to fluctuate. The only way to know for certain a moving average value at a particular bar is to wait for the bar to close, which means it is no longer bar 0. When a client requests an expert advisor that trades intrabar, they intend to compare the moving averages at “bar 1″ with “bar 0″. In plain language, that means to compare the value at the last closed bar with the currently open bar.
Hopefully, this description makes sense when you open a chart and see the bars already loaded. The final confusing element is when a new bar pops onto the screen. The previous examples showed 5 bars on the chart (bars 0-4). When a sixth bar pops up, the count is reset to the new time period on every update.
Say that we’re looking at an H1 chart in MetaTrader and that the current time is 06:00. When the new hour strikes, the chart loads a new candle to represent 07:00. It’s at this time that the count resets.
Reverse engineer expert advisor
I guarantee that you are not the first trader to consider the idea of reverse engineering an expert advisor. The idea pops up most frequently among traders that don’t want to pay for a commercial expert advisor. Alternatively, they want to use the same strategy when another trader that doesn’t want to share the idea. The motivations for reverse engineering remind me of decompiling EAs, making me leery of the idea.
Reverse engineering a strategy only stands a chance when some parameters are known about the strategy. If, for example, you knew that the strategy involves MACD and moving average crossovers, it at least provides a reasonable starting point. The programmer could write software which combines every possible combination of two moving averages of various types with every known type of MACD. The programmer could then make guess about which types of signals might result in a buy or sell decision. If the guesses are not very good, then the outcome of the reverse engineering attempt is certain failure.
Then you have the problem of guessing on which chart to base the decisions. Many traders use standard charts like the M1 and M15, but many others use less common options like an M3. If the trader uses multiple charts like an M2 and M10, the resulting trade history would clutter together. Good luck trying to pry apart the different series.
Making things worse is if the trader uses a chart that doesn’t depend on time at all. Tick charts are the most common, although you occasionally see less orthodox options like Point and Figure charts and Kagi charts. Time is irrelevant. You wouldn’t have any idea on which charts to run the test.
This approach of making somewhat intelligent guesses while throwing mud at the wall is called a brute force attack. You literally designate every unique possible combination, then see which one opens the metaphorical safe. Some results will bear no resemblance at all to the actual results. If you get extraordinarily lucky and/or have fantastic intelligence, then you might find a close replica of the strategy.
It would be possible to study the correlation of the tested values with the values in the supplied account statement. You would ideally want to find data clusters with similar variable settings that do not dramatically alter the correlation between the reverse engineered strategy and the actual account statement.
If you don’t know very much about the strategy, or if what you think you know turns out to be wrong, then you stand no chance at all of reverse engineering the expert advisor. For all that you know, the EA that you thought used RSI might turn out to run on phases of the moon (yes, there are real strategies that do that) or that make decisions based on coin flips.
Most people assume that they know more than they really do. I would discourage all but the most fool hearty or stubborn from making the attempt, unless you had a very good reason for doing so.
Scripts in MetaTrader
Scripts are executable files in MetaTrader that only run one time. They are perfect for tasks that are routine but time consuming or unpleasant to do. The most advantageous use of scripts is that they do not rely on incoming price ticks in order to run. The script executes the moment that the trader drops it onto a chart.
An expert advisor runs continuously, but it relies on incoming ticks to know that it should update itself. The frequency that the market changes price varies with the time of day. This means that the period between updates for an expert advisor is highly unpredictable. The predictability of a script’s timing – it runs immediately – makes it more suitable for some trading tasks than an EA.
Script examples
A scalper wants to open a trade quickly. He routinely applies a 20 pip stop loss and a 3 pip take profit. His usual process would involve:
- Click inside of MetaTrader to open a trade
- Select the correct forex pair
- Wait for the trade to open
- Frantically add the take profit as quickly as possible
Alternatively, the trader could keep his chart open and the scripts window nearby. Whenever he decides to trade, he drags the script onto the chart. The steps above still occur. The critical difference is that they happen in a fraction of the time. The script runs once, then it removes itself from the chart.
Some tasks require a tiresome amount of clicking rather than speed. Scripts are also useful in that scenario. A lot of MetaTrader users like to stack multiple pending orders above and below the market in a grid pattern. An example would involve placing 10 orders above and below the market at different prices. Doing this manually would take a few minutes.
The alternative is to run a script that does it one time for you. Scripts can display input screens just like expert advisors. That way the user can control the settings.
When the trader is ready to bracket his orders, he drags and drops the script onto the chart. The orders show up on the screen at the requested prices. The total time takes a few seconds instead of several minutes.
Other script uses
The most common, though unorthodox, use of a script is to feed prices into the History Center for a custom offline chart. The script is programmed to run at a set interval such as every half second. The script samples the price, then records the information where the historical prices are kept. The offline chart then re-reads the information and updates the price.
MQL programmers elect to use a script instead of an expert advisor because they are setting an infinite loop. Although scripts technically run once, this script is never allowed to finish its first run. It keeps waiting every set interval to update the price. EAs would not work well here because new, incoming ticks would create a backlog of occasions where the EA is supposed to have run. I would expect MT4′s memory use to eventually get out of control and crash the program if someone elected to take this approach. Even though it is not an ideal solution, scripts update historical prices successfully without the tick backlog concern.
MetaTrader Logs
Log files are written records of all trading activity in MetaTrader. Whenever a trader submits an order, modifies a stop loss or connects to the broker, MetaTrader notes everything that happens along with the time.
We use log files to reconstruct a sequence of events when expert advisors run on a live account. Knowing the order in which things happened helps us to determine why an EA may not work properly.
Consider the logical steps where you want to go to the grocery store. If I were writing software to do this, my log file might read something like this:
1) Find my car keys
2) Find my wallet
3) Get in the car
4) Drive to the grocery store
5) Buy groceries
If my log file only says “find my wallet” and “get in the car”, I intuitively know that something is wrong. Why would the software not work when I get in my car? Because I don’t have my keys.
The log file helps your MQL programmer think along the same lines. When the log says, “calculate the entry rules” and the log only talks about the exits, it’s clear that the flow of the program doesn’t match the design intention.
Log files only come from the computer where the expert advisor runs. When a problem inevitably arises, your programmer will request that you send the log files from the computer where you’re running the EA. This is unfortunately a necessary part of the debugging process.
We do all of our quality assurance testing in the MetaTrader backtester. Although this usually catches the most obvious bugs, new errors will always pop up while forward testing the EA. The log file is what helps connect us to the problem, even though it happened on another computer.
Find your MetaTrader 4 log file
MetaTrader keeps two sets of log files. The most basic logs are located in YOUR BROKER NAME\logs. Most of our clients navigate here accidentally and assume it’s the file that we need. Expert advisors cannot write to this log file, so it unfortunately doesn’t do us much good.
Locating the correct log file for your programmer will vary based on your operating system. Anything in capital letters changes based on your personal information.
Windows XP and Windows Server 2003 users can find the logs in C:\Program Files\YOUR BROKER NAME\experts\logs
Expert advisors that run on Windows Vista or 7 have to put in more effort. Those log files are found in
C:\Users\YOUR USER NAME\appdata\local\virtualstore\Program Files (x86)\YOUR METATRADER INSTALLATION\experts\logs.
The part that confuses most Windows 7 users is that when they navigate to the folder with their USER NAME, the appdata does not appear as an option. The easiest trick is to double click on the current folder name at the top of the screen. Once you’ve clicked, the entire directory name will appear. If you type “appdata” after the final backslash and push enter, the window will navigate to the correct location. You can then continue clicking until the log file appears.
The name of the file corresponds to the date. The format is year, month and date (YYYYMMDD). Today is January 23, 2012, so the log file for today is named 20120123.log.
Finally, log files are often enormous – 50 MB or more. Please right click on the file and select “Compress” or “Send to, Zip File”. Sending the zip file can reduce the file size by 80% or more.











MQL Debugging
MQL is a very simple scripting language. Debugging MQL, unfortunately, is not easy at all. The MetaEditor compiler that MetaQuotes supplies simply does not contain the sophisticated tools that most programmers are accustomed to using.
MQL debugging problems
Visual Studio and other sophisticated IDEs (integrated development environments) contain a number of features that make it easy to debug code while the programmer writes it. The biggest example of this are break points. A break point is a point in the code where the compiler tells the computer to cease running the program when it arrives at a certain line of code.
Consider the example where a trailing stop sets the new stop incorrectly. The natural instinct for most programmers would be to run the expert advisor on the visual backtester, then insert break points on the lines of code shortly after the trailing stop calculations. Break points halt the code, allowing the programmer to peer inside the brains of the software to see what it thought at the time it made a decision. The key advantage in Visual Studio is that the values of all of the variables are clearly visible. It is possible to walk through the program step by step. Whenever one of the steps does not follow the desired rules, the required change is usually obvious. MetaQuotes thankfully included break points in MQL5. They are not available in MQL4.
The lack of full intellisense support affects my programming speed more than anything. Intellisense detects the use of reserved words like OrderSelect() or ObjectGet(). The MetaEditor includes a rudimentary intellisense, but it lacks the fine details that make it so convenient in Visual Studio.
I am used to programming in C# where I can type the first few letters of a variable or class, then the IDE fills out the rest. When I type “Mes” in C# and push the space bar, I know that the MessageBox option will appear (assuming that I declared the appropriate namespace). The MetaEditor provides a list of candidates for reserved words. The programmer must then either select the option with the mouse or press enter.
I know it seems trivial to require pressing enter instead of the space bar, but think about how many times code resuses the same reserved words or variables. The extra key presses really do add up to a lot of unnecessary typing motions. That’s doubly true for a thirty year old that already wears a wrist brace for carpal tunnel pain.
The MetaEditor’s biggest weakness is that it does not detect variable names. We often write expert advisors that contain several thousand lines of code. Tracking the names of tens of variables poses its own challenges. When the coder types in the same set of variable names repeatedly, it would be nice to simply type the first three letters and move on. Copy and paste might provide a decent alternative. The problem is that variables usually group together. You cannot keep 5 different copy and paste items readily available.
The MetaEditor allows functions to return invalid types. Functions declared as double can return strings, integers or nothing at all. The MQL4 compiler does not track whether or not these are valid. It leaves it up to the programmer to discover the invalid type during real time testing. This oversight is a nightmare for the unwitting programmer that mistakenly returns the wrong type.
This is doubly true when a double function is mistakenly returned to an integer variable. MQL4 does not prevent illegal double to int casts. Even worse, the expert advisor continues running with a 0 value for the interger instead of throwing an exception or error message. I cannot count how many hours that I’ve wasted tracking down variables that look perfect, only to realize that I declared the wrong data type. This usually happens when I’m on autopilot, pounding out code. What seems efficient at the time usually costs several hours of hair pulling frustration.
MQL debugging techniques
The MQL programmers on staff here usually resort to any of the following techniques. You may find that using them in combinations helps improve the debugging process even more.
Debug compiler error
This one can be the most frustrating. The MetaEditor attempts to hint at which line of code causes the compiling error. I say attempts because it gets it wrong more often than it gets it right. There’s nothing more irritating than looking at a perfectly legitimate line of code that the compiler flags as problematic.
I almost always resort to commenting out increasingly large sections of code until the error disappears. I start with commenting out a single line of code. If that doesn’t work, then I comment out ten lines. If that doesn’t work, I might comment out entire functions or sections of code. When the compiler finally runs properly, you know that the last section of commented out code contains the bug.
Next, you backtrack. Start making the offending commented-out section smaller and smaller until the error reappears. Now, you have finally zeroed in on the actual source of the problem.
Debug in real time or on the backtester
My preferred method of debugging is to comment most of the relevant decision information onto the screen, which is done using the Comment() function. I then run the visual backtester, watching how the data behaves in relation to the visual information.
On screen comments are essentially jury-rigged break points. Controlling how and when they appear allows the programmer to step through the code to uncover the issue. The only difference is that comments do not forcefully prevent the code from running. The text which appears is very small. Aside from that, I really like that fact that it’s so robust. The comment function always works without a hitch, making it the best friend of a programmer that’s debugging code.
Taking screenshots takes this to the next level. Whenever customers ask questions about why an expert advisor behaved a certain way, the easiest answers come with screenshots. Commenting the imitation break points usually provide bullet proof answers – the programmer and customer can literally see what the EA thought at the time it made a decision. MQL4 offers the WindowScreenShot() function to do this.
The EAs that we program always take screenshots during critical actions, such as entering a trade or adjusting an exit condition. The goal is to provide a visual record of every decision with an eye to answering future questions about the behavior.
Our default template includes a true/false variable called WriteScreenshots. Traders control whether they want to bother with this debugging feature or not. The only downside to it is that every recorded action eats up about 50kb of hard drive space.
Log files represent the final debugging option. The major drawback is that they are so ugly and difficult to read. I almost always prefer screenshots for this reason.
Nonetheless, log files do have their place. The main way to use them is as error catchers. Whenever a process goes awry due to a problem with either MetaTrader itself or with a broker transaction, the log file is the easiest place to catch it.
Debugging MQL files is a skill that takes awhile to learn. The tools at the programmer’s disposal are much different from those available to higher level languages. Once you get used to working with the much simpler tools in the MetaEditor and MetaTrader, the bug clearing process usually gets much easier.