策略原理:
此策略是价格突破上下轨的同时,同时满足6条均线的价格也要超过上下轨才会开仓,可以防止假的突破开仓,减少亏损次数。策略比较适合商品是单边行情。
测试商品rb,周期为5 min。
源码如下:
Input:Btime(0930),Etime(1445);
//定义时间段。
var:Thigh(0),Tlow(0), mp(0),x(0),y(0),m(6),ma(0),flag1(0),flag2(0);
//定义变量
if date<>date[1] then begin
mp=0;
x=0;
y=0;
Thigh=0;
Tlow=0;
flag1=0;
flag2=0;
end;
//每天给变量重新赋值。
if time=Btime then begin
Thigh=highD(0);
Tlow=lowD(0);
end;
//给高点跟低点赋值,以开盘前6个bar的最高为高点,最低为低点。
value1=tl_new(date,900,thigh,date,1500,thigh);
value2=tl_new(date,900,tlow,date,1500,tlow);
//绘图上下轨。
ma=Average(close,m);
//均线价格。修改m值即可调整用来过滤的均线。
mp=marketposition;
//持仓多空定义。
if mp[1]<>1 and mp=1 then begin
x=1;
end;
if mp[1]<>-1 and mp=-1 then begin
y=1;
end;
//用来控制每天多空的进场次数。
if time>Btime and time
if mp=0 and close crosses above thigh then begin
flag1=1;
end;
//当价格上穿上轨时给flag1赋值。
if flag1=1 and x<=1 and ma>=thigh then
buy ("b1")next bar at Thigh+1 stop;
end;
//当价格上穿后且均线也上穿上轨此时做多。
if mp<>-1 then sell("sp") next bar at tlow stop;
//做多后以下轨的价格做出场点位。防止反向亏损。
if time>Btime and time
if mp=0 and close crosses below Tlow then begin
flag2=1;
end;
//当价格下穿下轨时给flag2赋值。
if flag2=1 and y<=1 and ma<=Tlow then
sellshort("s1") next bar at tlow-1 stop;
end;
//当价格下穿且均线下穿下轨此时做空。
if mp<>1 then buytocover("bp") next bar at thigh stop;
//做空后以上轨的价格做出场点位,防止反向亏损。
if time>=1450 and mp<>-1 then sell("sppcs") next bar at market;
if time>=1450 and mp<>1 then buytocover("sppcb") next bar at market;
//收盘前平仓语句。