连续n笔以上(n大于等于2笔)都是亏损,怎么得到第一笔亏损交易,开仓至今的周期数(即K线根数)?
[此贴子已经被作者于2015/5/10 22:08:55编辑过]
variable:n=0;
if 平多条件 and holding>0 then begin
sell(1,0,market);
if numprofit(1)>0 then n:=0;
if numprofit(1)<0 then n:=n+1;
end
if 平空条件 and holding>0 then begin
sellshort(1,0,market);
if numprofit(1)>0 then n:=0;
if numprofit(1)<0 then n:=n+1;
end
全局变量n来计数当前的连续亏损次数
那么需求第一次亏损且当前连亏大于2次到现在的周期就是:
nn:=barslast(ref(n=0,1) and n=1);
if n>2 then xx:=nn;
xx就是所求周期数
这个nn是第一次亏损平仓以来的周期数吧,我想得到是开仓以来的周期数。
上面的公式不能用于平仓即反手,用于反手的策略上,该怎么改写?
我的代码如下,是平仓即反手,但加入了止损,按下面的写,记录不了连亏次数。
variable:cishu=0;//记录连亏次数
if PK then begin
平空:SELLSHORT(1,SS,THISCLOSE); //平空信号
if NUMPROFIT(1)>0 then cishu:=0;
if NUMPROFIT(1)<0 then cishu:=cishu+1;
end
if KD and holding=0 then begin
开多:BUY(1,SS,THISCLOSE);
//开多信号
end
if PD then begin
平多:SELL(1,SS,THISCLOSE); //平多信号
if NUMPROFIT(1)>0 then cishu:=0;
if NUMPROFIT(1)<0 then cishu:=cishu+1;
end
if KK and holding=0 then begin
开空:BUYSHORT(1,SS,THISCLOSE);
//开空信号
end
//多头止损
IF ENTERBARS>0 AND HOLDING>0 THEN BEGIN
IF C<stoploss THEN BEGIN
多头止损:SELL(1,HOLDING,THISCLOSE);
if NUMPROFIT(1)<0 then cishu:=cishu+1;
END
END
//空头止损
IF ENTERBARS>0 AND HOLDING<0 THEN BEGIN
IF C>stoploss THEN BEGIN
空头止损:SELLshort(1,HOLDING,THISCLOSE);
if NUMPROFIT(1)<0 then cishu:=cishu+1;
END
END
if PK and holding<0 then begin
pk里面不要加持仓判断,持仓判断写直接写在if判断里面