MT5指标编写实例课程--两均线"金叉“”死叉“报警

 

今天我给大家讲解下,我们最常用的两均线"金叉“”死叉“报警指标该怎么编写:

下面代码为讲解只用,不能直接复制到mql5文件里,会运行出错,后面我会附件附上源代码文件:
//+------------------------------------------------------------------+
//| MAcross.mq5 |
//| Copyright 2009, MetaQuotes Software Corp. |
//| / |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link "/"
#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<rates_total;i++)
{
if((longBuffer[i-1]>smallBuffer[i-1])&&(longBuffer<smallBuffer)) 判断两均线产生金叉了
{
upBuffer=longBuffer;在金叉处画上白色箭头
if(i==prev_calculated)只针对当前K线报警历史K线就不用报警了
{Alert("up");}弹出窗口报警
}
if((longBuffer[i-1]<smallBuffer[i-1])&&(longBuffer>smallBuffer)) 判断两均线产生了死叉
{
downBuffer=longBuffer;在死叉处画上青色箭头
if(i==prev_calculated)只针对当前K线报警历史K线就不用报警了
{Alert("down");}弹出窗口报警
}
}
//---
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+

 

源码文件呢?

原因: