[PEL] 复制代码
input:n1(5,1,500,1),n2(40,1,1000,1),m(9,2,1000,1),q(0.3,0.01,1,0.01),y(3,1,100,1),n(6,1,300,1);
globalvariable:at:=0;//记录开仓前的总资产
globalvariable:maxp:=0;//开仓后最高价或者最低价
ma5:ma(c,n1);
ma40:ma(c,n2);
kd:ma5>ma40 and h=hhv(h,m);
kk:ma40<ma5 and l=llv(l,m);
atr:"atr.atr"(14);
ss:=tasset*0.3/(n*3*multiplier*atr);
tcd:time>=185800;
//实际运行中这里建议换成dynainfo( 7) 获取最新值
最新价:c;
多持仓:tbuyholdingex('','',2);
空持仓:tsellholdingex('','',2);
多均价:tavgenterpriceex2('','',0);
空均价:tavgenterpriceex2('','',1);
多可用:tbuyholdingex('','',1);
空可用:tsellholdingex('','',1);
if kd and 多可用=0 and tcd then
begin
tbuy(1,ss,mkt);
at:=tasset;
end
if kk and 空可用=0 and tcd then
begin
tbuyshort(1,ss,mkt);
at:=tasset;
end
多均价:=tavgenterpriceex2('','',0);
//持仓后,更新开多后的最高价
if tbuyholdingex('','',1)<>0 then
begin
if maxp=0 then maxp:=多均价;
if 最新价>maxp then begin
maxp:=最新价;
end
end
空均价:=tavgenterpriceex2('','',1);
//持仓后,更新开空后的最低价
if tsellholdingex('','',1)<>0 then
begin
if maxp=0 then maxp:=空均价;
if 最新价<maxp then
begin
maxp:=最新价;
end
end
//从盈利最高价回撤 y倍atr平仓
if (maxp-最新价)>=y*atr and 多可用>0 then
begin
tsell(1,0,mkt);
maxp:=0;
end
if (最新价-maxp)>=y*atr and 空可用>0 then
begin
tsellshort(1,0,mkt);
maxp:=0;
end
//总资产亏损30% 平仓。以开仓时候的资产总值作为基准值来计算浮亏百分百。
if (at-tasset)/at>=q and (多可用>0 or 空可用>0)then
begin
tsell(多可用>0,0,mkt);
tsellshort(空可用>0,0,mkt);
maxp:=0;
end
[PEL] 复制代码
//n1,n2是均线参数;m是高点判定周期,默认值是9;
//q是总资金的百分百
//n是品种数,这里建议回测品种数 和这个参数保持一致。否则代码本身无法知道你回测的品种数量的。
//y是表示3倍ATR回撤的参数
input:n1(5,1,500,1),n2(40,1,1000,1),m(9,2,1000,1),q(0.3,0.01,1,0.01),y(3,1,100,1),n(10,1,300,1);
//均线1,均线2.
ma5:ma(c,n1);
ma40:ma(c,n2);
//5日均线上穿40日均线,注意这里不是大于 是上穿.
kd:cross(ma5,ma40) and h=hhv(h,m);
kk:cross(ma40,ma5) and l=llv(l,m);
//调用日线当前最新的ATR值
atr:"atr.atr#DAY"(14);
//进行手数的计算,不用取值,下单时候系统会自动处理的
ss:tasset*q/(n*3*multiplier*atr);
//时间条件。回测中无法精确控制时间,因此在回测时候直接按照日线收盘时候作为条件处理,实际运行中则判断当前时间是否大于等于145955;
//不建议在不活跃品种中使用这个时间控制
tcd:1;
//实际运行时候注释上面这句,把下面这句注释去掉
//tcd:CURRENTTIME>=145955;
//实际运行中这里建议换成dynainfo( 7) 获取最新值
最新价:c;
多可用:tbuyholdingex('','',1);
空可用:tsellholdingex('','',1);
净持仓:多可用+空可用;
//开多
if kd and 多可用=0 and 净持仓=0 and tcd then
begin
tbuy(1,ss,mkt);
end
//开空
if kk and 空可用=0 and 净持仓=0 and tcd then
begin
tbuyshort(1,ss,mkt);
end
//表示当前品种持仓期间最高盈利价格的全局变量名称
strx:STKLABEL&'_maxp';
maxp:=EXTGBDATA(strx);//开仓后最高价或者最低价
多均价:=tavgenterpriceex2('','',0);
//持仓后,更新开多后的最高价
if tbuyholdingex('','',1)<>0 then
begin
if maxp=0 then maxp:=多均价;
if 最新价>maxp then begin
EXTGBDATASET(strx,最新价);
end
end
空均价:=tavgenterpriceex2('','',1);
//持仓后,更新开空后的最低价
if tsellholdingex('','',1)<>0 then
begin
if maxp=0 then maxp:=空均价;
if 最新价<maxp then
begin
EXTGBDATASET(strx,最新价);
end
end
//从盈利最高价回撤 y倍atr平多仓
if (maxp-最新价)>=y*abs(atr) and 多可用>0 then
begin
tsell(1,0,mkt);
EXTGBDATASET(strx,0);
end
if (最新价-maxp)>=y*abs(atr) and 空可用>0 then
begin
tsellshort(1,0,mkt);
EXTGBDATASET(strx,0);
end