DRAW_HISTOGRAM2

DRAW_HISTOGRAM2 样式使用两个指标缓冲区的值绘制一个指定颜色–垂直段的直方图。节段的宽度,颜色和样式的指定类似DRAW_LINE 样式 - 使用编译程序指令 或动态使用 PlotIndexSetInteger() 函数。动态改变标图属性允许基于当前状况改变直方图的外观。

DRAW_HISTOGRAM2 样式可以被用在单独的图表子窗口和它的主窗口。由于不绘制空值,指标缓冲区的所有值都需要明确设定。缓冲区不通过零值初始化。

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

在每个柱形的开盘价和收盘价之间标图指定颜色和宽度的垂直节段的指标示例。所有 直方图列的颜色,宽度和样式每N个订单号都会随机变化一次。指标启动期间,在OnInit() 函数,不会绘制直方图的工作日数量 - invisible_day - 随机设置。为此,空值设置 PLOT_EMPTY_VALUE=0:

//--- 设置空值

   PlotIndexSetDouble(index_of_plot_DRAW_SECTION,PLOT_EMPTY_VALUE,0);

请注意,最初DRAW_HISTOGRAM2属性的plot1 使用编译程序指令 #property设置,然后在OnCalculate()函数这三种属性随机设置。N 参数设置在指标的 外部参数 ,使手动配置成为可能(指标属性窗口的参数标签)。

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

//|                                              DRAW_HISTOGRAM2.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_HISTOGRAM2"

#property description "It draws a segment between Open and Close on each bar"

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

#property description "after every N ticks"



#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   1

//--- 标图Histogram_2

#property indicator_label1  "Histogram_2"

#property indicator_type1   DRAW_HISTOGRAM2

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- 输入参数

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

//--- 指标缓冲区

double         Histogram_2Buffer1[];

double         Histogram_2Buffer2[];

//--- 没有标图指标的工作日

int invisible_day;

//--- 存储颜色的数组0到5的

color colors[]={clrRed,clrBlue,clrGreen};

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

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

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

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

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

int OnInit()

  {

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

   SetIndexBuffer(0,Histogram_2Buffer1,INDICATOR_DATA);

   SetIndexBuffer(1,Histogram_2Buffer2,INDICATOR_DATA);

//--- 设置空值

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);

//--- 获得从0到5的随机数

   invisible_day=MathRand()%6;

//---

   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();

      //--- 重置0计数器

      ticks=0;

     }



//--- 计算指标值

   int start=0;

//--- 通过每柱的开盘价获得工作日

   MqlDateTime dt;

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

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

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

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

     {

      TimeToStruct(time[i],dt);

      if(dt.day_of_week==invisible_day)

        {

         Histogram_2Buffer1[i]=0;

         Histogram_2Buffer2[i]=0;

        }

      else

        {

         Histogram_2Buffer1[i]=open[i];

         Histogram_2Buffer2[i]=close[i];

        }

     }

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

   return(rates_total);

  }

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

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

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

void ChangeLineAppearance()

  {

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

   string comm="";

//--- 线型颜色变化模块

   int number=MathRand(); // 获得随机数

//--- 除数等于colors[]数组的大小

   int size=ArraySize(colors);

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

   int color_index=number%size;

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

   PlotIndexSetInteger(0,PLOT_LINE_COLOR,colors[color_index]);

//--- 写下线型颜色

   comm=comm+"\r\n"+(string)colors[color_index];



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

   number=MathRand();

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

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

//--- 设置线型宽度

   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,width);

//--- 写下线型宽度

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



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

   number=MathRand();

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

   size=ArraySize(styles);

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

   int style_index=number%size;

//--- 设置线型样式

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

//--- 写下线型样式

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

//--- 添加计算中省略的工作日的信息

   comm="\r\nNot plotted day - "+EnumToString((ENUM_DAY_OF_WEEK)invisible_day)+comm;

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

   Comment(comm);

  }