- Main characteristics of indicators
- Main indicator event: OnCalculate
- Two types of indicators: for main window and subwindow
- Setting the number of buffers and graphic plots
- Assigning an array as a buffer: SetIndexBuffer
- Plot settings: PlotIndexSetInteger
- Buffer and chart mapping rules
- Applying directives to customize plots
- Setting plot names
- Visualizing data gaps (empty elements)
- Indicators in separate subwindows: sizes and levels
- General properties of indicators: title and value accuracy
- Item-wise chart coloring
- Skip drawing on initial bars
- Waiting for data and managing visibility (DRAW_NONE)
- Multicurrency and multitimeframe indicators
- Tracking bar formation
- Testing indicators
- Limitations and advantages of indicators
- Creating an indicator draft in the MQL Wizard
Applying directives to customize plots
So far, we have been customizing graphic plots using PlotIndexSetInteger function calls. MQL5 allows you to do the same using #property preprocessor directives. The main difference between these two methods is that the directives are processed at compile time and the properties described with them are read from the executable file during loading, even before the handler OnInit is executed (if it exists). That is, the directives provide some default values which may be used as is if you don't need to change them.
On the other hand, the PlotIndexSetInteger function call allows you to change properties on the go, during program execution. Changing properties dynamically using functions allows you to create more flexible scenarios for using the indicator. The directives and the relevant PlotIndexSetInteger function calls are shown in the table below.
Directives |
Function |
Description |
---|---|---|
indicator_colorN |
PlotIndexSetInteger(N-1, PLOT_LINE_COLOR, color) |
Line color for plotting |
indicator_styleN |
PlotIndexSetInteger(N-1, PLOT_LINE_STYLE, type) |
Drawing style from the ENUM_LINE_STYLE enumeration |
indicator_typeN |
PlotIndexSetInteger(N-1, PLOT_DRAW_TYPE, type) |
Drawing type from the ENUM_DRAW_TYPE enumeration |
indicator_widthN |
PlotIndexSetInteger(N-1, PLOT_LINE_WIDTH, width) |
Line thickness in pixels (1 - 5) |
Please note that the numbering of plots in directives starts from 1, while in functions it starts from 0. For example, the directive #property indicator_type1 DRAW_ZIGZAG is equivalent to calling PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_ZIGZAG).
It is also worth noting that by using the function, you can set many more properties than through directives: the ENUM_PLOT_PROPERTY_INTEGER enumeration provides ten elements.
The properties described by the directives are available (visible and can be edited by the user) in the indicator settings dialog even when it is placed on the chart for the first time. In particular, this includes the thickness, color, and style of the lines (tab Colors), the number and placement of levels (tab Levels). The same properties set by functions (and if they do not have default values in directives) appear in the dialog only the second and subsequent times.
Let's adjust the IndHighLowClose.mq5 indicator to use directives. The new version is in the file IndPropHighLowClose.mq5. The use of directives simplifies the OnInit handler; OnCalculate does not change.
#property indicator_chart_window
|
The new indicator looks absolutely the same as the old one.