It's hard to eliminate errors when writing considerably complicated programs. MetaEditor offers you the built-in debugger to help you solve this problem. The debugging of applications consists in the possibility of the step-by-step program execution, calculating local variables, setting at breakpoints in a preset location.
In order to start debugging, the window of the main file of the project must be active (*.MQ4 or *.MQ5). In order to debug include files (*.MQH), it's necessary to start the debugging of the file, into which they are included.
Further the debugging process is described in details:
Before you start the program debugging, it's necessary to check its parameters in the "Debugging" tab of the MetaEditor options window. In this window you can specify a symbol and a chart period, in which the debugging of applications will be performed. Such a chart will be temporarily created each time when debugging is started, and it will be deleted after the debugging completion.
The "debug.tpl" template located in the /profiles/templates folder of the client terminal is applied to a chart created for debugging.
A breakpoint is a command that triggers when the program execution is passed to an indicated line and stops the program in this location. Breakpoints allow analyzing the program behavior in a selected location: viewing values of variables, stack of functions. After that the debugging process can be continued or completed.
Before you start debugging, place such breakpoints in necessary code positions. To do this, click twice by the left mouse-button on the gray field near the left border of the code line. You can also place the mouse cursor in the necessary line and execute the " Toggle Breakpoint" command of the "Debug" menu or in the "Standard" toolbar, or you can press the "F9" key. Breakpoints are disabled the same way.
To start the debugging process, it's necessary to open a program file, selecting it in the "Navigator" window. After that execute the " Start Debugging" command in the "Debug" menu or in the "Standard" toolbar, or press the "F5" key. After that the program to debug will be attached to a special chart in the trading terminal. As soon as the program execution reaches the line with the first breakpoint, it will be stopped. The point where the execution is stopped will be changed into . Besides, the "Debugging" tab will appear in the "Toolbox" window. In its left part the stack of function call will be shown.
The debugging of applications is performed in real conditions using the price data received from server. A special temporary chart that is created for debugging doesn't have any differences from the ordinary charts opened in the client terminal.
In the process of debugging you can watch values of different expressions (variables) obtained at this program execution stage. For these purposes the right part of the "Debug" tab in the "Toolbox" window is used. An expression can be added in the following ways:
•During debugging, call a context menu on a necessary expression and select the " Add Watch" command or press the "Shift+F9" key combination;
•In the right part of the "Debug" tab in the "Toolbox" window call the context menu and execute the " Add" command or press the "Insert" key. After that a new line will appear, and in its "Expression" field your should enter the name of a watched parameter.
•In order to change the name of a watched expression, it's necessary to click twice by the left mouse button on its name or select it and execute the "Edit" command of the context menu or press "F2".
In the expressions watching window, you can conduct simple mathematical calculations (addition, subtraction, multiplication and division), as well as view values in certain array points. For example, you can indicate a record like A, where A is the name of an array, 3 and 4 are positions in its dimensions. When adding objects to observed expressions, the list of its members can be shown by adding a point at their end or pressing "Ctrl+Space":
By default, integer numbers are displayed in the watch window in the decimal format. To display them in the binary or hexadecimal format, specify a comma separated modifier b or x respectively in the Expression field:
The left part of the "Debug" tab in the "Toolbox" window allows viewing stacks. Here the following components are shown:
•Name of a file, from which a function is called;
•Number of a line in the file, where this function is called.
When the debugging process is started, only the address of a called function, at which a breakpoint has triggered, is shown in this window.
• Step Into — go one step of the program execution, entering called functions. The same action can be performed by pressing the "F11" key.
• Step Over — go one step of the program execution, not entering called functions. The same action can be performed by pressing "F10".
• Step Out — go to the execution of a program step one level higher. The same action can be performed pressing "Shift+F11".
For executing the above commands, the debugging process must be stopped. This is done automatically when program execution reaches a breakpoint. Also debugging can be suspended manually by pressing " Break Debugging" in the "Debug" menu or in the "Standard" toolbar, or by pressing the "Break" key.
When an application is at a break point, occurring of events continues. At that all of them are placed into a queue, and their handling by Expert Advisor will be continued after exiting the code of the current event handle.
The breaking of the program debugging process is done automatically, when it reaches a line with a breakpoint. However, the debugging process can be managed manually:
The execution of program while debugging it can be broken manually using the " Break Debugging" command in the "Debug" menu or in the "Standard" toolbar, or pressing the "Break" key. After that you can executed the step-by-step debugging.
In order to continue the running of program in the debugging mode after it is broken automatically or manually, execute the " Continue Debugging" command in the "Debug" menu or in the "Standard" toolbar of the same name, or press "F5".
In order to stop debugging, it's necessary to execute the " Stop Debugging" command in the "Debug menu" or in the "Standard" toolbar, or press "Shift+F5". After the program is stopped, it is removed from the special chart, to which it was attached for debugging, and the chart is closed.
Debugging is impossible without the source MQ4, MQ5 and MQH files. It is also recommended not to distribute EX4/EX5 files obtained in the result of debugging.
Trading robots and technical indicators can be debugged not only on real time updated price data, but also using history prices. A program operation can be tested on a required history interval. Debugging runs in the Strategy Tester in the visual testing mode. A chart for debugging is based on a sequence of ticks emulated in the tester.
To start debugging on history data, configure testing parameters in the MetaEditor settings.
Set breakpoints in the code program and start testing on history data.
This will initiate visual testing and the debugging process.