源码解析理论
输出INPUT:INB(20,1,500,1),OUTB(10,1,500,1),RISK(2,0,10,0.1)
输出//和风版海龟源码VARIABLE:TIMES=0,I=0,N=0
RN赋值:1日前的真实波幅的20日指数移动平均
N赋值:如果HOLDING=0,返回RN,否则返回上个输出值
RH赋值:1日前的最高价
RL赋值:1日前的最低价
输出H1:INB日内RH的最高值
输出H2:OUTB日内RH的最高值,LINEDOT
输出L1:INB日内RL的最低值
输出L2:OUTB日内RL的最低值,LINEDOT
输出LOTST:ASSET*RISK*0.01/(N*2*期货每手乘数),线宽为0
LOTS赋值:如果RISK=0,返回1,否则返回LOTST
示固定开1手TBC赋值:H<>L
//判断是否停板PARTLINE(HOLDING>0,ENTERPRICE-2*N)
条件判断 BARPOS<INB+1 THEN EXIT
开多BEGINBUYP赋值:开盘价和H1的较大值
开仓买入
TIMES赋值:1
DOBEGINBUYP赋值:开盘价和ENTERPRICE+N*0.5的较大值
开仓买入
TIMES赋值:TIMES+1
END
//连续开仓END
开空BEGINSELLP赋值:开盘价和L1的较小值
平仓买入
TIMES赋值:1
DOBEGINSELLP赋值:开盘价和ENTERPRICE-N*0.5的较小值
平仓买入
TIMES赋值:TIMES+1
END
//连续开仓END
END
ITLONGP赋值:ENTERPRICE-2*N和L2的较大值
出场BEGINEXITP赋值:开盘价和EXITLONGP的较小值
平仓卖出
TIMES赋值:0
END
开多BEGINBUYP赋值:开盘价和ENTERPRICE+N*0.5的较大值
开仓买入
TIMES赋值:TIMES+1
END
//连续开仓END
//ELSEEND
ITLONGP赋值:ENTERPRICE+2*N和H2的较小值
出场BEGINEXITP赋值:开盘价和EXITLONGP的较大值
开仓卖出
TIMES赋值:0
END
开多BEGINSELLP赋值:开盘价和ENTERPRICE-N*0.5的较小值
平仓买入
TIMES赋值:TIMES+1
END
//连续开仓END
//ELSEEND
//HOLDING<0
源码:
input:inb(20,1,500,1),outb(10,1,500,1),risk(2,0,10,0.1);
//和风版海龟源码
variable:times=0,i=0,n=0;
rn:=ema(ref(tr,1),20);
n:=valuewhen(holding=0,rn);
rh:=ref(h,1);
rl:=ref(l,1);
h1:hhv(rh,inb);
h2:hhv(rh,outb),linedot;
l1:llv(rl,inb);
l2:llv(rl,outb),linedot;
lotst:asset*risk*0.01/(n*2*multiplier),linethick0;
lots:=if(risk=0,1,lotst);
//如果risk取0,表示固定开1手
tbc:=h<>l;
//判断是否停板
partline(holding>0,enterprice-2*n);
if barpos<inb+1 then exit;
if holding=0 and tbc then //不是停板才可以交易
begin
if h>h1 then //开多
begin
buyp:=max(o,h1);
buy(1,lots,limitr,buyp);
times:=1;
while h>enterprice+n*0.5 and times<4 do
begin
buyp:=max(o,enterprice+n*0.5);
buy(1,lots,limitr,buyp);
times:=times+1;
end;
//连续开仓
end;
//开多结束
else if l<l1 then //开空
begin
sellp:=min(o,l1);
buyshort(1,lots,limitr,sellp);
times:=1;
while l<enterprice-n*0.5 and times<4 do
begin
sellp:=min(o,enterprice-n*0.5);
buyshort(1,lots,limitr,sellp);
times:=times+1;
end;
//连续开仓
end;
end;
//holding=0
if holding>0 and tbc then //已有多仓
begin //
exitlongp:=max(enterprice-2*n,l2);
if l<exitlongp and enterbars<>0 then //出场
begin
exitp:=min(o,exitlongp);
sell(1,0,limitr,exitp);
times:=0;
end;
//出场
else
begin
while h>enterprice+n*0.5 and times<4 do //开多
begin
buyp:=max(o,enterprice+n*0.5);
buy(1,lots,limitr,buyp);
times:=times+1;
end;
//连续开仓
end;
//else
end;
//holding>0
if holding<0 and tbc then //已有空仓
begin
exitlongp:=min(enterprice+2*n,h2);
if h>exitlongp and enterbars<>0 then //出场
begin
exitp:=max(o,exitlongp);
sellshort(1,0,limitr,exitp);
times:=0;
end;
//出场
else
begin
while l<enterprice-n*0.5 and times<4 do //开多
begin
sellp:=min(o,enterprice-n*0.5);
buyshort(1,lots,limitr,sellp);
times:=times+1;
end;
//连续开仓
end;
//else
end;
//holding<0