金字塔源码:
//定义全局变量并初始化
//nlow记录创20新高后3期内创2期新低时k线的最低值
//mhigh记录创20期新低后3期内创2期新高时k线的最高值
VARIABLE:nlow=0,mhigh=0;
//定义参数
Input:snum(1,1,100,1);
//中间变量
h20:=ref(hhv(h,20),1); //20周期最高价
l20:ref(llv(l,20),1); //20周期最低价
h2:=ref(hhv(h,2),1); //2周期最高价
l2:=ref(llv(l,2),1); //2周期最低价
//创20新高后3期内创2期新低,记录最低价
con1:=BARSLAST(h>h20)<=3 and low<l2;
if con1 then nlow:=low;
//创20期新低后3期内创2期新高,记录最高价
con2:=barslast(low<l20)<=3 and high>h2;
if con2 then mhigh:=high;
//交易条件
//开多平多条件
BuyCond:=barslast(con1)<=3 and high>h20;
SellCond1:=low<=nlow;
SellCond2:high-enterprice>2*abs(enterprice-nlow);
//开空平空条件
BuyshortCond:=barslast(con2)<=3 and l<l20;
SellshortCond1:=high>=mhigh;
SellshortCond2:enterprice-low>=2*abs(mhigh-enterprice);
//下单模块
if SellCond1 then 多损:Sell(holding>0,snum,market);
if SellCond2 then 多盈:Sell(holding>0,snum,market);
if SellshortCond1 then 空损:sellshort(holding<0,snum,market);
if SellshortCond2 then 空盈:sellshort(holding<0,snum,market);
//开空
if BuyshortCond then buyshort(holding=0,snum,market);
//开多
if BuyCond then Buy(holding=0,snum,market);