MetaEditor HelpCreating ProgramsDebugging 

Debugging

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:

Presetting

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.

Breakpoints

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 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.

Breakpoint

Start of Debugging

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 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 Triggered Breakpoint. 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.

Watching Expressions

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 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 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".

Viewing stack and expressions

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[3][4], 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":

Inserting object members

Viewing Stack of Calls

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;
  • Function name;
  • 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-By-Step Debugging

For the step-by-step debugging with the viewing of calls stack, commands of the "Debug" menu or of the "Standard" toolbar are used:

  • Step Into 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 Step Over — go one step of the program execution, not entering called functions. The same action can be performed by pressing "F10".
  • Step Out 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 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.

Breaking, Continuing, Stopping Debugging

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:

  • Breaking
    The execution of program while debugging it can be broken manually using the "Break Debugging 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.
  • Continuing
    In order to continue the running of program in the debugging mode after it is broken automatically or manually, execute the "Continue Debugging Continue Debugging" command in the "Debug" menu or in the "Standard" toolbar of the same name, or press "F5".
  • Stopping
    In order to stop debugging, it's necessary to execute the "Stop Debugging 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.

 

MetaTrader 5 — a complex trading platform for providing brokerage services on the retail Forex market.

Copyright 2000-2014, MetaQuotes Software Corp.