和风版的海龟交易系统金字塔源码改编为文华财经麦语言
//金字塔的海龟交易系统源码
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
改编为文华财经麦语言源码:
INB:=20;
OUTB:=10;
RISK:=2;//定义参数
TR : =MAX(MAX((HIGH-LOW),ABS(REF(CLOSE,1)-HIGH)),ABS(REF(CLOSE,1)-LOW));//TR定义
RN:=EMA(REF(TR,1),20); //一周期前TR的20周期平滑移动平均
N:=VALUEWHEN(BKVOL=0&&SKVOL=0,RN); //最近一次持仓为0时的RN值
H1:HV(H,INB); //前20个周期的最高价
H2:HV(H,OUTB),DOT; //前10个周期的最高价
L1:LV(L,INB); //前20个周期的最高价
L2:LV(L,OUTB),DOT; //前10个周期的最高价
LOTST:MONEYTOT*RISK*0.01/(N*2*UNIT),NODRAW; //模组资金*0.02计算的开仓手数
LOTS:=IF(RISK=0,1,LOTST); //如果RISK取0,表示固定开1手
TBC:=H<>L;//判断是否一字停板 不是返回1,是返回0
PARTLINE1(BKVOL>0,BKPRICE-2*N);
BARPOS>=INB+1&&BKVOL=0&&TBC&&H>H1,BK(LOTS);//多头持仓为0时,且不是一字停板并且创20周期新高,买开LOTS手
BARPOS>=INB+1&&BKVOL>0&&H>BKPRICE+N*0.5,BK(LOTS);//多头持仓大于0时,且不是一字停板并且最高价大于买开仓价格+n*0.5,买开LOTS手
BARPOS>=INB+1&&SKVOL=0&&TBC&&L<L1,SK(LOTS);//空头持仓为0时,且不是一字停板并且创20周期新低,卖开LOTS手
BARPOS>=INB+1&&SKVOL>0&&L<SKPRICE-N*0.5,SK(LOTS);//空头持仓大于0时,且不是一字停板并且最低价小于卖开仓价格-n*0.5,卖开LOTS手
BKVOL>0&&L<MAX(BKPRICE-2*N,L2),SP(BKVOL);//多头持仓大于0并且最低价小于10周期最低价或者小于买开价-2*N,平多仓
SKVOL>0&&H>MIN(SKPRICE+2*N,H2),BP(SKVOL);//空头持仓大于0并且最高价大于10周期最高价或者大于卖开价+2*N,平空仓
TRADE_AGAIN(3);