画线的就先不处理了,下面是图表版本的代码,你先看下效果吧:
[PEL] 复制代码 input:len(15);
input:m1(0.05);
input:m2(10);
input:maxperiod(20);
variable:tp:=0;
variable:spacep:=0;
variable:inicost:=0;
variable:ratchet:=0;
variable:stopline:=0;
variable:barn:=0;
//ATR
value1:ma(tr,len);
last_spacep:=ref(spacep,1);
last_value:=ref(value1,1);
lv:=llv(low,m2);
//初始开仓,开仓语句要写在前面,否则会影响到后面某些函数的取值
buy(holding=0 and TODAYBAR=1,1,marketr);
//开仓K记录此时的开仓价
if enterbars=0 then begin
tp:=enterprice;
end
if holding=1 then begin
if enterbars>=1 then
begin
if high>tp then tp:=high;
end;
//最大盈利
spacep:=tp-enterprice;
//输出spacep
spacep_value:spacep;
//最大盈利大于一个ATR:记录此时的最低价和K位置
if last_spacep<last_value and spacep>value1 then begin
inicost:=lv;
barn:=barpos;
drawtext(1,high+10,'start ratchet');
end;
if spacep>value1 then
begin
ratchet:=enterbars* m1*value1;
//根据持仓周期更新止盈价
stopline:=inicost + ratchet;
//最新价大于止盈价 平仓
ATR止盈:sell(c>=stopline,holding,LIMITR,stopline);
end
//持仓超过指定周期,且盈利没有达到一个atr 直接平仓
if enterbars>=maxperiod and spacep<value1 then 到期平仓:sell(1,holding,market);
end; |