[MT4指标]
MT5指标编写实例课程--两均线"金叉“”死叉“报警
今天我给大家讲解下,我们最常用的两均线"金叉“”死叉“报警指标该怎么编写:
首先我们打开MQL5编辑器:
新建一个指标文件:
上图中:
我们确定指标的名字是macross,
给指标定义两个外部参数,也就是均线的周期参数
longma表示长周期均线的周期参数。
smallma表示短周期均线的周期参数。
将来指标完成后 可以如果想改均线的周期可以在指标加载的时候修改很方便。
上图中:我们定义了要在指标上画四样东西:
long表示画一条长周期均线,颜色是红色。
small表示画一条短周期均线,颜色是黄色。
up表示画向上箭头,颜色是白色。
donw表示画向下箭头,颜色是青色。
做好以上工作后,我们点"完成"。
下面我们展示下,完成后的效果:
接下来我们来看代码:
下面代码为讲解只用,不能直接复制到mql5文件里,会运行出错,后面我会附件附上源代码文件:
//+------------------------------------------------------------------+ //| MAcross.mq5 | //| Copyright 2009, MetaQuotes Software Corp. | //| http://www.mql5.com
| //+------------------------------------------------------------------+ #property copyright "2009, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.00" #property indicator_chart_window 画在主图上 #property indicator_buffers 4 定义4个画线位置 #property indicator_plots 4 画4条线 //---- plot long #property indicator_label1 "long" 这条线的名称 #property indicator_type1 DRAW_LINE 这条线的类型是线性 #property indicator_color1 Red 这条线的颜色 #property indicator_style1 STYLE_SOLID 这条线是实线 #property indicator_width1 1 这条线的宽度是1 //---- plot small #property indicator_label2 "small" #property indicator_type2 DRAW_LINE #property indicator_color2 Yellow #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //---- plot up #property indicator_label3 "up" #property indicator_type3 DRAW_ARROW 这里我们要画箭头 #property indicator_color3 White #property indicator_style3 STYLE_SOLID #property indicator_width3 1 //---- plot down #property indicator_label4 "down" #property indicator_type4 DRAW_ARROW #property indicator_color4 MediumBlue #property indicator_style4 STYLE_SOLID #property indicator_width4 1 //--- input parameters input int longma=50; 长均线周期(也就是上面图中我们输入的) input int smallma=10; 短均线周期(也就是上面图中我们输入的) //--- indicator buffers double longBuffer; (也就是上面图中我们输入的画线是long的时候,这里就会产生一个longBuffer数组) double smallBuffer; 同上 double upBuffer; 同上 double downBuffer; 同上 int longma_handle; 这里定义长周期均线的句柄,有了句柄,以后就可以做相关的操作。 int smallma_handle; 这里定义短周期均线的句柄,有了句柄,以后就可以做相关的操作。 //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,longBuffer,INDICATOR_DATA); SetIndexBuffer(1,smallBuffer,INDICATOR_DATA); SetIndexBuffer(2,upBuffer,INDICATOR_DATA); SetIndexBuffer(3,downBuffer,INDICATOR_DATA); PlotIndexSetInteger(2,PLOT_DRAW_TYPE,DRAW_ARROW); PlotIndexSetInteger(3,PLOT_DRAW_TYPE,DRAW_ARROW); PlotIndexSetInteger(2,PLOT_ARROW,1001); 箭头类型是1001号箭头 PlotIndexSetInteger(3,PLOT_ARROW,1002); 箭头类型是1002号箭头 longma_handle=iMA(Symbol(),0,longma,0,MODE_SMA,PRICE_CLOSE); 初始化长周期均线函数 smallma_handle=iMA(Symbol(),0,smallma,0,MODE_SMA,PRICE_CLOSE); 初始化短周期均线函数 //--- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, K线总数,会随着K线的增加而增加 const int prev_calculated, 本根K线前一根K线的序号,通常来说prev_calculated=rates_total-1 const datetime& time, 存储所有K线的时间数组 const double& open, 存储所有K线的开盘价数组 const double& high, 存储所有K线的最高价数组 const double& low, 存储所有K线的最低价数组 const double& close, 存储所有K线的收盘价数组 const long& tick_volume, const long& volume, const int& spread) { int malong=CopyBuffer(longma_handle,0,0,rates_total,longBuffer); 使用longma_handle长周期均线数组句柄,把均线的值复制到longBuffer数组中 int masmall=CopyBuffer(smallma_handle,0,0,rates_total,smallBuffer); 使用smallma_handle长周期均线数组句柄,把均线的值复制到smallBuffer数组中 for(int i=10;ismallBuffer[i-1])&&(longBuffersmallBuffer)) 判断两均线产生了死叉
{
downBuffer=longBuffer;在死叉处画上青色箭头
if(i==prev_calculated)只针对当前K线报警历史K线就不用报警了
{Alert("down");}弹出窗口报警
}
}
//---
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
以上解释我尽量做到详细,但是也难免有遗漏之处,如果还有问题,请再提问吧!
原文来自:第一MT5编程论坛:http://www.mt5.net.cn
原文地址:http://www.mt5.net.cn/read.php?tid=287
MAcross.rar
//+------------------------------------------------------------------+ //| MAcross.mq5 | //| Copyright 2009, MetaQuotes Software Corp. | //| http://www.mql5.com
| //+------------------------------------------------------------------+ #property copyright "2009, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.00" #property indicator_chart_window 画在主图上 #property indicator_buffers 4 定义4个画线位置 #property indicator_plots 4 画4条线 //---- plot long #property indicator_label1 "long" 这条线的名称 #property indicator_type1 DRAW_LINE 这条线的类型是线性 #property indicator_color1 Red 这条线的颜色 #property indicator_style1 STYLE_SOLID 这条线是实线 #property indicator_width1 1 这条线的宽度是1 //---- plot small #property indicator_label2 "small" #property indicator_type2 DRAW_LINE #property indicator_color2 Yellow #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //---- plot up #property indicator_label3 "up" #property indicator_type3 DRAW_ARROW 这里我们要画箭头 #property indicator_color3 White #property indicator_style3 STYLE_SOLID #property indicator_width3 1 //---- plot down #property indicator_label4 "down" #property indicator_type4 DRAW_ARROW #property indicator_color4 MediumBlue #property indicator_style4 STYLE_SOLID #property indicator_width4 1 //--- input parameters input int longma=50; 长均线周期(也就是上面图中我们输入的) input int smallma=10; 短均线周期(也就是上面图中我们输入的) //--- indicator buffers double longBuffer; (也就是上面图中我们输入的画线是long的时候,这里就会产生一个longBuffer数组) double smallBuffer; 同上 double upBuffer; 同上 double downBuffer; 同上 int longma_handle; 这里定义长周期均线的句柄,有了句柄,以后就可以做相关的操作。 int smallma_handle; 这里定义短周期均线的句柄,有了句柄,以后就可以做相关的操作。 //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,longBuffer,INDICATOR_DATA); SetIndexBuffer(1,smallBuffer,INDICATOR_DATA); SetIndexBuffer(2,upBuffer,INDICATOR_DATA); SetIndexBuffer(3,downBuffer,INDICATOR_DATA); PlotIndexSetInteger(2,PLOT_DRAW_TYPE,DRAW_ARROW); PlotIndexSetInteger(3,PLOT_DRAW_TYPE,DRAW_ARROW); PlotIndexSetInteger(2,PLOT_ARROW,1001); 箭头类型是1001号箭头 PlotIndexSetInteger(3,PLOT_ARROW,1002); 箭头类型是1002号箭头 longma_handle=iMA(Symbol(),0,longma,0,MODE_SMA,PRICE_CLOSE); 初始化长周期均线函数 smallma_handle=iMA(Symbol(),0,smallma,0,MODE_SMA,PRICE_CLOSE); 初始化短周期均线函数 //--- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, K线总数,会随着K线的增加而增加 const int prev_calculated, 本根K线前一根K线的序号,通常来说prev_calculated=rates_total-1 const datetime& time, 存储所有K线的时间数组 const double& open, 存储所有K线的开盘价数组 const double& high, 存储所有K线的最高价数组 const double& low, 存储所有K线的最低价数组 const double& close, 存储所有K线的收盘价数组 const long& tick_volume, const long& volume, const int& spread) { int malong=CopyBuffer(longma_handle,0,0,rates_total,longBuffer); 使用longma_handle长周期均线数组句柄,把均线的值复制到longBuffer数组中 int masmall=CopyBuffer(smallma_handle,0,0,rates_total,smallBuffer); 使用smallma_handle长周期均线数组句柄,把均线的值复制到smallBuffer数组中 for(int i=10;i
原文地址:http://www.mt5.net.cn/read.php?tid=287
MAcross.rar