MQL5画多种颜色空心K线的方法

MQL5多种颜色空心K线的方法

实际效果图如下:
在这里插入图片描述原理:
这是MQL5文档中关于Draw_Cadles 的文档说明

//--- 不同颜色的蜡烛图和烛芯 
#property indicator_label1  "One color candles" 
#property indicator_type1   DRAW_CANDLES 
//--- 烛芯和外框是绿色,牛市蜡烛图的主体是白色,而熊市蜡烛图的主体是红色 
#property indicator_color1  clrGreen,clrWhite,clrRed

上面的最后一条预编译指令就是在设置K线颜色,第一个颜色(clrGreen)为上、下影线及主体外框线颜色,第一个颜色(clrWhite)为上涨K线的主体颜色,最后的一个颜色(clrRed)为下跌K线的主体颜色。要想形成空心K线实际上只要使K线的主体颜色与MT5的背景颜色相同,即可形成空心K线效果。MT5中背景色多半为黑色或白色,只要将中间的颜色设置为相应的颜色即可。

如果想要绘制出多种颜色的空心K线,只要利用画多个蜡烛图拼接而成。让显示的部分高开低收Buffer接收正常的K线数据,使不想显示的K线数据Buffer设置为0.0实现隐藏即可。

具体实现步骤:
(1)设置两色均线Buffer

//--- plot MA20
#property indicator_label1  "MA20"
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrRed,clrDarkTurquoise
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3

(2)设置多头空心蜡烛图1

//--- plot LongCandles
#property indicator_label2  "LongCandles"
#property indicator_type2   DRAW_CANDLES
#property indicator_color2  clrRed,clrBlack,clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

(3)设置空头空心蜡烛图2

//--- plot ShortCandles
#property indicator_label3  "ShortCandles"
#property indicator_type3   DRAW_CANDLES
#property indicator_color3  clrDarkTurquoise,clrBlack,clrDarkTurquoise
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1

(4)设置Buffer数组

//--- indicator buffers
double        MA20Buffer[];
double        MA20Colors[];
double        LongCandlesOpen[];
double        LongCandlesHigh[];
double        LongCandlesLow[];
double        LongCandlesClose[];
double        ShortCandlesOpen[];
double        ShortCandlesHigh[];
double        ShortCandlesLow[];
double        ShortCandlesClose[];

(5)OnInit()函数设置

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
	//指标缓冲区Buffer绑定
	SetIndexBuffer(0,MA20Buffer,INDICATOR_DATA);
	SetIndexBuffer(1,MA20Colors,INDICATOR_COLOR_INDEX);

	SetIndexBuffer(2,LongCandlesOpen,INDICATOR_DATA);
	SetIndexBuffer(3,LongCandlesHigh,INDICATOR_DATA);
	SetIndexBuffer(4,LongCandlesLow,INDICATOR_DATA);
	SetIndexBuffer(5,LongCandlesClose,INDICATOR_DATA);

	SetIndexBuffer(6,ShortCandlesOpen,INDICATOR_DATA);
	SetIndexBuffer(7,ShortCandlesHigh,INDICATOR_DATA);
	SetIndexBuffer(8,ShortCandlesLow,INDICATOR_DATA);
	SetIndexBuffer(9,ShortCandlesClose,INDICATOR_DATA);

	//时间序列化指标Buffer
	ArraySetAsSeries(MA20Buffer,true);
	ArraySetAsSeries(MA20Colors,true);

	ArraySetAsSeries(LongCandlesOpen,true);
	ArraySetAsSeries(LongCandlesHigh,true);
	ArraySetAsSeries(LongCandlesLow,true);
	ArraySetAsSeries(LongCandlesClose,true);

	ArraySetAsSeries(ShortCandlesOpen,true);
	ArraySetAsSeries(ShortCandlesHigh,true);
	ArraySetAsSeries(ShortCandlesLow,true);
	ArraySetAsSeries(ShortCandlesClose,true);

	//buffer初始化填充0.0
	ArrayInitialize(LongCandlesOpen,0.0);
	ArrayInitialize(LongCandlesHigh,0.0);
	ArrayInitialize(LongCandlesLow,0.0);
	ArrayInitialize(LongCandlesClose,0.0);

	ArrayInitialize(ShortCandlesOpen,0.0);
	ArrayInitialize(ShortCandlesHigh,0.0);
	ArrayInitialize(LongCandlesOpen,0.0);
	ArrayInitialize(ShortCandlesLow,0.0);

	//设置空值不画线,将值为0.0的K线不显示
	PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
	PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0);
	PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0);
   
	MA20_h = iMA(Symbol(),PERIOD_CURRENT,MA20_Period,MA20_Shift,SM_Mode,AppliedPrice);
//---
    return(INIT_SUCCEEDED);
}

(6)OnCalculate()函数实现

//+------------------------------------------------------------------+
//| 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[])
{
	//时间序列化高开低收价
	ArraySetAsSeries(open,true);
	ArraySetAsSeries(high,true);
	ArraySetAsSeries(low,true);
	ArraySetAsSeries(close,true);
       
	if(MA20_h > 0)//check the handle Valid?
	{
		//valid
		if(BarsCalculated(MA20_h) < rates_total)//check all Bars Calculated?
			return(rates_total);
		else
		{
			if(prev_calculated <= 0)
			{
			   //all bars Calculated
			   CopyBuffer(MA20_h,0,0,rates_total,MA20Buffer);
			}
			else
			{  
			   // 仅填充那些更新的数据 
			   int to_copy=rates_total-prev_calculated;
			   // 如果没有不同,我们仍然会复制一个值 - 在零柱
			   if(to_copy == 0 )  to_copy = 1;
			   CopyBuffer(MA20_h,0,0,to_copy,MA20Buffer);
			}
		}
	}
	//更新颜色缓冲区
	int start;
	if(prev_calculated <= 0)
	{
		start = 0;
	}
	else
	{
		start = prev_calculated-1;
	}
	for(int i = start; i<rates_total; i++)
	{
		if(MA20Buffer[i] < close[i])
		{  
		   
			MA20Colors[i] = 0;
			
			//均线在收盘价之上LongCandlesBuffer填充数据
			LongCandlesOpen[i]=open[i];
			LongCandlesHigh[i]=high[i];
			LongCandlesLow[i]=low[i];
			LongCandlesClose[i]=close[i];
			
			//均线在收盘价之上ShortCandlesBuffer填充0.0,让其不显示
			ShortCandlesOpen[i]=0.0;
			ShortCandlesHigh[i]=0.0;
			ShortCandlesLow[i]=0.0;
			ShortCandlesClose[i]=0.0;

		}
		else
		{
			MA20Colors[i] = 1;
         //均线在收盘价之下LongCandlesBuffer填充0.0,让其不显示
			LongCandlesOpen[i]=0.0;
			LongCandlesHigh[i]=0.0;
			LongCandlesLow[i]=0.0;
			LongCandlesClose[i]=0.0;
			
			//均线在收盘价之下ShortCandlesBuffer填充数据
			ShortCandlesOpen[i]=open[i];
			ShortCandlesHigh[i]=high[i];
			ShortCandlesLow[i]=low[i];
			ShortCandlesClose[i]=close[i];
		}
	}

	//--- return value of prev_calculated for next call
	return(rates_total);
}

源码链接: MQL5多色空心K线源码.
作者:HalfGiant

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值