-- 作者:jiangsen
-- 发布时间:2012/9/14 20:05:35
-- 收盘前平仓
//声明参数 Input : T20(20,15,60,1) ; Input : T10(10,10,30,1); Input : ATRLen(20,15,30,1) ; Input : PosNum(1,1,20,1) ;
//声明变量 nt := 1 ; //调试信息带时间戳 BuyOrderThisBar := 0 ; //当前Bar有过交易
VARIABLE : _DEBUG = 1 ; //是否输出前台交易指令 VARIABLE : _TDEBUG = 1 ; //是否输出后台交易指令 VARIABLE : _DEBUGOUT = 0 ; //是否输出后台交易的调试信息
VARIABLE : myEntryPrice =0 ; //开仓价格 VARIABLE : myExitPrice =0 ; //平仓价格
VARIABLE : TurtleUnits=0 ; //交易单位 VARIABLE : Position=0 ; //仓位状态 //0表示没有仓位,1表示持有多头, -1表示持有空头
VARIABLE : T20Hi=Close ; //20周期的高点 VARIABLE : T20Lo=Close ; //20周期的低点
VARIABLE : T10Hi=Close ; //10周期的高点 VARIABLE : T10Lo=Close ; //10周期的低点
//准备需要计算的变量 T20Hi := ref(hhv(h,T20),1) ; T20Lo := ref(llv(l,T20),1) ;
T10Hi := ref(hhv(h,T10),1) ; T10Lo := ref(llv(l,T10),1) ;
AvgTR := ref(MA(TR,ATRLen),1) ;
//开始执行时 初始化数据 If BARPOS=1 Then Begin //Position := 0 ;
End //If
//如果当前是没有持仓的状态 If Position=0 and BARPOS>T20 and h>l Then Begin
//建立多头进场条件 Long := h > T20Hi ; //多头进场 if Long then begin myEntryPrice := IF(Open>T20Hi+MINDIFF ,Open ,T20Hi+MINDIFF ) ; buy( _DEBUG,PosNum,limitr,myEntryPrice); Position := 1 ; TurtleUnits := 1 ; N := AvgTR ; BuyOrderThisBar := 1;
end //if
//建立空头进场条件 Short := l < T20Lo ; //空头进场 if Short and Position=0 then begin myEntryPrice := IF(Open<T20Lo-MINDIFF ,Open ,T20Lo-MINDIFF ) ; buyshort( _DEBUG,PosNum,limitr,myEntryPrice); Position := -1 ; TurtleUnits := 1 ; N := AvgTR ; BuyOrderThisBar := 1;
end //不要跳转,让程序检查同一根K线是否可以加仓 //Goto ContinueLine ; End //If
//如果当前持有多头仓位的状态
If Position=1 and BARPOS>T20 and h>l Then Begin
//多头加仓条件 While (High>myEntryPrice+0.5*N) and TurtleUnits<4 Do Begin myEntryPrice := IF(Open>myEntryPrice+0.5*N ,Open ,myEntryPrice+0.5*N ) ; myEntryPrice := Ceiling(myEntryPrice/MINDIFF)*MINDIFF ; buy( _DEBUG, PosNum, limitr, myEntryPrice); TurtleUnits := TurtleUnits+1 ; BuyOrderThisBar := 1;
End //While //建立多头离场条件 LongX1 := (low < T10Lo) ; if LongX1 and BuyOrderThisBar=0 then begin myExitPrice := IF(Open<T10Lo-MINDIFF ,Open ,T10Lo-MINDIFF ) ; sell( _DEBUG ,0,limitr,myExitPrice); Position := 0 ; TurtleUnits := 0 ; end
//建立多头止损条件 LongX2 := (Low<myEntryPrice-2*N) ;
if LongX2 and Position=1 and BuyOrderThisBar=0 then begin myExitPrice := IF(Open<myEntryPrice-2*N ,Open ,myEntryPrice-2*N ) ; myExitPrice := Floor(myExitPrice/MINDIFF)*MINDIFF ; sell( _DEBUG ,0,limitr,myExitPrice); Position := 0 ; TurtleUnits := 0 ; end
Goto ContinueLine ;
End //If
//如果当前持有空头仓位的状态
If Position = -1 and BARPOS>T20 and h>l Then Begin
//空头加仓条件 While (Low<myEntryPrice-0.5*N) and TurtleUnits<4 Do Begin myEntryPrice := IF(Open<myEntryPrice-0.5*N ,Open ,myEntryPrice-0.5*N ) ; myEntryPrice := Floor(myEntryPrice/MINDIFF)*MINDIFF ; buyshort( _DEBUG,PosNum, limitr, myEntryPrice); TurtleUnits := TurtleUnits+1 ; BuyOrderThisBar := 1; End //If
//建立空头离场条件 ShortX1 := h > T10Hi ;
if ShortX1 and BuyOrderThisBar=0 then begin myExitPrice := IF(Open>T10Hi+MINDIFF ,Open ,T10Hi+MINDIFF ) ; sellshort( _DEBUG,0,limitr,myExitPrice); Position := 0 ; TurtleUnits := 0 ; end
//建立空头止损条件 ShortX2 := High > myEntryPrice + 2*N ;
if ShortX2 and Position = -1 and BuyOrderThisBar=0 then begin myExitPrice := IF(Open>myEntryPrice+2*N ,Open ,myEntryPrice+2*N ) ; myExitPrice := Ceiling(myExitPrice/MINDIFF)*MINDIFF ; sellshort( _DEBUG,0,limitr,myExitPrice); Position := 0 ; TurtleUnits := 0 ; end
End //If
//显示账户状态 ContinueLine@ 资产:ASSET,LINETHICK0; 可用现金:CASH(0),LINETHICK0; Pos:HOLDING,LINETHICK0; 交易次数:TOTALDAYTRADE, LINETHICK0 ;
If _DEBUGOUT>0 Then Begin
DEBUGFILE2(\'c:\\debugfile.txt\',\'BarPos=%.0f\' ,BARPOS,nt ) ; DEBUGFILE2(\'c:\\debugfile.txt\',\'T20Hi=%.2f\' ,T20Hi ,nt) ; DEBUGFILE2(\'c:\\debugfile.txt\',\'N=%.2f\' ,N ,nt) ; DEBUGFILE2(\'c:\\debugfile.txt\',\'Close=%.2f\' ,C ,nt) ; DEBUGFILE2(\'c:\\debugfile.txt\',\'Position=%.0f\' ,Position,nt ) ; DEBUGFILE2(\'c:\\debugfile.txt\',\'TurtleUnits=%.0f\' ,TurtleUnits,nt ) ; DEBUGFILE2(\'c:\\debugfile.txt\',\'myEntryPrice=%.0f\' ,myEntryPrice ,nt) ; DEBUGFILE2(\'c:\\debugfile.txt\',\'myExitPrice=%.0f\' ,myExitPrice ,nt) ; End //If
想让这个系统每天收盘前平仓,该在什么地方添加什么语句呢?
|