DRAW_LINE

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

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

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

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

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

//|                                                    DRAW_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_LINE"

#property description "It draws a line of a specified color at Close prices"

#property description "Color, width and style of lines is changed randomly"

#property description "after every N ticks"



#property indicator_chart_window

#property indicator_buffers 1

#property indicator_plots   1

//--- 线型属性使用编译程序指令设置

#property indicator_label1  "Line"      // 数据窗口的标图名称

#property indicator_type1   DRAW_LINE   // 标图类型是线型

#property indicator_color1  clrRed      // 线型颜色

#property indicator_style1  STYLE_SOLID // 线型样式

#property indicator_width1  1           // 线型宽度

//--- 输入参数

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

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

double         LineBuffer[];

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

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

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

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

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

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

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

int OnInit()

  {

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

   SetIndexBuffer(0,LineBuffer,INDICATOR_DATA);

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

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

      //--- 重置0计数器

      ticks=0;

     }



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

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

     {

      LineBuffer[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+(string)colors[color_index];



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

   number=MathRand();

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

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

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

   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,width);

//--- 写下线型宽度

   comm=comm+", Width="+IntegerToString(width);



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

   number=MathRand();

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

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

  }