本帖最后由 瞬间的光辉 于 2009-10-30 07:37 编辑 Y,}h{*9Kd wL3RcXW``e 今天我给大家讲解下,我们最常用的两均线"金叉“”死叉“报警指标该怎么编写:
k\T]*A 首先我们打开MQL5编辑器:
iV?8'^
Y%eW6Y# 新建一个指标文件:
cQThpgha
OAW_c.)5D
@jMo/kO/A 上图中:
OGJrwl 我们确定指标的名字是macross,
G:lhrT{ 给指标定义两个外部参数,也就是均线的周期参数
Vraz}JV longma表示长周期均线的周期参数。
1EQ:@1 smallma表示短周期均线的周期参数。
<kwF<J 将来指标完成后 可以如果想改均线的周期可以在指标加载的时候修改很方便。
%P<fz1
*& );-r`. 上图中:我们定义了要在指标上画四样东西:
0k?]~f long表示画一条长周期均线,颜色是红色。
W
wj+\ small表示画一条短周期均线,颜色是黄色。
UCj+V@{ up表示画向上箭头,颜色是白色。
*,O3@,+>H donw表示画向下箭头,颜色是青色。
4Jc~I 做好以上工作后,我们点"完成"。
+/mCYI Tw`dLK? 下面我们展示下,完成后的效果:
|+sAqx1IF
p*YV*Arv 接下来我们来看代码:
M($GZ~ b%A 下面代码为讲解只用,不能直接复制到mql5文件里,会运行出错,后面我会附件附上源代码文件: &%/T4$'+Y+ //+------------------------------------------------------------------+
UEYJd&n0CB //| MAcross.mq5 |
*nHMQ/uf //|Copyright 2009, MetaQuotes Software Corp. |
;tA$
x!5] //|
http://www.mql5.com |
DOkuT/+ //+------------------------------------------------------------------+
!>Q\Y`a,* #property copyright "2009, MetaQuotes Software Corp."
<KCyXU* #property link "http://www.mql5.com"
P&tw!B #property version "1.00"
.~rg#*]^ #property indicator_chart_window 画在主图上
seP h%Sa_ #property indicator_buffers 4 定义4个画线位置
o^owv( #property indicator_plots 4 画4条线
psz0q| //---- plot long
{/aHZ<I&^h #property indicator_label1 "long" 这条线的名称
H<?s[MH[ #property indicator_type1 DRAW_LINE 这条线的类型是线性
rp[3?-fk #property indicator_color1 Red 这条线的颜色
2hryY #property indicator_style1 STYLE_SOLID 这条线是实线
V 7ZGT
#property indicator_width1 1 这条线的宽度是1
S9%,{y //---- plot small
C'Y2kb #property indicator_label2 "small"
aS\$@41" #property indicator_type2 DRAW_LINE
WA:r4V #property indicator_color2 Yellow
~ ^ #property indicator_style2 STYLE_SOLID
m_`%#$s} #property indicator_width2 1
`c-omNu //---- plot up
/5j5\F:33 #property indicator_label3 "up"
Y|
ch ; #property indicator_type3 DRAW_ARROW 这里我们要画箭头
3Z&!zSK^ #property indicator_color3 White
Vi}E9I4 #property indicator_style3 STYLE_SOLID
,_,*I/o>B #property indicator_width3 1
=_ b/g //---- plot down
\^RKb-6n #property indicator_label4 "down"
('Uj|m}9 #property indicator_type4 DRAW_ARROW
v_WQ<G? #property indicator_color4 MediumBlue
RgQs`aI #property indicator_style4 STYLE_SOLID
$60`Hh 4/ #property indicator_width4 1
Q[K)Yd //--- input parameters
VY+P c/b input int longma=50; 长均线周期(也就是上面图中我们输入的)
C"_ Roir? input int smallma=10; 短均线周期(也就是上面图中我们输入的)
L
G5_\sY! //--- indicator buffers
bcpsjUiy# double longBuffer[]; (也就是上面图中我们输入的画线是long的时候,这里就会产生一个longBuffer[]数组)
TB+k[UxB double smallBuffer[]; 同上
}f?[m&< double upBuffer[]; 同上
ZILJXX4 double downBuffer[]; 同上
]W`?0VwF int longma_handle; 这里定义长周期均线的句柄,有了句柄,以后就可以做相关的操作。
#vga
qe9 int smallma_handle; 这里定义短周期均线的句柄,有了句柄,以后就可以做相关的操作。
$[,4Ib_| +qC[X~\ //+------------------------------------------------------------------+
liH#=C8l*% //| Custom indicator initialization function |
8B?U\cfa^ //+------------------------------------------------------------------+
W&%,XwkQ int OnInit()
H${L F.8 {
-mG`* 0 //--- indicator buffers mapping
P@vUQ SetIndexBuffer(0,longBuffer,INDICATOR_DATA);
Wtcib- SetIndexBuffer(1,smallBuffer,INDICATOR_DATA);
-kMw[Y SetIndexBuffer(2,upBuffer,INDICATOR_DATA);
nk%v|ZxoFv SetIndexBuffer(3,downBuffer,INDICATOR_DATA);
#YE?&5t Nn FR; PlotIndexSetInteger(2,PLOT_DRAW_TYPE,DRAW_ARROW);
*0!p_Hco PlotIndexSetInteger(3,PLOT_DRAW_TYPE,DRAW_ARROW);
NOXP}M PlotIndexSetInteger(2,PLOT_ARROW,1001); 箭头类型是1001号箭头
Nyy&'\`! PlotIndexSetInteger(3,PLOT_ARROW,1002); 箭头类型是1002号箭头
(!5Ta7X longma_handle=iMA(Symbol(),0,longma,0,MODE_SMA,PRICE_CLOSE); 初始化长周期均线函数
:) lG}c
smallma_handle=iMA(Symbol(),0,smallma,0,MODE_SMA,PRICE_CLOSE); 初始化短周期均线函数
|du%c`wl //---
1~L;S return(0);
hFi gY\$m }
-!1=S: S //+------------------------------------------------------------------+
$kTm"I //| Custom indicator iteration function |
W-s 6+DY //+------------------------------------------------------------------+
M=liG+d int OnCalculate(const int rates_total, K线总数,会随着K线的增加而增加
.t1:;H b const int prev_calculated, 本根K线前一根K线的序号,通常来说prev_calculated=rates_total-1
f%o[eW# const datetime time[], 存储所有K线的时间数组
Q`9c/vPU const double open[], 存储所有K线的开盘价数组
vYV!8o.I const double high[], 存储所有K线的最高价数组
LRu,_2" const double low[], 存储所有K线的最低价数组
DH?n~qKpC const double close[], 存储所有K线的收盘价数组
(a_bU5) const long tick_volume[],
/(JG\Ut const long volume[],
T&'Jc const int spread[])
)%@7tx {
(SRY(q int malong=CopyBuffer(longma_handle,0,0,rates_total,longBuffer); 使用longma_handle长周期均线数组句柄,把均线的值复制到longBuffer数组中
vd lss| int masmall=CopyBuffer(smallma_handle,0,0,rates_total,smallBuffer); 使用smallma_handle长周期均线数组句柄,把均线的值复制到smallBuffer数组中
oM!&S'M/ &K0b3AWc for(int i=10;i
L6_%SGY_iE
{ |)!f".`
if((longBuffer[i-1]>smallBuffer[i-1])&(longBuffer ar<8wq<4G
{ )K{ s^]Jp
upBuffer=longBuffer;在金叉处画上白色箭头 ~".@mubt1$
if(i==prev_calculated)只针对当前K线报警历史K线就不用报警了 32 i6j
{Alert("up");}弹出窗口报警 ;<garDf
} qhY+<S9
if((longBuffer[i-1]smallBuffer)) 判断两均线产生了死叉 INr1bAe$
{ $Lj]NtO
downBuffer=longBuffer;在死叉处画上青色箭头 A6=Z2i0w>X
if(i==prev_calculated)只针对当前K线报警历史K线就不用报警了 `6VnL)
{Alert("down");}弹出窗口报警 #4m5I="
} A.r7 ks
} \]<R`YMV
//--- s8,YQ5-
//--- return value of prev_calculated for next call X&zGgP/
return(rates_total); o>M^&)Xs
} tlQ6>v'
//+------------------------------------------------------------------+ ?Vg~7Eu0
以上解释我尽量做到详细,但是也难免有遗漏之处,如果还有问题,请再提问吧! #-YbZ
原文来自:第一MT5编程论坛:http://www.mt5.net.cn原文地址:http://www.mt5.net.cn/read.php?tid=287 :0y-n.-{
MAcross.rar (5 K) 下载次数:60