有两点疑问:1:VARIABLE : T20HI=CLOSE ;variable是否不应该用,是否错误?
2:variable全局变量不是保存前值吗,为什么还用EXTGBDATASET函数?
以下代码来自于金字塔应用程序自带策略码
WARNING_DISABLE:4;
//声明参数
INPUT : T20(20,15,60,1) ; //进场的周期
INPUT : T10(10,10,30,1); //出场的周期
INPUT : ATRLEN(20,15,30,1) ;
INPUT : POSNUM(2,1,20,1) ; //每次交易的手数
//声明变量
BUYORDERTHISBAR := 0 ; //当前BAR有过交易
VARIABLE : _DEBUG = 1 ; //是否输出前台交易指令
VARIABLE : _TDEBUG = 1 ; //是否输出后台交易指令
VARIABLE : _DEBUGOUT = 1 ; //是否输出后台交易的调试信息
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) ;
//采用全局变量保存最后一根K线的计算状态
STRENTRYBARPOS:=STRCAT(STKLABEL,'ENTRYBARPOS') ;
STREXITBARPOS:=STRCAT(STKLABEL,'EXITBARPOS') ;
STRPREENTRYPRICE:=STRCAT(STKLABEL,'PREENTRYPRICE') ;
STRTURTLEUNITS:=STRCAT(STKLABEL,'TURTLEUNITS') ;
STRPOSITION:=STRCAT(STKLABEL,'POSITION') ;
STRPREN:=STRCAT(STKLABEL,'PREN') ;
IF NOT ( WORKMODE=1 ) THEN BEGIN
DRAWTEXTEX(1 ,0 ,0 ,0 ,'提示:本公式仅用于后台交易!' ),COLORYELLOW ;
EXIT ;
END //IF
//开始执行时 初始化数据
//注意:第一个数据的BARPOS=1
IF BARPOS=1 THEN BEGIN
//POSITION := 0 ;
END //IF
//如果当前棒是最后一根K线,执行
IF ISLASTBAR THEN BEGIN
// 如果最后一根K线发生过出场信号,则那一根K线不再交易
IF EXTGBDATA(STREXITBARPOS) = BARPOS THEN BEGIN
GOTO CONTINUELINE ;
END
//恢复上一秒计算时保存的数据
//如果记录的进场BARPOS和当前的相等,说明上一个进场信号也是最后一根K线发出的。11:40
IF EXTGBDATA(STRENTRYBARPOS) = BARPOS THEN BEGIN
MYENTRYPRICE := EXTGBDATA(STRPREENTRYPRICE) ;
TURTLEUNITS := EXTGBDATA(STRTURTLEUNITS) ;
POSITION := EXTGBDATA(STRPOSITION) ;
N := EXTGBDATA(STRPREN) ;
END
//如果当前是没有持仓的状态
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 ) ;
POSITION := 1 ;
TURTLEUNITS := 1 ;
N := AVGTR ;
TBUY( _TDEBUG,POSNUM,LMT,H),ALLOWREPEAT ;
EXTGBDATASET(STRENTRYBARPOS,BARPOS ) ;
EXTGBDATASET(STRPREENTRYPRICE,MYENTRYPRICE ) ;
EXTGBDATASET(STRTURTLEUNITS,TURTLEUNITS ) ;
EXTGBDATASET(STRPOSITION,POSITION ) ;
EXTGBDATASET(STRPREN,N ) ;
END //IF多头进场符合
//建立空头进场条件
SHORT := L < T20LO ;
//空头进场符合
IF SHORT AND POSITION=0 THEN BEGIN
MYENTRYPRICE := IF(OPEN<T20LO-MINDIFF ,OPEN ,T20LO-MINDIFF ) ;
POSITION := -1 ;
TURTLEUNITS := 1 ;
N := AVGTR ;
TBUYSHORT( _TDEBUG,POSNUM,LMT,L),ALLOWREPEAT;
EXTGBDATASET(STRENTRYBARPOS,BARPOS ) ;
EXTGBDATASET(STRPREENTRYPRICE,MYENTRYPRICE ) ;
EXTGBDATASET(STRTURTLEUNITS,TURTLEUNITS ) ;
EXTGBDATASET(STRPOSITION,POSITION ) ;
EXTGBDATASET(STRPREN,N ) ;
END //IF空头进场符合
GOTO CONTINUELINE ;
END //IF如果当前是没有持仓的状态
//如果当前持有多头仓位的状态
IF POSITION=1 AND BARPOS>T20 AND H>L THEN BEGIN
//多头加仓条件
IF (HIGH>MYENTRYPRICE+0.5*N) AND TURTLEUNITS<4 THEN BEGIN
MYENTRYPRICE := IF(OPEN>MYENTRYPRICE+0.5*N ,OPEN ,MYENTRYPRICE+0.5*N ) ;
MYENTRYPRICE := CEILING(MYENTRYPRICE/MINDIFF)*MINDIFF ;
TURTLEUNITS := TURTLEUNITS+1 ;
TBUY( _TDEBUG,POSNUM,LMT,H),ALLOWREPEAT ;
EXTGBDATASET(STRENTRYBARPOS,BARPOS ) ;
EXTGBDATASET(STRPREENTRYPRICE,MYENTRYPRICE ) ;
EXTGBDATASET(STRTURTLEUNITS,TURTLEUNITS ) ;
EXTGBDATASET(STRPOSITION,POSITION ) ;
END //IF多头加仓条件
//建立多头离场条件
LONGX1 := (LOW < T10LO) ;
IF LONGX1 AND EXTGBDATA(STRENTRYBARPOS)<>BARPOS AND EXTGBDATA(STREXITBARPOS)<>BARPOS THEN BEGIN
MYEXITPRICE := IF(OPEN<T10LO-MINDIFF ,OPEN ,T10LO-MINDIFF ) ;
POSITION := 0 ;
TURTLEUNITS := 0 ;
TSELL( _TDEBUG ,0,LMT,L),ALLOWREPEAT;
EXTGBDATASET(STREXITBARPOS,BARPOS ) ;
EXTGBDATASET(STRTURTLEUNITS,TURTLEUNITS ) ;
EXTGBDATASET(STRPOSITION,POSITION ) ;
END
//建立多头止损条件
LONGX2 := (LOW<MYENTRYPRICE-2*N) ;
IF LONGX2 AND POSITION=1 AND EXTGBDATA(STRENTRYBARPOS)<>BARPOS AND EXTGBDATA(STREXITBARPOS)<>BARPOS THEN BEGIN
MYEXITPRICE := IF(OPEN<MYENTRYPRICE-2*N ,OPEN ,MYENTRYPRICE-2*N ) ;
MYEXITPRICE := FLOOR(MYEXITPRICE/MINDIFF)*MINDIFF ;
POSITION := 0 ;
TURTLEUNITS := 0 ;
TSELL( _TDEBUG ,0,LMT,L),ALLOWREPEAT;
EXTGBDATASET(STREXITBARPOS,BARPOS ) ;
EXTGBDATASET(STRTURTLEUNITS,TURTLEUNITS ) ;
EXTGBDATASET(STRPOSITION,POSITION ) ;
END
GOTO CONTINUELINE ;
END //IF如果当前持有多头仓位的状态
//如果当前持有空头仓位的状态
IF POSITION = -1 AND BARPOS>T20 AND H>L THEN BEGIN
//空头加仓条件
IF (LOW<MYENTRYPRICE-0.5*N) AND TURTLEUNITS<4 THEN BEGIN
MYENTRYPRICE := IF(OPEN<MYENTRYPRICE-0.5*N ,OPEN ,MYENTRYPRICE-0.5*N ) ;
MYENTRYPRICE := FLOOR(MYENTRYPRICE/MINDIFF)*MINDIFF ;
TURTLEUNITS := TURTLEUNITS+1 ;
TBUYSHORT( _TDEBUG,POSNUM,LMT,L),ALLOWREPEAT;
EXTGBDATASET(STRENTRYBARPOS,BARPOS ) ;
EXTGBDATASET(STRPREENTRYPRICE,MYENTRYPRICE ) ;
EXTGBDATASET(STRTURTLEUNITS,TURTLEUNITS ) ;
EXTGBDATASET(STRPOSITION,POSITION ) ;
END //IF空头加仓条件
//建立空头离场条件
SHORTX1 := H > T10HI ;
IF SHORTX1 AND EXTGBDATA(STRENTRYBARPOS)<>BARPOS AND EXTGBDATA(STREXITBARPOS)<>BARPOS THEN BEGIN
MYEXITPRICE := IF(OPEN>T10HI+MINDIFF ,OPEN ,T10HI+MINDIFF ) ;
POSITION := 0 ;
TURTLEUNITS := 0 ;
TSELLSHORT( _TDEBUG,0,LMT,H),ALLOWREPEAT;
EXTGBDATASET(STREXITBARPOS,BARPOS ) ;
EXTGBDATASET(STRTURTLEUNITS,TURTLEUNITS ) ;
EXTGBDATASET(STRPOSITION,POSITION ) ;
END
//建立空头止损条件
SHORTX2 := HIGH > MYENTRYPRICE + 2*N ;
IF SHORTX2 AND POSITION = -1 AND EXTGBDATA(STRENTRYBARPOS)<>BARPOS AND EXTGBDATA(STREXITBARPOS)<>BARPOS THEN BEGIN
MYEXITPRICE := IF(OPEN>MYENTRYPRICE+2*N ,OPEN ,MYENTRYPRICE+2*N ) ;
MYEXITPRICE := CEILING(MYEXITPRICE/MINDIFF)*MINDIFF ;
POSITION := 0 ;
TURTLEUNITS := 0 ;
TSELLSHORT( _TDEBUG,0,LMT,H),ALLOWREPEAT;
EXTGBDATASET(STREXITBARPOS,BARPOS ) ;
EXTGBDATASET(STRTURTLEUNITS,TURTLEUNITS ) ;
EXTGBDATASET(STRPOSITION,POSITION ) ;
END
GOTO CONTINUELINE ;
END //IF如果当前持有空头仓位的状态
//如果以上3种情形都没有成立,则直接结束本次判断
GOTO CONTINUELINE ;
END //IF如果当前棒是最后一根K线
//////////////不是最后一根K线的情形
//如果当前是没有持仓的状态
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+MINDIFF);
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-MINDIFF);
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-MINDIFF);
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+MINDIFF);
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@ 资产:TASSET,LINETHICK0;
//可用现金:CASH(0),LINETHICK0;
POS:THOLDING,LINETHICK0;
//交易次数:TOTALDAYTRADE, LINETHICK0 ;
//EP:MYENTRYPRICE ;
// DEBUGOUT('POSITION=%.0F' ,POSITION ) ;
// DEBUGOUT('TURTLEUNITS=%.0F' ,TURTLEUNITS ) ;
// DEBUGOUT('BARPOS=%.0F' ,BARPOS ) ;
// DEBUGOUT('MYENTRYPRICE=%.0F' ,MYENTRYPRICE ) ;
IF _DEBUGOUT>0 AND ISLASTBAR THEN BEGIN
DEBUGFILE2('C:\DEBUGFILE.TXT','BARPOS=%.0F' ,BARPOS,1) ;
DEBUGFILE2('C:\DEBUGFILE.TXT','T20HI=%.2F' ,T20HI ,1) ;
DEBUGFILE2('C:\DEBUGFILE.TXT','N=%.2F' ,N ,1) ;
DEBUGFILE2('C:\DEBUGFILE.TXT','AVGTR=%.2F' ,AVGTR ,1) ;
DEBUGFILE2('C:\DEBUGFILE.TXT','POSITION=%.0F' ,POSITION,1 ) ;
DEBUGFILE2('C:\DEBUGFILE.TXT','TURTLEUNITS=%.0F' ,TURTLEUNITS,1 ) ;
DEBUGFILE2('C:\DEBUGFILE.TXT','OPEN=%.2F' ,O ,1) ;
DEBUGFILE2('C:\DEBUGFILE.TXT','HIGH=%.2F' ,H ,1) ;
DEBUGFILE2('C:\DEBUGFILE.TXT','LOW=%.2F' ,L ,1) ;
DEBUGFILE2('C:\DEBUGFILE.TXT','CLOSE=%.2F' ,C ,1) ;
DEBUGFILE2('C:\DEBUGFILE.TXT','MYENTRYPRICE=%.0F' ,MYENTRYPRICE ,1) ;
END //IF