我的问题策略模型代码要求说明:
1,非多即空;底仓交易手ss=2;
2,若平仓盈利时,不加仓;
3,若平仓亏损,则:亏1次,加仓1手ss+1,连亏2次,加仓2手ss+2... 直到连亏6次时,加仓6手ss+6;此后若再出现连亏,停止加仓,直到反向交易,从ss=2手开始交易。
我写的后台交易代码如下,经反复测试,或变通改写,均不能正确满足我的设计要求。出现的技术问题是,开多、开空起步交易正常,当出现多次连亏后,多空反手交易仅能前2次加仓正确,
多头仓到ss+1=3,空头仓到ss+2=-4,后面的连亏交易就不会增加加仓量了。求金老师及各位专家老师帮助修改正确之,谢谢!
-----------------------------
详细代码如下:
runmode:0; //以0逐K线模式运行(1系列模式)
//声明参数
input:ss(2,1,10,1); //底仓交易手数
//声明变量
globalvariable:kcs=0; //连亏次数
globalvariable:jyss=0; //交易手数(底仓+加仓)
////一、平空
IF (平空条件) and tholding<0 then begin
tsellshort(1,0,mkt);
END
if tnumprofit(1)>=0 or time=closetime(0) or time=opentime(1) then kcs:=0;
if tnumprofit(1)<0 then kcs:=kcs+1;
//连亏交易手数
if kcs=0 then jyss:=ss;
if kcs=1 then jyss:=ss+1;
if kcs=2 then jyss:=ss+2;
if kcs=3 then jyss:=ss+3;
if kcs=4 then jyss:=ss+4;
if kcs=5 then jyss:=ss+5;
if kcs>=6 then jyss:=ss+6;
////开多加仓
IF (开多条件) and tholding=0 then begin
tbuy(jyss>0,jyss,mkt);
kcs:=0;
jyss:=0;
END
////二、平多
IF(平多条件)and tholding>0 then begin
tsell(1,0,mkt); //全平多,若有
END
if tnumprofit(1)>=0 or time=closetime(0) or time=opentime(1) then kcs:=0;
if tnumprofit(1)<0 then kcs:=kcs+1;
//连亏交易手数
if kcs=0 then jyss:=ss;
if kcs=1 then jyss:=ss+1;
if kcs=2 then jyss:=ss+2;
if kcs=3 then jyss:=ss+3;
if kcs=4 then jyss:=ss+4;
if kcs=5 then jyss:=ss+5;
if kcs>=6 then jyss:=ss+6;
//开空加仓
IF(开空条件)and tholding=0 then begin
tbuyshort(jyss>0,jyss,mkt);
kcs:=0;
jyss:=0;
END