The Debugger – a Novelty of MT5

February 28, 2010
By Bogdan Baltatu, MQLmagazine editor

[Versiunea romaneasca] [ in romana] [English edition]

One of the upgrades brought by MetaQuotes to the MetaEditor IDE is the introduction of a debugger.

Across this article I’ll use the article ‘error’ as a reference to the conceptual code errors such as wrongly written conditions or wrongly written calculus expressions… Warning: don’t mistake them with the errors signaled by the compiler.

For the ones that don’t know , the purpose of the debugger is to run line by line to code in order to find conceptual programming errors, and to watch the values of the variables realtime.

In MT4, that lacked a debugger, the programmers were using the Print function to find the errors in the code, this being the single method available. This method is difficult to use and the time needed to find the error is way larger compared to using a debugger, because Prin needed a recompilation every time, re-attaching the EA and reading in logs. If we were calling functions giving a wrong result , because they were badly written, we had to go inside that function and attach Print lines to see where were the errors. After repairs, the code had to be cleaned up of redundant Print lines used to find the error.

Right now the debugger works only to test scripts, but we think it will be integrated with the Strategy Tester to work with experts. This is needed also because several buggy EA modules may be run only in rare market conditions, that will not appear while forward testing, but will be revealed while backtesting.

Now the things got simpler, from the same window you can follow the program being run line by line.

In order to use the debugger we have to call the DebugBreak() function where we want the normal execution stopped in order to start execution line by line. If we attach such a script to a chart, this will not stop running, as it will be stopped only if it’s started with the debug mode of the MetaEditor.

To start the script for debugging we have to press Debug->Start or F5 key or it’s dedicated button in the special bar dedicated to the debugger, bar which contains other 3 buttons for passing thru the code.


The group of buttons from the rectangle labeled 1 are the 6 buttons used at debugging. The first 3 buttons are getting in and out from the debug mode, but also to pause. The last 3 buttons are for passing the code in other manners, in the debug mode:

– passing thru the code by Step Into (F11) we go line by line but we enter the functions referred. For instance if we are with the cursor on the line ‘p=prod(a,b)’ and we push Step Into then the cursor will go into the ‘prod’ function and we will be be able to go line by line thru the code of this function;

– passing thru the code by Step Over (F10) we go line by line thru the code but the debugger will jump over called functions (these will be executed, but not line by line in a debug mode).

– Step Out should make the debugger get out of the function where the cursor is, and not close the debug mode, thing which is happening now.

The green arrow labeled with 2 is the current line. If we push F10 (Step Over), the arrow will move to the next line.

Running a code in the Debug mode a new page opens within the Toolbox pageframe, which is divided in two (areas 3 and 4).

In area 3 we see the name of the file that’s being run, the name of the function and the number of the line where we are in the debug mode.

Area 4 is similar to the Watch window from Borland C++ / Borland Pascal or other older IDEs , where can be watched realtime the values and types of variables.

In picture above in area 4 we have 3 variables which we follow : a, s and p. The variable ‘a’ has the value of 5 because we passed the line ‘a=5′ where we are initializing it. The variable ‘p’ has the value 0, because this is the default value taken by the integer variables when they are defined, while ’s’ is undefined, because that’s the stop where the execution is stopped, right before it being declared.

These being said I wish you luck writing codes and debugging them.