DRAW_ZIGZAG
DRAW_ZIGZAG 样式根据指标缓冲区的值绘制指定颜色的节段。该样式非常相似于DRAW_SECTION,但与后者都不太相同,如果为该柱设置两个指标缓冲区的值,它允许在一个柱内绘制垂直节段。节段从第一个缓冲区的值标图到第二个指标缓冲区的值。缓冲区不会只包含空值,因为在这种情况下,不会标图。
线的宽度,颜色和样式的指定类似 DRAW_SECTION 样式 - 使用 编译程序指令 或动态使用 PlotIndexSetInteger() 函数。动态改变标图属性允许 "激活" 指标,以便于可以根据当前状况改变其外观。
从一个缓冲区的非空值到另一个指标缓冲区的非空值绘制部分。若要指定哪个值应被视为“空”,请在 PLOT_EMPTY_VALUE 属性设定该值:
//--- 0(空)值将不参与绘制
PlotIndexSetDouble(index_of_plot_DRAW_ZIGZAG,PLOT_EMPTY_VALUE,0);始终明确填写指标缓冲区的值,在缓冲区设置空值跳过柱形。
标图DRAW_ZIGZAG所需的缓冲区的数量是2。
基于最高价和最低价标图的指标示例。zigzag 线的颜色,宽度和样式每N 个订单号都会随机变化一次。
请注意,最初DRAW_ZIGZAG属性的plot1 使用编译程序指令#property设置,然后在OnCalculate() 函数这些属性随机设置。N 参数设置在指标的 外部参数 ,使手动配置成为可能(指标属性窗口的参数标签)。
//+------------------------------------------------------------------+
//| DRAW_ZIGZAG.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_ZIGZAG"
#property description "It draws a \"saw\" as straight segments, skipping the bars of one day"
#property description "The day to skip is selected randomly during indicator start"
#property description "The color, width and style of segments are changed randomly"
#property description " every N ticks"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots 1
//--- 标图ZigZag
#property indicator_label1 "ZigZag"
#property indicator_type1 DRAW_ZIGZAG
#property indicator_color1 clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- 输入参数
input int N=5; // 改变订单号数量
//--- 指标缓冲区
double ZigZagBuffer1[];
double ZigZagBuffer2[];
//--- 没有标图指标的工作日
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,ZigZagBuffer1,INDICATOR_DATA);
SetIndexBuffer(1,ZigZagBuffer2,INDICATOR_DATA);
//--- 获得从0到6的随机值,在这一天不标图指标
invisible_day=MathRand()%6;
//--- 0(空)值将不参与绘制
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//--- 0(空)值将不参与绘制
PlotIndexSetString(0,PLOT_LABEL,"ZigZag1;ZigZag2");
//---
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;
}
//--- 获得每个柱形工作日所需的时间结构
MqlDateTime dt;
//--- 计算的初始位置
int start=0;
//--- 如果在前一个订单号计算指标,那么在倒数第二个开始计算
if(prev_calculated!=0) start=prev_calculated-1;
//--- 计算循环
for(int i=start;i<rates_total;i++)
{
//--- 在结构中写下柱形的开盘时间
TimeToStruct(time[i],dt);
//--- 如果该柱的工作日等于invisible_day
if(dt.day_of_week==invisible_day)
{
//--- 写下该柱形的缓冲区空值
ZigZagBuffer1[i]=0;
ZigZagBuffer2[i]=0;
}
//--- 如果工作日没问题,填写缓冲区
else
{
//--- 如果柱形数量
if(i%2==0)
{
//--- 在第一个缓冲区写下最高价,在第二个缓冲区写下最低价
ZigZagBuffer1[i]=high[i];
ZigZagBuffer2[i]=low[i];
}
//--- 柱形数量是奇数
else
{
//--- 按相反顺序填写柱形
ZigZagBuffer1[i]=low[i];
ZigZagBuffer2[i]=high[i];
}
}
}
//--- 返回 prev_calculated值以便下次调用函数
return(rates_total);
}
//+------------------------------------------------------------------+
//| 改变zigzag节段的外观 |
//+------------------------------------------------------------------+
void ChangeLineAppearance()
{
//--- 形成ZigZag属性信息的字符串
string comm="";
//--- 改变ZigZag颜色的模块
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);
//--- 改变线型样式的模块
number=MathRand();
//--- 除数等于样式数组的大小
size=ArraySize(styles);
//--- 获得选择新样式作为整数除法余数的标引
int style_index=number%size;
//--- 设置颜色为 PLOT_LINE_COLOR 属性
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);
}