MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code. These are the places where the program execution should be paused. Then launch the program on a regular chart or the strategy tester. As soon as the program reaches a breakpoint, it is paused. This allows you to see the values of calculated variables and then continue program execution step by step checking the execution algorithm.
Before debugging, set its general parameters in MetaEditor settings. You can specify a symbol and a period of chart, on which the debugging of applications is to occur. Such a chart is temporarily created every time you start debugging and removed immediately after its completion.
debug.tpl template located in the /profiles/templates folder of the trading platform is applied to a debugging chart.
A breakpoint is a command triggered when a program execution reaches a specified string. The command stops the execution at that string. Breakpoints allow examining a program behavior in a specified code area: view the variable values and the stack of functions. In the future, the debugging process can be resumed or terminated.
Before launching a debugging, set at least one breakpoint in the program code. To do this, double-click on the gray field to the left of the code line. You can also place a cursor on the line and click Toggle Breakpoint in the Debug menu or F9. Breakpoints are disabled the same way.
If you need to jump to the point, at which program execution stopped, double click on the function in the call stack viewing window. Also, using the window context menu, you can open the list of all breakpoints in the currently debugged program:
You can jump to any breakpoint by double-clicking on it.
As soon as the program execution in debugging mode reaches a breakpoint, it is paused. The line the execution is paused on is marked with the icon. The Debug tab also appears in the Toolbox window. Its left part displays the function call stack.
During debugging, the values of various expressions (variables) can be tracked on the current program execution step. The right part of the Debug tab of the Toolbox window displays the appropriate results. To track expression values, add it to the observed ones:
In the expression observation window, you can perform simple mathematical calculations (addition, subtraction, multiplication and division), as well as view values at specific array points, for example, by specifying A entry, where A is an array name, while 3 and 4 are positions in its dimensions. When adding objects to observed expressions, we can view the list of their members by specifying a full stop in the end or clicking Ctrl+Space:
To view the contents of an array or object, double-click on it in the observation window.
By default, integers are displayed in the observation window in decimal form. To display the value in binary or hexadecimal form, specify the comma-separated b or x modifier accordingly in the Expression field.
Data for returning control from sub-programs (nested functions) to the program (main event handler OnInit, OnTick, etc.) is specified in the call stack. The stack allows you to track the entire sequence of an event occurrence in the trading platform up to calling a specific function.
The following data is displayed for each function:
The step-by-step debugging commands can be used only after the program is paused in debug mode:
Events continue occurring in the trading platform even if the program is at a breakpoint. All occurred events are placed in a queue and their processing using an expert is continued after the current event handler exits the code.
Program is stopped automatically during debugging as soon as the execution reaches a string with a breakpoint installed. Debugging can also be managed manually:
Apart from real-time price data, debugging of trading robots and technical indicators can be performed on history as well. Any program can be tested on required history data without waiting for certain trading events in real time.
Debugging on history data runs in the visual testing mode in the Strategy Tester. An application is executed on a chart based on an emulated sequence of ticks in the tester or accumulated history ticks (received from a broker).
To start debugging on history, set testing parameters in MetaEditor settings.
Set the breakpoints in the code, and then start the debugging using historical prices.
This will initiate visual testing and the debugging process.