- Functions for getting the basic properties of the current chart
- Chart identification
- Getting the list of charts
- Getting the symbol and timeframe of an arbitrary chart
- Overview of functions for working with the complete set of properties
- Descriptive chart properties
- Checking the status of the main window
- Getting the number and visibility of windows/subwindows
- Chart display modes
- Managing the visibility of chart elements
- Horizontal shifts
- Horizontal scale (by time)
- Vertical scale (by price and indicator readings)
- Colors
- Mouse and keyboard control
- Undocking chart window
- Getting MQL program drop coordinates on a chart
- Translation of screen coordinates to time/price and vice versa
- Scrolling charts along the time axis
- Chart redraw request
- Switching symbol and timeframe
- Managing indicators on the chart
- Opening and closing charts
- Working with tpl chart templates
- Saving a chart image
Scrolling charts along the time axis
MetaTrader 5 users are familiar with the quick chart navigation panel, which opens by double-clicking in the left corner of the timeline or by pressing the Space or Input keys. A similar possibility is also available programmatically by using the ChartNavigate function.
bool ChartNavigate(long chartId, ENUM_CHART_POSITION position, int shift = 0)
The function shifts the chartId chart by the specified number of bars relative to the predefined chart position specified by the position parameter. It is of ENUM_CHART_POSITION enumeration type with the following elements.
Identifier |
Description |
---|---|
CHART_BEGIN |
Chart beginning (oldest prices) |
CHART_CURRENT_POS |
Current position |
CHART_END |
Chart end (latest prices) |
The shift parameter sets the number of bars by which the chart should be shifted. A positive value shifts the chart to the right (towards the end), and a negative value shifts the chart to the left (towards the beginning).
The function returns true if successful or false as a result of an error.
To test the function, let's create a simple script ChartNavigate.mq5. With the help of input variables, the user can choose a starting point and a shift in bars.
#property script_show_inputs
|
The log displays the number of the first visible bar before and after the move.
A more practical example would be the script ChartSynchro.mq5, which allows you to synchronously scroll through all charts it is running on, in response to the user manually scrolling through one of the charts. Thus, you can synchronize windows of different timeframes of the same instrument or analyze parallel price movements on different instruments.
void OnStart()
|
Alignment is performed by the date and time of the first visible bar (CHART_FIRST_VISIBLE_BAR). The script in a loop checks this value and, if it works on an active chart, writes it to a global variable. Scripts on other charts read this variable and adjust their position accordingly with ChartNavigate. The parameters specify the relative movement of the chart (CHART_CURRENT_POS), and the number of bars to move is defined as the difference between the current number of the first visible bar and the one read from the global variable.
The following image shows the result of synchronizing the H1 and M15 charts for EURUSD.
An example of the script for synchronizing chart positions
After we get familiar with the system events on charts, we will convert this script into an indicator and get rid of the infinite loop.