双线一柱变色MACD指标

MT5已经自带了很多指标,给交易带来了极大便利,作为辅助工具,很多时候我们需要对其进行进一步改造,或重新自定义指标,本例通过实现双线一柱变色MACD指标来进行说明。

添加绘制元素

在这个例子中,除了MACD自带的2条线,上柱、下柱外,还需要2块填充区、上下箭头,共同完成一个酷炫的指标,分别对应绘制元素Line、Color Histogram、Filling、Arrow。创建自定义指标时,会引导添加这些元素,添加完成后生成的文件会自动将需要设置的部分全部写好。

取指标数据

通过获取系统自带的MACD指标数据,进行变色重建,实现起来非常方便,可以直接取到需要的数据。需要先建立指标句柄,再用CopyBuffer()函数取数据。

int macd_handle = 0;
macd_handle = iMACD(Symbol(),Period(),12,26,9,PRICE_CLOSE);
//取指标数据
CopyBuffer(macd_handle,0,0,rates_total,macdBuffer);
CopyBuffer(macd_handle,1,0,rates_total,signalBuffer);

填充缓冲区

生成自定义指标,其实就是填充缓冲区的过程,将指标线、柱图、填充区、箭头缓冲区数据进行赋值,就可以实现我们需要的效果。

在填充箭头缓冲区时,需要更改箭头的样式,并且只是在双线出现金叉、死叉时才赋值,所以还需要将0值位置设为空绘制区PLOT_EMPTY_VALUE。

PlotIndexSetInteger(5,PLOT_ARROW,225);
PlotIndexSetInteger(6,PLOT_ARROW,226);
PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,0);
PlotIndexSetDouble(6,PLOT_EMPTY_VALUE,0);

需要注意的是,采用循环遍历赋值的方式,循环变量起始值在初始加载指标时为1,加载之后为prev_calculated - 1,这样在循环体内,即加载了所有历史数据,也实时更新最新数据,不会重新计算所有历史值。

//给缓冲区赋值,起始值

int start = 1;
if(prev_calculated > 1)
{
start = prev_calculated - 1;
}
for(int i = start; i < rates_total; i++)
{
//循环体
}

实现源码

#property copyright "公众号 Luyuanmw 微信 wentxiong"
#property 源码讲解   "https://study.163.com/course/courseMain.htm?courseId=1213774825&share=2&shareId=480000002309742"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 12
#property indicator_plots   7
//--- plot macd
#property indicator_label1  "macd"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot signal
#property indicator_label2  "signal"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot upper
#property indicator_label3  "upper"
#property indicator_type3   DRAW_COLOR_HISTOGRAM
#property indicator_color3  clrPurple,clrDimGray
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2
//--- plot down
#property indicator_label4  "down"
#property indicator_type4   DRAW_COLOR_HISTOGRAM
#property indicator_color4  clrPurple,clrDimGray
#property indicator_style4  STYLE_SOLID
#property indicator_width4  2
//--- plot fill
#property indicator_label5  "fill"
#property indicator_type5   DRAW_FILLING
#property indicator_color5  clrRed,clrBlue
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1
//--- plot arrow
#property indicator_label6  "arrow"
#property indicator_type6   DRAW_COLOR_ARROW
#property indicator_color6  clrRed,clrBlue
#property indicator_style6  STYLE_SOLID
#property indicator_width6  1
#property indicator_label7  "arrow2"
#property indicator_type7   DRAW_COLOR_ARROW
#property indicator_color7  clrRed,clrBlue
#property indicator_style7  STYLE_SOLID
#property indicator_width7  1
//--- indicator buffers
double         macdBuffer[];
double         signalBuffer[];
double         upperBuffer[];
double         upperColors[];
double         downBuffer[];
double         downColors[];
double         fillBuffer1[];
double         fillBuffer2[];
double         arrowBuffer[];
double         arrowColors[];
double         arrow2Buffer[];
double         arrow2Colors[];
int macd_handle = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,macdBuffer,INDICATOR_DATA);
SetIndexBuffer(1,signalBuffer,INDICATOR_DATA);
SetIndexBuffer(2,upperBuffer,INDICATOR_DATA);
SetIndexBuffer(3,upperColors,INDICATOR_COLOR_INDEX);
SetIndexBuffer(4,downBuffer,INDICATOR_DATA);
SetIndexBuffer(5,downColors,INDICATOR_COLOR_INDEX);
SetIndexBuffer(6,fillBuffer1,INDICATOR_DATA);
SetIndexBuffer(7,fillBuffer2,INDICATOR_DATA);
SetIndexBuffer(8,arrowBuffer,INDICATOR_DATA);
SetIndexBuffer(9,arrowColors,INDICATOR_COLOR_INDEX);
SetIndexBuffer(10,arrow2Buffer,INDICATOR_DATA);
SetIndexBuffer(11,arrow2Colors,INDICATOR_COLOR_INDEX);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
PlotIndexSetInteger(5,PLOT_ARROW,225);
PlotIndexSetInteger(6,PLOT_ARROW,226);
PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,0);
PlotIndexSetDouble(6,PLOT_EMPTY_VALUE,0);
//指标handle
macd_handle = iMACD(Symbol(),Period(),12,26,9,PRICE_CLOSE);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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[])
{
//---
//指标线缓冲区
CopyBuffer(macd_handle,0,0,rates_total,macdBuffer);
CopyBuffer(macd_handle,1,0,rates_total,signalBuffer);
//给缓冲区赋值,起始值
int start = 1;
if(prev_calculated > 1)
{
start = prev_calculated - 1;
}
for(int i = start; i < rates_total; i++)
{
//柱图缓冲区
if(macdBuffer[i] > signalBuffer[i])
{
upperBuffer[i] = macdBuffer[i] - signalBuffer[i];
}
else
{
downBuffer[i] = macdBuffer[i] - signalBuffer[i];
}
//柱图颜色缓冲区
if(upperBuffer[i] > upperBuffer[i - 1])
{
upperColors[i] = 0;
}
else
{
upperColors[i] = 1;
}
if(downBuffer[i] < downBuffer[i - 1])
{
downColors[i] = 0;
}
else
{
downColors[i] = 1;
}
//填充区缓冲区
fillBuffer1[i] = macdBuffer[i];
fillBuffer2[i] = signalBuffer[i];
//箭头缓冲区
if(macdBuffer[i] > signalBuffer[i] && macdBuffer[i - 1] < signalBuffer[i - 1])
{
arrowBuffer[i - 1] = macdBuffer[i] - 0.0005;
arrowColors[i - 1] = 0;
}
if(macdBuffer[i] < signalBuffer[i] && macdBuffer[i - 1] > signalBuffer[i - 1])
{
arrow2Buffer[i - 1] = macdBuffer[i] + 0.0003;
arrow2Colors[i - 1] = 1;
}
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路远马亡Trade

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值