MQL5变色线绘制教程:比MQL4更简便的实现方法
在MQL5中,有一种特殊的指标数组叫做“颜色数组”,它与绘制线条的指标数组配合使用。通过对其简单赋值,可以使画出的线条实现变色效果。这一特性在德璞资本官网的许多技术分析案例中都有所提及。
首先,需要在指标头部的定义中指定某条线对应的数组采用变色画线方式。指定方法如下:
#property indicator_typeX DRAW_COLOR_LINE
这里,X代表画线数组的序号。
DRAW_COLOR_LINE代表画线,此外还可以使用以下几种画线方式:
复制代码
DRAW_COLOR_LINE
Colorful Line 彩色线
DRAW_COLOR_SECTION
Multicolored section 彩色块
DRAW_COLOR_HISTOGRAM
Multicolored histogram from the zero line 彩色柱状图
DRAW_COLOR_HISTOGRAM2
Multicolored histogram of the two indicator buffers 彩色柱状图2
DRAW_COLOR_ARROW
Drawing colored arrows 彩色箭头
DRAW_COLOR_ZIGZAG
Colorful ZigZag 彩色ZigZag
DRAW_COLOR_BARS
Multi-colored bars 彩色竹线图
DRAW_COLOR_CANDLES
Multi-colored candles 彩色蜡烛图
接下来,需要紧跟一个颜色定义语句:
#property indicator_colorX Red,Green
两个颜色之间使用逗号分隔。
============================================
针对上述程序头部的定义,接下来需要开始全局数组的定义。
需要注意,实现变色效果需要为一条线使用两个数组。
例如:
double bMaBuffer[],bColorBuffer[];
然后进入OnInit事件,对两个数组分别进行设定:
SetIndexBuffer(0,bMaBuffer,INDICATOR_DATA); // INDICATOR_DATA表示用于画线的数组
SetIndexBuffer(1,bColorBuffer,INDICATOR_COLOR_INDEX); // INDICATOR_COLOR_INDEX表示用于变色的颜色数组
注意:
如果需要绘制多条彩色线,则画线数组和颜色数组的序号应当紧邻。
============================================
下一步是在OnCalculate事件中计算画线数组,同时根据自定义条件对颜色数组进行赋值。
赋值规则如下:
当对应K线序号的颜色数组被赋值为1.0时,对应画线数组的颜色为第一个颜色。
当对应K线序号的颜色数组被赋值为0.0时,对应画线数组的颜色为第二个颜色。
完成。
程序示例源码如下:【绘制两条变色线】
复制代码
//+------------------------------------------------------------------+
//| Test.mq5 |
//| Copyright 2009, MetaQuotes Software Corp. |
//| |
//+------------------------------------------------------------------+
#property copyright "2009, 520FX"
#property link
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 2
#property indicator_color1 Red,Green
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
#property indicator_color2 Yellow,Blue
#property indicator_type2 DRAW_COLOR_LINE
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
input int MaPeriod=13;
double bMaBuffer[],bMaBuffer1[],bColorBuffer[],bColorBuffer1[];
int iMaHandle,iMaHandle1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,bMaBuffer,INDICATOR_DATA);
SetIndexBuffer(1,bColorBuffer,INDICATOR_COLOR_INDEX);
SetIndexBuffer(2,bMaBuffer1,INDICATOR_DATA);
SetIndexBuffer(3,bColorBuffer1,INDICATOR_COLOR_INDEX);
IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
iMaHandle=iMA(NULL,0,MaPeriod,0,MODE_SMA,PRICE_CLOSE);
iMaHandle1=iMA(NULL,0,MaPeriod+50,0,MODE_SMA,PRICE_CLOSE);
//---
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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[])
{
//--- return value of prev_calculated for next call
//--- checking for bars count
if(rates_total return(0); //--- detect start position int start; //if(prev_calculated1) start=prev_calculated-1; //else start=1; if(prev_calculated<0)return(-1);else start=rates_total-prev_calculated+1; int to_copy; if(prev_calculatedrates_total || prev_calculated<0) to_copy=rates_total; else { to_copy=rates_total-prev_calculated; if(prev_calculated0) to_copy++; } if(CopyBuffer(iMaHandle,0,0,to_copy,bMaBuffer)<=0) { Print("Getting fast SMA is failed! Error",GetLastError()); return(0); } if(CopyBuffer(iMaHandle1,0,0,to_copy,bMaBuffer1)<=0) { Print("Getting fast SMA1 is failed! Error",GetLastError()); return(0); } //--- main cycle for(int i=start;i { if(bMaBuffer[i]close[i-1]) bColorBuffer[i]=1.0; else bColorBuffer[i]=0.0; if(bMaBuffer1[i]close[i-1]) bColorBuffer1[i]=1.0; else bColorBuffer1[i]=0.0; } return(rates_total); } //+------------------------------------------------------------------+ 下一篇:暂无