样例如下:
代码逻辑是这样的,开仓条件是60均线突破开仓,减仓是跌破20日均线减仓,加仓条件是只要之前有过减仓则减仓2手,不过这里加仓的处理是只要之前有减仓就加仓,所以可能会出现连续加仓的。
ma5:ma(c,5);//5日均线
ma20:ma(c,20);
ma60:ma(c,60);
buycond1:=cross(c,ma60) and holding=0;//突破60日均线开仓
buycond2:cross(c,ma5);//减仓后再突破5日均线加仓
sellcond1:=cross(ma20,c) and holding>0;//跌破20日均线减仓
LastSell:=BARSLAST(sellcond1);
if buycond1 then buy(1,5,MARKET);//开仓
if sellcond1 then sell(1,2,MARKET);//减仓
if buycond2 and LastSell>0 then buy(1,2,MARKET);//加仓
代码如下:
ma5:ma(c,5);//5日均线
ma20:ma(c,20);
ma60:ma(c,60);
sellcond1:=cross(ma20,c) and c>ma60 and holding>0;//跌破20日均线减仓条件
buycond1:=cross(c,ma60) and holding=0;//突破60日均线开仓条件
LastSell:=BARSLAST(sellcond1);
buycond2:cross(c,ma5) and holding<5 and LastSell>0;//加仓条件
if buycond1 then buy(1,5,MARKET);//开仓
if sellcond1 then sell(1,2,MARKET);//减仓
if buycond2 then buy(1,2,MARKET);//加仓
当前持仓:holding;