DRAW_COLOR_ZIGZAG

DRAW_COLOR_ZIGZAG 样式使用两个指标缓冲区的值绘制不同颜色的节段。该样式是 DRAW_ZIGZAG的颜色版,例如,允许为每个节段指定来自预定义颜色的独立颜色。节段从第一个缓冲区的值标图到第二个指标缓冲区的值。缓冲区不会只包含空值,因为在这种情况下,不会标图。

线的宽度,颜色和样式的指定类似 DRAW_ZIGZAG 样式 - 使用编译程序指令 或动态使用PlotIndexSetInteger() 函数。动态改变标图属性允许 "激活" 指标,以便于可以根据当前状况改变其外观。

从一个缓冲区的非空值到另一个指标缓冲区的非空值绘制部分。若要指定哪个值应被视为“空”,请在 PLOT_EMPTY_VALUE 属性设定该值:

//--- 0(空)值将不参与绘制

   PlotIndexSetDouble(index_of_plot_DRAW_COLOR_ZIGZAG,PLOT_EMPTY_VALUE,0);

始终明确填写指标缓冲区的值,在缓冲区设置空值跳过柱形。

标图DRAW_COLOR_ZIGZAG 所需的缓冲区的数量是3:

  • 存储zigzag节段终端值的两个缓冲区;
  • 存储颜色标引的一个缓冲区,它被用于绘制节段(它使得仅设置非空值有意义)。

基于最高价和最低价标图的指标示例。zigzag 线的颜色,宽度和样式每N 个订单号都会随机变化一次。

请注意,DRAW_COLOR_ZIGZAG样式的plot1,使用编译程序指令#property设置8个颜色,然后在OnCalculate() 函数随机选择来自存储在colors[] 数组的14种颜色的颜色。

N 参数设置在指标的 外部参数 ,使手动配置成为可能(指标属性窗口的参数标签)。

//+------------------------------------------------------------------+

//|                                            DRAW_COLOR_ZIGZAG.mq5 |

//|                        Copyright 2011, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2011, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"



#property description "An indicator to demonstrate DRAW_COLOR_ZIGZAG"

#property description "It draws a broken line as a sequence of colored sections, the color depends on the number of the day of the week"

#property description "The color, width and style of segments are changed randomly"

#property description " every N ticks"



#property indicator_chart_window

#property indicator_buffers 3

#property indicator_plots   1

//--- 标图Color_Zigzag

#property indicator_label1  "Color_Zigzag"

#property indicator_type1   DRAW_COLOR_ZIGZAG

//--- 定义8 种用于填充节段的颜色(它们存储在指定数组)

#property indicator_color1  clrRed,clrBlue,clrGreen,clrYellow,clrMagenta,clrCyan,clrLime,clrOrange

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- 输入参数

input int      N=5;              // 改变订单号数量 

int            color_sections;

//--- 节段终端缓冲区的值

double         Color_ZigzagBuffer1[];

double         Color_ZigzagBuffer2[];

//--- 节段终端缓冲区的颜色标引

double         Color_ZigzagColors[];

//--- 存储颜色的数组包含14种元素

color colors[]=

  {

   clrRed,clrBlue,clrGreen,clrChocolate,clrMagenta,clrDodgerBlue,clrGoldenrod,

   clrIndigo,clrLightBlue,clrAliceBlue,clrMoccasin,clrWhiteSmoke,clrCyan,clrMediumPurple

  };

//--- 存储线型样式的数组

ENUM_LINE_STYLE styles[]={STYLE_SOLID,STYLE_DASH,STYLE_DOT,STYLE_DASHDOT,STYLE_DASHDOTDOT};

//+------------------------------------------------------------------+

//| 自定义指标初始化函数                                                |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- 指标缓冲区映射

   SetIndexBuffer(0,Color_ZigzagBuffer1,INDICATOR_DATA);

   SetIndexBuffer(1,Color_ZigzagBuffer2,INDICATOR_DATA);

   SetIndexBuffer(2,Color_ZigzagColors,INDICATOR_COLOR_INDEX);

