//+------------------------------------------------------------------+
//| 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);
}
|