Interactive events on charts
MetaTrader 5 charts not only provide a visual representation of data and are the execution environment for MQL programs, but also support the mechanism of interactive events, which allows programs to respond to the actions of the user and other programs. This is done by a special event type OnChartEvent which we already discussed in the Overview of event handling functions.
Any indicator or Expert Advisor can receive such events provided that the event processing function of the same name with a predefined signature is described in the code. In some of the indicator examples that we considered earlier, we have already had to take advantage of this opportunity. In this chapter, we will look at the event system in detail.
The OnChartEvent event is generated by the client terminal during the following chart manipulations performed by the user:
- Changing the chart size or settings
- Keystrokes when the chart window is in focus
- Mouse cursor movement
- Mouse clicks on the chart
- Mouse clicks on graphical objects
- Creating a graphical object
- Deleting a graphical object
- Moving a graphical object with the mouse
- Finishing to edit the test in the input field of the OBJ_EDIT object
The MQL program receives the listed events only from the chart on which it is running. Like other event types, they are added to a queue. All events are then processed one by one in the order of arrival. If there is already an OnChartEvent event of a particular type in the MQL program queue or it is being processed, a new event of the same type is not queued (discarded).
Some event types are always active, while others are disabled by default and must be explicitly enabled by setting the appropriate chart properties using the ChartSetInteger call. Such disabled events include, in particular, mouse movements and mouse wheel scrolling. All of them are characterized by the fact that they can generate massive event streams, and in order to save resources, it is recommended to enable them only when necessary.
In addition to standard events, there is the concept of "custom events". The meaning and content of parameters for such events are assigned and interpreted by the MQL program itself (one or several, if we are talking about the interaction of a complex of programs). An MQL program can send "user events" to a chart (including another one) using the function EventChartCustom. Such events are also handled by the OnChartEvent function.
If there are several MQL programs on the chart with the OnChartEvent handler, they will all receive the same stream of events.
All MQL programs run in threads other than the main thread of the application. The main terminal thread is responsible for processing all Windows system messages, and as a result of this processing, in turn, it generates Windows messages for its own application. For example, dragging a chart with the mouse generates several WM_MOUSE_MOVE system messages (in terms of the Windows API) for subsequent drawing of the application window, and also sends internal messages to Expert Advisors and indicators launched on this chart. In this case, a situation may arise that the main thread of the application has not yet managed to process the system message about redrawing the WM_PAINT window (and therefore has not yet changed the appearance of the chart), and the Expert Advisor or indicator has already received an event about moving the mouse cursor. Then the chart property CHART_FIRST_VISIBLE_BAR will be changed only after the chart is drawn.
Since of the two types of interactive MQL programs, we have studied only indicators so far, all the examples in this chapter will be built on the basis of indicators. The second type, Expert Advisors, will be described in the next Part of the book. However, the principles of working with events in them completely coincide with those presented here.