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 45 we discussed a few questions about back testing with the Strategy Tester. This time we're going to talk about issues in debugging EAs in MT4.
As I've mentioned in the past, the MQL4 development environment is quite a bit behind the times. MQL4 doesn't even include simple software development technologies like inline debuggers that have been included in development environments since the 80s. I'm not sure why, except that maybe the MQL4 environment was an afterthought for MT4. MetaQuotes has recently included many new features in MT4, so hopefully there are more features forthcoming for MQL4.
Consequently, we have to use "workarounds" (I hate workarounds ;)) to do our debugging. Our main tool is the Print() function which prints a line in the Experts log. You can string literals (quoted text) and variables to your heart's content with the Print() function. Just separate them with commas. And remember to use spaces so you can actually interpret the results.
I put a number of "standard" lines in my code - starting with a line that identifies the beginning of the EA's execution. That will help you find the beginning of the last execution of the EA in the Experts log if you don't clear the log before starting the execution. I also have a line that identifies the beginning of the included functions. For example - at the beginning of the init() function:
Print("-----",Title," ",Version," Initializing ",Symbol(),"-----");
Where the variable Title contains the program title and the variable Version contains the program version. I include the hyphens to set the line out from other lines in the Experts log.
One thing you should always keep in mind is that the start() function executes once for EACH tick of the market. There could be many ticks per second at certain times of the day. If you have a Print() statement that executes at every market tick, your Experts log file could grow to thousands of lines - which could make it totally useless. So when you add Print() statements, be sure you know how often it will execute.
Another thing I like to do is to put the Print() statement in a conditional (if()) statement using an external boolean variable - debug. For example:
extern bool debug=false;
if(debug) Print("Function Name: a=",a,", b=",b);
Then you can turn the debug statements on and off whenever you execute the EA.
The Print() function, used judiciously, can provide you with quite a bit of information about your variables as the program runs.
That's it for today. Next time we'll talk a little bit about Global variables; what they are and how we can use them.
As always, thanks for your attention and please follow me on Twitter and LinkedIn.
Please leave a comment below if you have any questions!
Also, please give this strategy a 5 star if you enjoyed it!