DRAW_COLOR_ARROW

DRAW_COLOR_ARROW 样式根据指标缓冲区的值绘制颜色箭头( Wingdings的设置符号) 。与DRAW_ARROW相对照,在该样式中为每个符号可以设定来自indicator_color1 属性指定的预定义颜色的一种颜色。

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

交易品种代码使用PLOT_ARROW 属性进行设置。

//--- 定义PLOT_ARROW绘制的来自Wingdings字体的符号代码到

   PlotIndexSetInteger(0,PLOT_ARROW,code);

PLOT_ARROW的默认值=159 (一圈)。

每个箭头实际上都是有高度和定位点的符号,可以包含图表上的一些重要信息(例如,柱形的收盘价)。因此,我们可以额外指定像素的垂直移动,这并不依赖图表的比例。箭头沿着指定的像素数量将向下移动,虽然指标的值将会保持相同:

//--- 设置箭头的像素垂直移动

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,shift);

PLOT_ARROW_SHIFT的负值意味着箭头向上移动,正值表示箭头向下移动。

DRAW_COLOR_ARROW 样式可以被用在单独的图表子窗口和它的主窗口。由于不绘制空值并且也不会出现在"数据窗口",指标缓冲区的所有值都应该明确设定。缓冲区不通过零值初始化。

//--- Set an empty value

   PlotIndexSetDouble(DRAW_COLOR_ARROW_plot_index,PLOT_EMPTY_VALUE,0);

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

  • 存储价格值的一个缓冲区,它被用于绘制交易品种(加入PLOT_ARROW_SHIFT属性给予的像素移动);
  • 存储颜色标引的一个缓冲区,它被用于绘制箭头 (它使得仅设置非空值有意义)。

以比前柱收盘价高的收盘价在每柱上绘制箭头的指标示例。所有 箭头的宽度,移动和符号代码每 N 个订单号都会随机变化一次。 交易品种的颜色取决于绘制它的柱数。

在示例中,对于DRAW_COLOR_ARROW样式的plot1,属性,颜色和大小使用编译程序指令 #property设置,然后在 OnCalculate() 函数随机设置属性。N 参数设置在指标的 外部参数 ,使手动配置成为可能(指标属性窗口的参数标签)。

请注意,最初的8种颜色使用编译程序指令 #property设置,然后在 OnCalculate()函数,随机设置来自存储在colors[] 数组的14种颜色的颜色。

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

//|                                             DRAW_COLOR_ARROW.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_ARROW"

#property description "Draws different-color arrows set by Unicode characters, on a chart"

#property description "The color, size, shift and symbol code of the arrow are changed"

#property description " randomly every N ticks"

#property description "The code parameter sets the base value: code=159 (a circle)"



#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   1

//--- 标图ColorArrow

#property indicator_label1  "ColorArrow"

#property indicator_type1   DRAW_COLOR_ARROW

//--- 定义 8 种颜色用于为基于工作日的直方图填充颜色(它们存储在指定数组)

#property indicator_color1  clrRed,clrBlue,clrSeaGreen,clrGold,clrDarkOrange,clrMagenta,clrYellowGreen,clrChocolate

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1



//--- 输入参数

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

input ushort   code=159;    // 在DRAW_ARROW绘制的符号代码

int            color_sections;

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

double         ColorArrowBuffer[];

//--- 存储颜色标引的缓冲区

double         ColorArrowColors[];

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

color colors[]=

  {

   clrRed,clrBlue,clrGreen,clrChocolate,clrMagenta,clrDodgerBlue,clrGoldenrod,

   clrIndigo,clrLightBlue,clrAliceBlue,clrMoccasin,clrWhiteSmoke,clrCyan,clrMediumPurple

  };

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

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

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

int OnInit()

  {

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

   SetIndexBuffer(0,ColorArrowBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ColorArrowColors,INDICATOR_COLOR_INDEX);

//--- 定义PLOT_ARROW绘制的符号代码

   PlotIndexSetInteger(0,PLOT_ARROW,code);

//--- 设置箭头的像素垂直移动

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,5);

//--- 设置空值为 0

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);

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

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

//---

   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=1;

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

//--- 计算循环

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

     {

      //--- 如果当前收盘价高于之前的价格,请绘制一个箭头

      if(close[i]>close[i-1])

         ColorArrowBuffer[i]=close[i];

      //--- 否则指定null 值

      else

         ColorArrowBuffer[i]=0;

      //--- 箭头颜色

      int index=i%color_sections;

      ColorArrowColors[i]=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("ArrowColorIndex[%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);



//--- 改变箭头代码 (PLOT_ARROW)的模块

   number=MathRand();

//--- 获得整数除法的余数来计算新的箭头代码(从0到19)

   int code_add=number%20;

//--- 设置新的符号代码作为ode+code_add的结果

   PlotIndexSetInteger(0,PLOT_ARROW,code+code_add);

//--- 写下符号代码 PLOT_ARROW

   comm="\r\n"+"PLOT_ARROW="+IntegerToString(code+code_add)+comm;



//--- 改变箭头的像素垂直移动的模块

   number=MathRand();

//--- 获得整数除法余数的移动

   int shift=20-number%41;

//--- 设置新移动自

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,shift);

//--- 写下PLOT_ARROW_SHIFT移动

   comm="\r\n"+"PLOT_ARROW_SHIFT="+IntegerToString(shift)+comm;



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

   Comment(comm);

  }