以文本方式查看主题 - 金字塔客服中心 - 专业程序化交易软件提供商 (http://weistock.com/bbs/index.asp) -- 公式模型编写问题提交 (http://weistock.com/bbs/list.asp?boardid=4) ---- 亏三次加仓,盈三次减仓软件难实现吗? (http://weistock.com/bbs/dispbbs.asp?boardid=4&id=140873) |
-- 作者:tanyongde -- 发布时间:2016/10/13 14:12:05 -- 亏三次加仓,盈三次减仓软件难实现吗? 下面是突破20日高低点策略,想实现连续盈三次后减仓,直到连续亏三次后恢复初始仓位,老师修改二次也不行,老样回测仓位错乱,软件难实现还是写法有错? input :k(20,5,30,1) input:avglen(30,10,100,5); input : atrlen(20,5,30,1) ; input : ss(2,1,1000,1) ; //声明变量 nt := 1 ; //调试信息带时间戳 buyorderthisbar := 0 ; //当前bar有过交易 variable : _debug = 1 ; //是否输出前台交易指令 variable : turtleunits=0 ; //交易单位 variable : myentryprice1=0; variable : myentryprice2=0; variable : myexitprice=0; variable : n=0; variable : m=0; variable : yk=0; //准备需要计算的变量 zhh:ref(high,k); zll:ref(low,k); kqls:=enterbars+1; khh:=hhv(high,kqls); kll:=llv(low,kqls); avgtr :=ref(ma(tr,atrlen),1); //开始执行时 初始化数据 if barpos=1 then begin position :=0 ; posnum:=ss; end //如果当前是没有持仓的状态 if position=0 and barpos>20 and h>l then begin //建立多头进场条件 long:=high>=zhh ; //多头进场 if long and holding=0 then begin myentryprice1 :=if(open>zhh+0.2 ,open+0.6 ,zhh+0.6); buy( _debug,posnum,limitr,myentryprice1); jqsh:=1; position := 1 ; turtleunits := 1 ; // n:= avgtr ; buyorderthisbar := 1; end //建立空头进场条件 short:=low<=zll ; //空头进场 if short and position=0 and holding=0 then begin myentryprice2:= if(open<zll-0.2 ,open-0.6 ,zll-0.6) ; buyshort( _debug,posnum,limitr,myentryprice2); jqsh:=1; position := -1 ; turtleunits := 1 ; //n := avgtr ; buyorderthisbar := 1; end end //如果当前持有多头仓位的状态 if position=1 and barpos>20 and h>l then begin //多头加仓条件 {while (close>myentryprice+0.5*n) and turtleunits<4 and jqsh=1 do begin myentryprice := if(open>myentryprice+0.5*n ,open+0.6 ,myentryprice+0.5*n+0.6 ) ; myentryprice:= ceiling(myentryprice/mindiff)*mindiff ; buy( _debug, posnum, limitr,myentryprice ); turtleunits := turtleunits+1 ; buyorderthisbar := 1; end } //建立多头离场条件 longx1 := low<zll-mindiff; myexitprice:=zll-mindiff; if longx1 and buyorderthisbar=0 and holding>0 then begin myexitprice := if(open<myexitprice-0.2 ,open-0.6 ,myexitprice-0.6) ; sell( _debug ,0,limitr,myexitprice); position := 0 ; turtleunits := 0 ; yk:=myexitprice- myentryprice2; if yk>0 then begin n:=0; m:=m+1; end if yk<0 then begin n:=n+1; m:=0; end // if n>=3 then posnum:=ss; end // if m>=3 then posnum:=0.5*ss; //建立多头止损条件 longx2 := low<(khh-2*n); if longx2 and position=1 and buyorderthisbar=0 and holding>0 then begin myexitprice := if(open<khh-2*n ,open-0.6 ,khh-2*n-0.6) ; myexitprice := floor(myexitprice/mindiff)*mindiff ; sell( _debug ,0,limitr,myexitprice); position := 0 ; turtleunits := 0 ; yk:=myexitprice- myentryprice2; if yk>0 then begin n:=0; m:=m+1; end if yk<0 then begin n:=n+1; m:=0; end end end //如果当前持有空头仓位的状态 if position = -1 and barpos>20 and h>l then begin //空头加仓条件 {while (close<myentryprice-0.5*n) and turtleunits<4 and jqsh=1 do begin myentryprice := if(open<myentryprice-0.5*n ,open-0.6 ,myentryprice-0.5*n-0.6 ) ; kkj := floor(myentryprice/mindiff)*mindiff ; buyshort( _debug,posnum, limitr,myentryprice ); turtleunits := turtleunits+1 ; buyorderthisbar := 1; end } //建立空头离场条件 shortx1 := high>zhh+mindiff; myexitprice:=zhh+mindiff; if shortx1 and buyorderthisbar=0 and holding<0 then begin myexitprice := if(open>myexitprice,open+0.6, myexitprice+0.6); sellshort( _debug,0,limitr,myexitprice); position := 0 ; turtleunits := 0 ; yk:=myentryprice2-myexitprice; if yk>0 then begin n:=0; m:=m+1; end if yk<0 then begin n:=n+1; m:=0; end //if n>=3 then posnum:=ss; //if m>=3 then posnum:=0.5*ss; end //建立空头止损条件 shortx2 := high>kll+2*n ; if shortx2 and position = -1 and buyorderthisbar=0 and holding<0 then begin myexitprice:= if(open>kll+2*n ,open+0.6 ,kll+2*n+0.6) ; myexitprice := ceiling(myexitprice/mindiff)*mindiff ; sellshort( _debug,0,limitr,myexitprice); position := 0 ; turtleunits := 0 ; yk:=myentryprice2-myexitprice; if yk>0 then begin n:=0; m:=m+1; end if yk<0 then begin n:=n+1; m:=0; end // if n>=3 then posnum:=ss; //if m>=3 then posnum:=0.5*ss; end end if n>=3 and ref(n,1)=2 then posnum:=ss; if m>=3 and ref(m,1)=2 then posnum:=0.5*ss; |
-- 作者:jinzhe -- 发布时间:2016/10/13 15:29:34 -- 不要重复发帖,在之前那个帖子里面进行处理 |