DRAW_COLOR_HISTOGRAM

DRAW_COLOR_HISTOGRAM样式根据一系列从0 到指定值的颜色列绘制一个直方图。该值从指标缓冲区获得。每个列都有其自己的来自预设定颜色的颜色。

直方图的宽度,颜色和样式的指定类似 DRAW_HISTOGRAM 样式 - 使用 编译程序指令 或动态使用 PlotIndexSetInteger() 函数。动态改变标图属性允许基于当前状况改变直方图的外观。

因为每个柱上从0水平绘制列,DRAW_COLOR_HISTOGRAM 应该更好的用在单独的图表窗口。大部分情况下这种类型的标图用于创建oscillator类型的指标,例如, Awesome OscillatorMarket Facilitation Index。对于空的不显示的值,应该指定0值。

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

  • 一个缓冲区用于在每柱上存储垂直节段的非零值,节段的第二个终端始终在指标的零线上;
  • 存储颜色标引的一个缓冲区,它被用于绘制节段(它使得仅设置非空值有意义)。

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

基于MathSin()绘制指定颜色正弦曲线的指标示例。所有 直方图列的颜色,宽度和样式每 N个订单号都会随机变化一次。柱形参数指定正弦曲线周期,指定柱形数量之后,正弦曲线将重复这个循环。

请注意,DRAW_COLOR_HISTOGRAM样式的plot1 ,使用编译程序指令#property设置5个颜色,然后在OnCalculate() 函数随机选择来自存储在colors[] 数组的14种颜色的颜色。N 参数设置在指标的 外部参数 ,使手动配置成为可能(指标属性窗口的参数标签)。

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

//|                                         DRAW_COLOR_HISTOGRAM.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_HISTOGRAM"

#property description "It draws a sinusoid as a histogram in a separate window"

#property description "The color and width of columns are changed randomly"

#property description "after every N ticks"

#property description "The bars parameter sets the number of bars to repeat the sinusoid"



#property indicator_separate_window

#property indicator_buffers 2

#property indicator_plots   1

//--- 输入参数

input int      bars=30;          // 柱形的正弦曲线周期

input int      N=5;              // 改变直方图的订单号数量

//--- 标图 Color_Histogram

#property indicator_label1  "Color_Histogram"

#property indicator_type1   DRAW_COLOR_HISTOGRAM

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

#property indicator_color1  clrRed,clrGreen,clrBlue,clrYellow,clrMagenta,clrCyan,clrMediumSeaGreen,clrGold

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- 缓冲区的值

double         Color_HistogramBuffer[];

//--- 颜色标引缓冲区

double         Color_HistogramColors[];

//--- 获得2Pi角弧度的因素,当乘以柱的参数时

double         multiplier;

int            color_sections;

//--- 存储颜色的数组包含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_HistogramBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,Color_HistogramColors,INDICATOR_COLOR_INDEX);

//---- 为正弦曲线填充颜色的颜色数量

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

//--- 计算乘数

   if(bars>1)multiplier=2.*M_PI/bars;

   else

     {

      PrintFormat("Set the value of bars=%d greater than 1",bars);

      //--- 提前终止指标

      return(INIT_PARAMETERS_INCORRECT);

     }

//---

   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;

     }



//--- 计算指标值

   int start=0;

//---如果之前OnCalculate开始期间已经计算

   if(prev_calculated>0) start=prev_calculated-1; // 在倒数第二柱设置计算起点

//--- 填写指标缓冲区的值

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

     {

      //--- 价值

      Color_HistogramBuffer[i]=sin(i*multiplier);

      //--- 颜色

      int color_index=i%(bars*color_sections);

      color_index/=bars;

      Color_HistogramColors[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("HistogramColorIndex[%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);

  }