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:
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:
When the debugging process is started, only the address of a called function, at which a breakpoint has triggered, is shown in this window.
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:
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.