DRAW_ARROW
DRAW_ARROW样式根据指标缓冲区的值绘制指定颜色的箭头( Wingdings的设置符号) 。交易品种的宽度和颜色的指定类似 DRAW_LINE 样式 - 使用编译程序指令 或动态使用 PlotIndexSetInteger() 函数。动态改变标图属性允许基于当前状况改变指标的外观。
交易品种代码使用PLOT_ARROW 属性进行设置。
//--- 定义PLOT_ARROW绘制的来自Wingdings字体的符号代码到
PlotIndexSetInteger(0,PLOT_ARROW,code);PLOT_ARROW的默认值=159 (一圈)。
每个箭头实际上都是有高度和定位点的符号,可以包含图表上的一些重要信息(例如,柱形的收盘价)。因此,我们可以额外指定像素的垂直移动,这并不依赖图表的比例。箭头沿着指定的像素数量将向下移动,虽然指标的值将会保持相同:
//--- 设置箭头的像素垂直移动
PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,shift);PLOT_ARROW_SHIFT的负值意味着箭头向上移动,正值表示箭头向下移动。
DRAW_ARROW 样式可以被用在单独的图表子窗口和它的主窗口。由于不绘制空值并且也不会出现在"数据窗口",指标缓冲区的所有值都应该明确设定。缓冲区不通过零值初始化。
//--- 设置空值
PlotIndexSetDouble(index_of_plot_DRAW_ARROW,PLOT_EMPTY_VALUE,0);标图DRAW_ARROW所需的缓冲区的数量是 1。
以比前柱收盘价高的收盘价在每柱上绘制箭头的指标示例。所有 箭头的颜色,宽度,移动和符号代码每 N 个订单号都会随机变化一次。
在示例中, 对于DRAW_ARROW 样式的 plot1,属性,颜色和大小使用编译程序指令 #property设置,然后在OnCalculate() 函数随机设置属性。N 参数设置在指标的 外部参数 ,使手动配置成为可能(指标属性窗口的参数标签)。
//+------------------------------------------------------------------+
//| DRAW_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_ARROW"
#property description "Draws arrows set by Unicode characters, on a chart"
#property description "The color, size, shift and symbol code of the arrow are changed in a random way"
#property description "after every N ticks"
#property description "The code parameter sets the base value: code=159 (a circle)"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
//--- 标图箭头
#property indicator_label1 "Arrows"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrGreen
#property indicator_width1 1
//--- 输入参数
input int N=5; // 改变订单号数量
input ushort code=159; // 在DRAW_ARROW绘制的符号代码
//--- 标图的指标缓冲区
double ArrowsBuffer[];
//--- 存储颜色的数组0到5的
color colors[]={clrRed,clrBlue,clrGreen};
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 指标缓冲区映射
SetIndexBuffer(0,ArrowsBuffer,INDICATOR_DATA);
//--- 定义PLOT_ARROW绘制的符号代码
PlotIndexSetInteger(0,PLOT_ARROW,code);
//--- 设置箭头的像素垂直移动
PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,5);
//--- 设置空值为 0
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---
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=1;
if(prev_calculated>0) start=prev_calculated-1;
//--- 计算循环
for(int i=1;i<rates_total;i++)
{
//--- 如果当前收盘价高于之前的价格,请绘制一个箭头
if(close[i]>close[i-1])
ArrowsBuffer[i]=close[i];
//--- 否则指定零值
else
ArrowsBuffer[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
//--- 设置颜色为PLOT_LINE_WIDTH属性
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,width);
//--- 写下箭头大小
comm=comm+"\r\nWidth="+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;
//--- 设置新移动从 -20 到 20
PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,shift);
//--- 写下PLOT_ARROW_SHIFT移动
comm="\r\n"+"PLOT_ARROW_SHIFT="+IntegerToString(shift)+comm;
//--- 使用注释在图表上显示信息
Comment(comm);
}