金字塔公式 金字塔模型策略源码: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
复制上述代码粘贴到到公式管理器
源码解析:
输出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赋值:昨日真实波幅的20日指数移动平均
N赋值:VALUEWHEN(HOLDING=0,RN)
RH赋值:昨日最高价
RL赋值:昨日最低价
输出 H1:INB日内RH的最高值
输出 H2:OUTB日内RH的最高值,LINEDOT
输出 L1:INB日内RL的最低值
输出 L2:OUTB日内RL的最低值,LINEDOT
输出 LOTST:ASSET*RISK*0.01/(N*2*MULTIPLIER),线宽为0
LOTS赋值:如果RISK=0,返回1,否则返回LOTST
TBC赋值:H<>L
//判断是否停板 PARTLINE(HOLDING>0,ENTERPRICE-2*N)
逻辑判断 BARPOS<INB+1 THEN EXIT
BUYP赋值:开盘价和H1的较大值
BUY(1,LOTS,LIMITR,BUYP)
TIMES赋值:1
BUYP赋值:开盘价和ENTERPRICE+N*0.5的较大值
BUY(1,LOTS,LIMITR,BUYP)
TIMES赋值:TIMES+1
END
//连续开仓 END
SELLP赋值:开盘价和L1的较小值
BUYSHORT(1,LOTS,LIMITR,SELLP)
TIMES赋值:1
SELLP赋值:开盘价和ENTERPRICE-N*0.5的较小值
BUYSHORT(1,LOTS,LIMITR,SELLP)
TIMES赋值:TIMES+1
END
//连续开仓 END
END
EXITLONGP赋值:ENTERPRICE-2*N和L2的较大值
EXITP赋值:开盘价和EXITLONGP的较小值
SELL(1,0,LIMITR,EXITP)
TIMES赋值:0
END
BUYP赋值:开盘价和ENTERPRICE+N*0.5的较大值
BUY(1,LOTS,LIMITR,BUYP)
TIMES赋值:TIMES+1
END
//连续开仓 END
//ELSE END
EXITLONGP赋值:ENTERPRICE+2*N和H2的较小值
EXITP赋值:开盘价和EXITLONGP的较大值
SELLSHORT(1,0,LIMITR,EXITP)
TIMES赋值:0
END
SELLP赋值:开盘价和ENTERPRICE-N*0.5的较小值
BUYSHORT(1,LOTS,LIMITR,SELLP)
TIMES赋值:TIMES+1
END
//连续开仓 END
//ELSE END
//HOLDING<0