DRAW_COLOR_LINE

DRAW_COLOR_LINE 值是DRAW_LINE 样式的颜色变量;它也使用指标缓冲区的值绘制线型。但是该样式,类似标题有COLOR单词的所有颜色样式一样,有一个额外特定的指标缓冲区,它可以存储来自特别颜色数组的颜色标引(数量)。因此,每个线型节段的颜色都可以通过指定颜色标引进行定义,在该柱绘制线型。

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

标图DRAW_COLOR_LINE 所需的缓冲区数量是 2。

  • 一个缓冲区存储用于绘制线型的指标值;
  • 一个缓冲区存储每个柱形的线型颜色标引。

颜色可以通过编译程序指令 #property indicator_color1指定,以逗号分隔。颜色数量不能超过64种。

//--- 定义5种用于填充每个柱形的颜色(它们存储在指定数组)

#property indicator_color1  clrRed,clrBlue,clrGreen,clrOrange,clrDeepPink // (Up to 64 colors can be specified)

使用柱形收盘价绘制线型的指标示例。线型的宽度和样式每N=5 订单号都会随机变化一次。

线型节段颜色在自定义函数ChangeColors()也会随机变化。

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

//| 改变线段颜色                                                       |

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

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("LineColorIndex[%d]=%s \r\n",plot_color_ind,ColorToString(cols[i],true));

      ChartSetString(0,CHART_COMMENT,comm);

     }

//---

  }

该示例显示了指标"color"版的特点 - 改变线型节段的颜色,您无需改变ColorLineColors[]缓冲区的值(包含颜色标引)。您所需要做的只是在特定数组设置新的颜色。这允许您为整体标图快速改变一次颜色,只使用PlotIndexSetInteger() 函数改变小型颜色数组。

请注意,最初DRAW_COLOR_LINE属性的plot1 使用编译程序指令#property设置,然后在OnCalculate() 函数这三种属性随机设置。

N 和长度 (柱形颜色节段的长度) 参数设置在指标的 外部参数 ,使手动配置成为可能(指标属性窗口的参数标签)。

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

//|                                              DRAW_COLOR_LINE.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_LINE"

#property description "It draws a line on Close price in colored pieces of 20 bars each"

#property description "The width, style and color of the line parts are changed randomly"

#property description "every N ticks"



#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   1

//--- 标图 ColorLine

#property indicator_label1  "ColorLine"

#property indicator_type1   DRAW_COLOR_LINE

//--- 定义5种用于填充每个柱形的颜色(它们存储在指定数组)

#property indicator_color1  clrRed,clrBlue,clrGreen,clrOrange,clrDeepPink // (Up to 64 colors can be specified)

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- 输入参数

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

input int      Length=20;     // 柱形每个颜色部分的长度

int            line_colors=5; // 设置颜色数量是 5 - 见 #property indicator_color1

//--- 标图缓冲区

double         ColorLineBuffer[];

//--- 存储每柱线型颜色的缓冲区

double         ColorLineColors[];



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

color colors[]={clrRed,clrBlue,clrGreen,clrChocolate,clrMagenta,clrDodgerBlue,clrGoldenrod};

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

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

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

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

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

int OnInit()

  {

//--- 绑定数组和指标缓冲区

   SetIndexBuffer(0,ColorLineBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ColorLineColors,INDICATOR_COLOR_INDEX);

//--- 初始化随机数生成器

   MathSrand(GetTickCount());

//---

   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,5);

      //--- 重置0计数器

      ticks=0;

     }



//--- 计算指标值的模块

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

     {

      //--- 指标值写入缓冲区

      ColorLineBuffer[i]=close[i];

      //--- 现在,为该柱随机设置颜色标引

      int color_index=i%(5*Length);

      color_index=color_index/Length;

      //--- 对于该柱,线型将具有color_index标引的颜色

      ColorLineColors[i]=color_index;

     }



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

   return(rates_total);

  }

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

//| 改变线段颜色                                                       |

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

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("LineColorIndex[%d]=%s \r\n",plot_color_ind,ColorToString(cols[i],true));

      ChartSetString(0,CHART_COMMENT,comm);

     }

//---

  }

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

//| 改变指标的线型显示外观                                              |

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

void ChangeLineAppearance()

  {

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

   string comm="";

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

   int number=MathRand();

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

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

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

   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,width);

//--- 写下线型宽度

   comm=comm+" Width="+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=EnumToString(styles[style_index])+", "+comm;

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

   Comment(comm);

  }