//----为zigzag填充颜色的颜色数量

   color_sections=8;   //  请见注释 #property indicator_color1 属性

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| 自定义指标迭代函数                                                 |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

   static int ticks=0;

//--- 计算订单号改变样式,颜色和线型宽度

   ticks++;

//--- 如果足够数量的订单号被积累

   if(ticks>=N)

     {

      //--- 改变线型属性

      ChangeLineAppearance();

      //--- 改变用于标图节段的颜色

      ChangeColors(colors,color_sections);

      //--- 重置0计数器

      ticks=0;

     }



//--- 获得每个柱形工作日所需的时间结构

   MqlDateTime dt;



//--- 计算的初始位置

   int start=0;

//--- 如果在前一个订单号计算指标,那么在倒数第二个开始计算

   if(prev_calculated!=0) start=prev_calculated-1;

//--- 计算循环

   for(int i=start;i<rates_total;i++)

     {

      //--- 在结构中写下柱形的开盘时间

      TimeToStruct(time[i],dt);



      //--- 如果柱形数量

      if(i%2==0)

        {

         //---  在第一个缓冲区写下最高价,在第二个缓冲区写下最低价

         Color_ZigzagBuffer1[i]=high[i];

         Color_ZigzagBuffer2[i]=low[i];

         //--- 节段的颜色

         Color_ZigzagColors[i]=dt.day_of_year%color_sections;

        }

      //--- 柱形数量是奇数

      else

        {

         //--- 按相反顺序填写柱形

         Color_ZigzagBuffer1[i]=low[i];

         Color_ZigzagBuffer2[i]=high[i];

         //--- 节段的颜色

         Color_ZigzagColors[i]=dt.day_of_year%color_sections;

        }

     }

//--- 返回 prev_calculated值以便下次调用函数

   return(rates_total);

  }

//+------------------------------------------------------------------+

//| 改变zigzag节段的颜色                                               |

//+------------------------------------------------------------------+

void  ChangeColors(color  &cols[],int plot_colors)

  {

//--- 颜色数

   int size=ArraySize(cols);

//--- 

   string comm=ChartGetString(0,CHART_COMMENT)+"\r\n\r\n";



//--- 为每个颜色标引随机定义一个新的颜色

   for(int plot_color_ind=0;plot_color_ind<plot_colors;plot_color_ind++)

     {

      //--- 获得随机数

      int number=MathRand();

      //--- 获得col[]数组的标引作为整数除法的余数

      int i=number%size;

      //--- 设置每个标引的颜色为 PLOT_LINE_COLOR属性

      PlotIndexSetInteger(0,                    //  图形样式数量

                          PLOT_LINE_COLOR,      //  属性标识符

                          plot_color_ind,       //  颜色标引,我们在这里编写颜色

                          cols[i]);             //  新颜色

      //--- 编写颜色

      comm=comm+StringFormat("ZigzagColorIndex[%d]=%s \r\n",plot_color_ind,ColorToString(cols[i],true));

      ChartSetString(0,CHART_COMMENT,comm);

     }

//---

  }

//+------------------------------------------------------------------+

//| 改变zigzag节段的外观                                               |

//+------------------------------------------------------------------+

void ChangeLineAppearance()

  {

//--- 形成Color_ZigZag属性信息的字符串

   string comm="";

//--- 改变线型宽度的模块

   int number=MathRand();

//--- 获得整数除法余数的宽度

   int width=number%5;   // 宽度设置从 0 到 4

//--- 设置颜色为PLOT_LINE_WIDTH属性

   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,width);

//--- 写下线型宽度

   comm=comm+"\r\nWidth="+IntegerToString(width);



//--- 改变线型样式的模块

   number=MathRand();

//--- 除数等于样式数组的大小

   int size=ArraySize(styles);

//--- 获得选择新样式作为整数除法余数的标引

   int style_index=number%size;

//--- 设置颜色为 PLOT_LINE_COLOR 属性

   PlotIndexSetInteger(0,PLOT_LINE_STYLE,styles[style_index]);

//--- 写下线型样式

   comm="\r\n"+EnumToString(styles[style_index])+""+comm;

//--- 使用注释在图表上显示信息

   Comment(comm);

  }