代码问题,求教 [MC]
-
MC用户求助:
只是想实现 止损后,反手交易,并且手数翻倍。
所以手数应该是1 2 4 8 16 32 这样倍增。
但现在总是出现跳跃,不知道是为什么。
不用一定在哪个品种上测试,小品种上都可以测试,螺纹,大豆,豆粕等等都行。
例如下图1 2 4 然后没有8 直接跳到了16 。
32后 直接跳到了128
是和模式(bar内?)有关?还是代码?
全部代码如下:
inputs:
Zhiyingx( 9999) ,
Zhiying( 9999) ,
Zhisun( 5 ) ;
variables:
nn(4) ,
ww(3) ,
var0( 0 ), var1( 0 ), var2( 0 ),Length( 20 ),Zhiyings( 20 ),TNUMSEQLOSS( 0 ),
shoushua( 1 ), shoushu(1), yici(0)
;
yici=0;
var0 = BollingerBand( Close, Length, 1 ) ;
var1 = BollingerBand( Close , Length, -1 ) ;
var2 = Average(C,Length);
condition3 = TIME>900 and TIME<2300;
condition1 = c[1]>o[1] and c[1]>Average(c[1],26) and condition3 ;;
condition2 = c[1]<o[1] and c[1]<Average(c[1],26) and condition3 ;;
if TNUMSEQLOSS<ww then begin
Zhiyings=Zhiying;
end
Else begin
Zhiyings=Zhiyingx;
end;
if TNUMSEQLOSS<nn then begin
shoushua=shoushu;
end
Else begin
shoushu=0.5;
end;
condition4 = CurrentBar > 1 and H>Zhisun+avgentryprice and marketposition=-1 and condition3 ;
condition5 = CurrentBar > 1 and l<avgentryprice-Zhisun and marketposition=1 and condition3 ;
condition6 = CurrentBar > 1 and l<avgentryprice-Zhiyings and marketposition=-1 and condition3 ;
condition7 = CurrentBar > 1 and h>avgentryprice+Zhiyings and marketposition=1 and condition3 ;
if (condition1 or yici[1]=1) and marketposition=0 then begin
Buy ( "kai1" ) 1 Contract next bar at Open stop ;
end;
if (condition2 or yici[1]=-1) and marketposition=0 then begin
Sell Short ( "kai2" ) 1 Contract next bar at Open stop ;
end;
if condition4 and condition5=False then begin
TNUMSEQLOSS=TNUMSEQLOSS+1;
print(date," ",time," buy TNUMSEQLOSS: ",TNUMSEQLOSS);
shoushu=Power(2,TNUMSEQLOSS);
print(date," ",time," buy shoushu: ",shoushu);
buy ( "kui1" ) shoushu Contracts next bar at Zhisun+avgentryprice stop ;
end;
if condition5 and condition4=False then begin
TNUMSEQLOSS=TNUMSEQLOSS+1;
print(date," ",time," sellshort TNUMSEQLOSS: ",TNUMSEQLOSS);
shoushu=Power(2,TNUMSEQLOSS);
print(date," ",time," sellshort shoushu: ",shoushu);
sellshort( "kui2" ) shoushu Contracts next bar at avgentryprice-Zhisun stop ;
end;
if condition6 then begin
buytocover ( "ying1" ) next bar at avgentryprice-Zhiyings stop ;
shoushu=1;
TNUMSEQLOSS=0;
yici=-1;
end;
if condition7 then begin
sell( "ying2" ) next bar at avgentryprice+Zhiyings stop ;
shoushu=1;
TNUMSEQLOSS=0;
yici=1;
end;
-
MC回复讨论一:
if condition4 and condition5=False then begin
TNUMSEQLOSS=TNUMSEQLOSS+1;
print(date," ",time," buy TNUMSEQLOSS: ",TNUMSEQLOSS);
shoushu=Power(2,TNUMSEQLOSS);
print(date," ",time," buy shoushu: ",shoushu);
buy ( "kui1" ) shoushu Contracts next bar at Zhisun+avgentryprice stop ;
end;
if condition5 and condition4=False then begin
TNUMSEQLOSS=TNUMSEQLOSS+1;
print(date," ",time," sellshort TNUMSEQLOSS: ",TNUMSEQLOSS);
shoushu=Power(2,TNUMSEQLOSS);
print(date," ",time," sellshort shoushu: ",shoushu);
sellshort( "kui2" ) shoushu Contracts next bar at avgentryprice-Zhisun stop ;
end;
以上代码是您累加变量TNUMSEQLOSS的地方,也是问题的所在,之所以出现”跳跃“是因为重复累加,也就是条件连续满足导致连续累加;因为您使用的是条件单,而条件单不一定委托出去就会成交,但是您在条件成立时肯定会累加变量TNUMSEQLOSS,所以导致两者的不一致;解决方法是,确定条件单成交之后再累加变量TNUMSEQLOSS
-
MC回复讨论二:
判断是否成交的方法有很多:
第一、在没有加仓的情况下,通过当根bar的marketposition与前一根bar的marketposition的信息对比进行判断,但是由于marketposition没有回溯的功能,但您需要先将marketposition的值赋值给新建的变量mp,然后通过判断mp[1]<>mp
第二、在有加仓的情况下,您还需要考虑关键字currentcontracts等持仓部位信息的变化,以此来判断。
第三、策略属性中有重新计算的设置,可以勾选“委托单成交”,这个用来判断委托单成交有点复杂,不建议使用。
-
MC回复讨论三:
inputs:
Zhiyingx( 9999) ,
Zhiying( 9999) ,
Zhisun( 5 ) ;
variables:
nn(4) ,
ww(3) ,
var0( 0 ), var1( 0 ), var2( 0 ),Length( 20 ),Zhiyings( 20 ),TNUMSEQLOSS( 0 ),
shoushua( 1 ), shoushu(1), yici(0), mp(0);;
yici=0;
var0 = BollingerBand( Close, Length, 1 ) ;
var1 = BollingerBand( Close , Length, -1 ) ;
var2 = Average(C,Length);
condition3 = TIME>900 and TIME<2300;
condition1 = c[1]>o[1] and c[1]>Average(c[1],26) and condition3 ;;
condition2 = c[1]<o[1] and c[1]<Average(c[1],26) and condition3 ;;
if TNUMSEQLOSS<ww then begin
Zhiyings=Zhiying;
end
Else begin
Zhiyings=Zhiyingx;
end;
if TNUMSEQLOSS<nn then begin
shoushua=shoushu;
end
Else begin
shoushu=0.5;
end;
mp=marketposition;
condition4 = CurrentBar > 1 and H>Zhisun+avgentryprice and marketposition=-1 and condition3 and mp[1]<>mp;
condition5 = CurrentBar > 1 and l<avgentryprice-Zhisun and marketposition=1 and condition3 and mp[1]<>mp;
condition6 = CurrentBar > 1 and l<avgentryprice-Zhiyings and marketposition=-1 and condition3 ;
condition7 = CurrentBar > 1 and h>avgentryprice+Zhiyings and marketposition=1 and condition3 ;
if (condition1 or yici[1]=1) and marketposition=0 then begin
Buy ( "kai1" ) 1 Contract next bar at Open stop ;
end;
if (condition2 or yici[1]=-1) and marketposition=0 then begin
Sell Short ( "kai2" ) 1 Contract next bar at Open stop ;
end;
if condition4 and condition5=False then begin
TNUMSEQLOSS=TNUMSEQLOSS+1;
print(date," ",time," buy TNUMSEQLOSS: ",TNUMSEQLOSS);
shoushu=Power(2,TNUMSEQLOSS);
print(date," ",time," buy shoushu: ",shoushu);
buy ( "kui1" ) shoushu Contracts next bar at Zhisun+avgentryprice stop ;
end;
if condition5 and condition4=False then begin
TNUMSEQLOSS=TNUMSEQLOSS+1;
print(date," ",time," sellshort TNUMSEQLOSS: ",TNUMSEQLOSS);
shoushu=Power(2,TNUMSEQLOSS);
print(date," ",time," sellshort shoushu: ",shoushu);
sellshort( "kui2" ) shoushu Contracts next bar at avgentryprice-Zhisun stop ;
end;
if condition6 then begin
buytocover ( "ying1" ) next bar at avgentryprice-Zhiyings stop ;
shoushu=1;
TNUMSEQLOSS=0;
yici=-1;
end;
if condition7 then begin
sell( "ying2" ) next bar at avgentryprice+Zhiyings stop ;
shoushu=1;
TNUMSEQLOSS=0;
yici=1;
end;
我按照您说的改了,这样基本交易1,2次就不交易了,这是怎么回事?
-
MC回复讨论四:
inputs:
Zhiyingx( 9999) ,
Zhiying( 9999) ,
Zhisun( 5 ) ;
variables:
nn(4) ,
ww(3) ,
var0( 0 ), var1( 0 ), var2( 0 ),Length( 20 ),Zhiyings( 20 ),TNUMSEQLOSS( 0 ),
shoushua( 1 ), shoushu(1), yici(0), mp(0);;
yici=0;
var0 = BollingerBand( Close, Length, 1 ) ;
var1 = BollingerBand( Close , Length, -1 ) ;
var2 = Average(C,Length);
condition3 = TIME>900 and TIME<2300;
condition1 = c[1]>o[1] and c[1]>Average(c[1],26) and condition3 ;;
condition2 = c[1]<o[1] and c[1]<Average(c[1],26) and condition3 ;;
if TNUMSEQLOSS<ww then begin
Zhiyings=Zhiying;
end
Else begin
Zhiyings=Zhiyingx;
end;
if TNUMSEQLOSS<nn then begin
shoushua=shoushu;
end
Else begin
shoushu=0.5;
end;
mp=marketposition;
condition4 = CurrentBar > 1 and H>Zhisun+avgentryprice and marketposition=-1 and condition3 and mp[1]<>mp;
condition5 = CurrentBar > 1 and l<avgentryprice-Zhisun and marketposition=1 and condition3 and mp[1]<>mp;
condition6 = CurrentBar > 1 and l<avgentryprice-Zhiyings and marketposition=-1 and condition3 ;
condition7 = CurrentBar > 1 and h>avgentryprice+Zhiyings and marketposition=1 and condition3 ;
if (condition1 or yici[1]=1) and marketposition=0 then begin
Buy ( "kai1" ) 1 Contract next bar at Open stop ;
end;
if (condition2 or yici[1]=-1) and marketposition=0 then begin
Sell Short ( "kai2" ) 1 Contract next bar at Open stop ;
end;
if condition4 and condition5=False then begin
TNUMSEQLOSS=TNUMSEQLOSS+1;
print(date," ",time," buy TNUMSEQLOSS: ",TNUMSEQLOSS);
shoushu=Power(2,TNUMSEQLOSS);
print(date," ",time," buy shoushu: ",shoushu);
buy ( "kui1" ) shoushu Contracts next bar at Zhisun+avgentryprice stop ;
end;
if condition5 and condition4=False then begin
TNUMSEQLOSS=TNUMSEQLOSS+1;
print(date," ",time," sellshort TNUMSEQLOSS: ",TNUMSEQLOSS);
shoushu=Power(2,TNUMSEQLOSS);
print(date," ",time," sellshort shoushu: ",shoushu);
sellshort( "kui2" ) shoushu Contracts next bar at avgentryprice-Zhisun stop ;
end;
if condition6 then begin
buytocover ( "ying1" ) next bar at avgentryprice-Zhiyings stop ;
shoushu=1;
TNUMSEQLOSS=0;
yici=-1;
end;
if condition7 then begin
sell( "ying2" ) next bar at avgentryprice+Zhiyings stop ;
shoushu=1;
TNUMSEQLOSS=0;
yici=1;
end;
我按照您说的改了,这样基本交易1,2次就不交易了,这是怎么回事?
有思路,想编写各种指标公式,程序化交易模型,选股公式,预警公式的朋友
可联系技术人员 QQ: 511411198 进行 有偿 编写!(不贵!点击查看价格!)
相关文章
-
没有相关内容