Graphical objects

MetaTrader 5 users are well aware of the concept of graphical objects: trend lines, price labels, channels, Fibonacci levels, geometric shapes, and many other visual elements that are used for the analytical chart markup. The MQL5 language allows you to create, edit, and delete graphical objects programmatically. This can be useful, for example, when it is desirable to display certain data simultaneously in a subwindow and the main window of an indicator. Since the platform only supports the output of indicator buffers in one window, we can generate objects in the other window. With the markup created from graphical objects, it is easy to organize semi-automated trading using Expert Advisors. Additionally, objects are often used to build custom graphical interfaces for MQL programs, such as buttons, input fields, and flags. These programs can be controlled without opening the properties dialog, and the panels created in MQL can offer much greater flexibility than standard input variables.  

Each object exists in the context of a particular chart. That's why the functions we will discuss in this chapter share a common characteristic: the first parameter specifies the chart ID. In addition, each graphical object is characterized by a name that is unique within one chart, including all subwindows. Changing the name of a graphical object involves deleting the object with the old name and creating the same object with a new name. You cannot create two objects with the same name.

The functions that define the properties of graphical objects, as well as the operations of creating (ObjectCreate) and moving (ObjectMove) objects on the chart, essentially serve to send asynchronous commands to the chart. If these functions are successfully executed, the command enters the shared event queue of the chart. The visual modification of the properties of graphical objects occurs during the processing of the event queue for that particular chart. Therefore, the external representation of the chart may reflect the changed state of objects with some delay after the function calls.

In general, the update of graphical objects on the chart is done automatically by the terminal in response to chart-related events such as receiving a new quote, resizing the window, and so on. To force the update of graphical objects, you can use the function for requesting chart redraw (ChartRedraw). This is particularly important after mass creation or modification of objects.

Objects serve as a source of programmatic events, such as creation, deletion, modification of their properties, and mouse clicks. All aspects of event occurrence and handling are discussed in a separate chapter, along with events in the general window context.

We will begin with the theoretical foundations and gradually move on to practical aspects.