DRAW_SECTION

DRAW_SECTION 通过指标缓冲区的值以指定颜色绘制一个节段。线的宽度,颜色和样式的指定类似DRAW_LINE 样式 - 使用 编译程序指令 或动态使用PlotIndexSetInteger()函数。动态改变标图属性允许“激活”指标,以便于可以根据当前状况改变其外观。

从一个非空值到指标缓冲区的另一个非空值绘制节段,空值将被忽略。若要指定哪个值应被视为“空”,请在PLOT_EMPTY_VALUE属性设定该值:例如,如果指标应该被绘制为非零值的系列节段,然后您需要设置零值为空值:

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

   PlotIndexSetDouble(index_of_plot_DRAW_SECTION,PLOT_EMPTY_VALUE,0);

始终明确填写指标缓冲区的值,向不应标图的元素设置缓冲区空值。

标图 DRAW_SECTION所需的缓冲区的数量是 1。

绘制最高价和最低价之间节段的指标示例。所有 节段的颜色,宽度和样式每N个订单号都会随机变化一次。

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

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

//|                                                 DRAW_SECTION.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_SECTION"

#property description "Draws straight sections every bars bars"

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

#property description "after every N ticks"



#property indicator_chart_window

#property indicator_buffers 1

#property indicator_plots   1

//--- 标图节段

#property indicator_label1  "Section"

#property indicator_type1   DRAW_SECTION

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- 输入参数

input int      bars=5;           // 柱形节段长度

input int      N=5;              // 改变节段样式的订单号数量

//--- 标图的指标缓冲区

double         SectionBuffer[];

//--- 计算阶段终点的辅助变量

int            divider;

//--- 存储颜色的数组

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

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

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

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

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

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

int OnInit()

  {

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

   SetIndexBuffer(0,SectionBuffer,INDICATOR_DATA);

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

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);

//--- 检查指标参数

   if(bars<=0)

     {

      PrintFormat("Invalid value of parameter bar=%d",bars);

      return(INIT_PARAMETERS_INCORRECT);

     }

   else divider=2*bars;

//---+

   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;

//--- 如果之前计算了指标,那么在前一柱开始设置

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

//--- 这是指标值的全部计算

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

     {

      //--- 获得柱数除以2*bars的余数

      int rest=i%divider;

      //--- 如果柱数可被2*bars除尽

      if(rest==0)

        {

         //--- 在该柱最高价设置节段终点

         SectionBuffer[i]=high[i];

        }

      //---如果除法的余数等于柱数, 

      else

        {

         //--- 在该柱最高价设置节段终点

         if(rest==bars) SectionBuffer[i]=low[i];

         //--- 如果什么也没发生,忽略柱形 - 设置0

         else SectionBuffer[i]=0;

        }

     }

//--- 返回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;

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

   Comment(comm);

  }