DRAW_ZIGZAG

The DRAW_ZIGZAG style draws segments of a specified color based on the values of two indicator buffers. This style is very similar to DRAW_SECTION, but unlike the latter, it allows drawing vertical segments within one bar, if values ​​of both indicator buffers are set for this bar. The segments are plotted from a value in the first buffer to a value in the second indicator buffer. None of the buffers can contain only empty values, since in this case nothing is plotted.

The width, color and style of the line can be specified like for the DRAW_SECTION style - using compiler directives or dynamically using the PlotIndexSetInteger() function. Dynamic changes of the plotting properties allows "to enliven" indicators, so that their appearance changes depending on the current situation.

Sections are drawn from a non-empty value of one buffer to a non-empty value of another indicator buffer. To specify what value should be considered as "empty", set this value in the PLOT_EMPTY_VALUE property:

//--- The 0 (empty) value will mot participate in drawingPlotIndexSetDouble(index_of_plot_DRAW_ZIGZAG,PLOT_EMPTY_VALUE,0);

Always explicitly fill in the values of the indicator buffers, set an empty value in a buffer to skip bars.

The number of buffers required for plotting DRAW_ZIGZAG is 2.

An example of the indicator that plots a saw based on the High and Low prices. The color, width and style of the zigzag lines change randomly every N ticks.

Note that initially for plot1 with DRAW_ZIGZAG the properties are set using the compiler directive #property, and then in the OnCalculate() function these properties are set randomly. The N parameter is set in external parameters of the indicator for the possibility of manual configuration (the Parameters tab in the indicator's Properties window).

//+------------------------------------------------------------------+//| DRAW_ZIGZAG.mq5 |//| Copyright 2011, MetaQuotes Software Corp. |//| https://www.mql5.com |//+------------------------------------------------------------------+#propertycopyright"Copyright 2011, MetaQuotes Software Corp."#propertylink"https://www.mql5.com"#propertyversion"1.00"#propertydescription"An indicator to demonstrate DRAW_ZIGZAG"#propertydescription"It draws a \"saw\" as straight segments, skipping the bars of one day"#propertydescription"The day to skip is selected randomly during indicator start"#propertydescription"The color, width and style of segments are changed randomly"#propertydescription" every N ticks"#propertyindicator_separate_window#propertyindicator_buffers 2#propertyindicator_plots 1//--- plot ZigZag#propertyindicator_label1"ZigZag"#propertyindicator_type1DRAW_ZIGZAG#propertyindicator_color1 clrBlue#propertyindicator_style1STYLE_SOLID#propertyindicator_width1 1//--- input parametersinputintN=5; // Number of ticks to change //--- indicator buffersdouble ZigZagBuffer1[];double ZigZagBuffer2[];//--- The day of the week for which the indicator is not plottedint invisible_day;//--- An array to store colorscolor colors[]={clrRed,clrBlue,clrGreen};//--- An array to store the line stylesENUM_LINE_STYLE styles[]={STYLE_SOLID,STYLE_DASH,STYLE_DOT,STYLE_DASHDOT,STYLE_DASHDOTDOT};//+------------------------------------------------------------------+//| Custom indicator initialization function |//+------------------------------------------------------------------+intOnInit() {//--- Binding arrays and indicator buffersSetIndexBuffer(0,ZigZagBuffer1,INDICATOR_DATA);SetIndexBuffer(1,ZigZagBuffer2,INDICATOR_DATA);//--- Get a random value from 0 to 6, for this day the indicator is not plotted invisible_day=MathRand()%6;//--- The 0 (empty) value will mot participate in drawingPlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);//--- The 0 (empty) value will mot participate in drawingPlotIndexSetString(0,PLOT_LABEL,"ZigZag1;ZigZag2");//---return(INIT_SUCCEEDED); }//+------------------------------------------------------------------+//| Custom indicator iteration function |//+------------------------------------------------------------------+intOnCalculate(constint rates_total,constint prev_calculated,constdatetime &time[],constdouble &open[],constdouble &high[],constdouble &low[],constdouble &close[],constlong &tick_volume[],constlong &volume[],constint &spread[]) {staticint ticks=0;//--- Calculate ticks to change the style, color and width of the line ticks++;//--- If a sufficient number of ticks has been accumulatedif(ticks>=N) {//--- Change the line properties ChangeLineAppearance();//--- Reset the counter of ticks to zero ticks=0; }//--- The structure of time is required to get the day of week of each barMqlDateTime dt;//--- The start position of calculationsint start=0;//--- If the indicator was calculated on the previous tick, then start the calculation with the last but one tickif(prev_calculated!=0) start=prev_calculated-1;//--- Calculation loopfor(int i=start;i<rates_total;i++) {//--- Write the bar open time in the structureTimeToStruct(time[i],dt);//--- If the day of the week of this bar is equal to invisible_dayif(dt.day_of_week==invisible_day) {//--- Write empty values to buffers for this bar ZigZagBuffer1[i]=0; ZigZagBuffer2[i]=0; }//--- If the day of the week is ok, fill in the bufferselse {//--- If the bar number if evenif(i%2==0) {//--- Write High in the 1st buffer and Low in the 2nd one ZigZagBuffer1[i]=high[i]; ZigZagBuffer2[i]=low[i]; }//--- The bar number is oddelse {//--- Fill in the bar in a reverse order ZigZagBuffer1[i]=low[i]; ZigZagBuffer2[i]=high[i]; } } }//--- return value of prev_calculated for next callreturn(rates_total); }//+------------------------------------------------------------------+//| Changes the appearance of the zigzag segments |//+------------------------------------------------------------------+void ChangeLineAppearance() {//--- A string for the formation of information about the ZigZag propertiesstring comm="";//--- A block for changing the color of the ZigZagint number=MathRand(); // Get a random number//--- The divisor is equal to the size of the colors[] arrayint size=ArraySize(colors);//--- Get the index to select a new color as the remainder of integer divisionint color_index=number%size;//--- Set the color as the PLOT_LINE_COLOR propertyPlotIndexSetInteger(0,PLOT_LINE_COLOR,colors[color_index]);//--- Write the line color comm=comm+"\r\n"+(string)colors[color_index];//--- A block for changing the width of the line number=MathRand();//--- Get the width of the remainder of integer divisionint width=number%5; // The width is set from 0 to 4//--- Set the color as the PLOT_LINE_WIDTH propertyPlotIndexSetInteger(0,PLOT_LINE_WIDTH,width);//--- Write the line width comm=comm+"\r\nWidth="+IntegerToString(width);//--- A block for changing the style of the line number=MathRand();//--- The divisor is equal to the size of the styles array size=ArraySize(styles);//--- Get the index to select a new style as the remainder of integer divisionint style_index=number%size;//--- Set the color as the PLOT_LINE_COLOR propertyPlotIndexSetInteger(0,PLOT_LINE_STYLE,styles[style_index]);//--- Write the line style comm="\r\n"+EnumToString(styles[style_index])+""+comm;//--- Add information about the day that is omitted in calculations comm="\r\nNot plotted day - "+EnumToString((ENUM_DAY_OF_WEEK)invisible_day)+comm;//--- Show the information on the chart using a commentComment(comm); }