- 显示:
- 17619
- 等级:
- 已发布:
- 2021.06.23 04:56
-
需要基于此代码的EA交易或指标吗?请在自由职业者服务中订购 进入自由职业者服务
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 "-> wentxiong" #property link "https://www.mql5.com/zh/users/xiongsir/seller" #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); } //+------------------------------------------------------------------+

在手动或自动化交易中,一键平仓功能都是比较常用的,特别是在涉及风控管理的时候,能够快速止损,有效控制风险。这个脚本功能实现起来并不难,但在程序化实现的过程中,会有一些不同的应用场景。

这是一个MT5的程序,它显示未平仓头寸的总止损和止盈价值。

MT5挂单类型有六种,能够很好地适应回调和突破场景,在一些突破策略交易系统中应用较多,当挂单条件不再有效的时候,就会涉及到挂单的取消操作。六种挂单应用说明如下: Buy Limit(回踩买入):在当前价格下方,等待行情下跌到挂单位置触发买入单。 Sell Limit(回踩卖出):在当前价格上方,等待行情上涨到挂单位置触发卖出单。 Buy Stop(突破买入):在当前价格上方,等待行情上涨到挂单位置触发买入单。 Buy Stop(突破卖出):在当前价格下方,等待行情下跌到挂单位置触发卖出单。 Buy Stop Limit(突破回踩买入):在当前价格上方,等待行情上涨到突破位置后,再下跌到回踩位置,触发买入单。 Sell Stop Limit(突破回踩卖出):在当前位置下方,等待行情下跌到突破位置后, 再上涨到回踩位置,触发卖出单。

价格是行情分析的重要参照因素,尤其是以支撑阻力位为核心的交易系统中,寻找合适的进场和出场价格位置是首要任务。那么按价格标记出这些参照位置就对分析、统计有实际意义。