//+------------------------------------------------------------------+
//| DRAW_COLOR_SECTION.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_SECTION"
#property description "It draws colored sections with the length equal to the specified number of bars"
#property description "The color, width and style of sections are changed randomly"
#property description "after every N ticks"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 1
//--- 标图ColorSection
#property indicator_label1 "ColorSection"
#property indicator_type1 DRAW_COLOR_SECTION
//--- 定义8 种用于填充节段的颜色(它们存储在指定数组)
#property indicator_color1 clrRed,clrGold,clrMediumBlue,clrLime,clrMagenta,clrBrown,clrTan,clrMediumVioletRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- 输入参数
input int N=5; // 改变订单号数量
input int bars_in_section=5; // 柱形节段的长度
//--- 计算节段终端的辅助变量
int divider;
int color_sections;
//--- 标图缓冲区
double ColorSectionBuffer[];
//--- 存储每柱线型颜色的缓冲区
double ColorSectionColors[];
//--- 存储颜色的数组包含14种元素
color colors[]=
{
clrRed,clrBlue,clrGreen,clrChocolate,clrMagenta,clrDodgerBlue,clrGoldenrod,
clrIndigo,clrLightBlue,clrAliceBlue,clrMoccasin,clrWhiteSmoke,clrCyan,clrMediumPurple
};
//--- 存储线型样式的数组
ENUM_LINE_STYLE styles[]={STYLE_SOLID,STYLE_DASH,STYLE_DOT,STYLE_DASHDOT,STYLE_DASHDOTDOT};
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 指标缓冲区映射
SetIndexBuffer(0,ColorSectionBuffer,INDICATOR_DATA);
SetIndexBuffer(1,ColorSectionColors,INDICATOR_COLOR_INDEX);
//--- 0(空)值将不参与绘制
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---- 为节段填充颜色的颜色数量
int color_sections=8; // 请见注释#property indicator_color1
//--- 检查指标参数
if(bars_in_section<=0)
{
PrintFormat("Invalid section length=%d",bars_in_section);
return(INIT_PARAMETERS_INCORRECT);
}
else divider=color_sections*bars_in_section;
//---
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=0;
//--- 如果之前计算了指标,那么在前一柱开始设置
if(prev_calculated>0) start=prev_calculated-1;
//--- 这是指标值的全部计算
for(int i=start;i<rates_total;i++)
{
//--- 如果柱数可被section_length整除,它意味着这是节段的终点
if(i%bars_in_section==0)
{
//--- 在该柱最高价设置节段终点
ColorSectionBuffer[i]=high[i];
//--- 柱数除以 section_length*number_of_colors的余数
int rest=i%divider;
//获得颜色数 = 从 0 到 number_of_colors-1
int color_indext=rest/bars_in_section;
ColorSectionColors[i]=color_indext;
}
//---如果除法的余数等于柱数,
else
{
//--- 如果什么也没发生,忽略柱形 - 设置0
else ColorSectionBuffer[i]=0;
}
}
//--- 返回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("SectionColorIndex[%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);
//--- 改变线型样式的模块
number=MathRand();
//--- 除数等于样式数组的大小
int 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);
}
|