软件自带代码,STRENTRYBARPOS:=STRCAT(STKLABEL,'ENTRYBARPOS'), ,ENTRYBARPOS就出现一次,也没有定义赋值,是不是有错误?
//该模型运行于后台程序化模式
//http://www.weistock.com/bbs/dispbbs.asp?boardid=10&Id=48570
//《定制的海龟交易系统V1.0后台版本》
// 适用于多时间框架图表
// 仅能工作于金字塔的后台
// 工作模式:轮询法,建议设置1秒,数据长度<300根
// 说明:
// 1、如果最后一根K线发生过进场或者加仓,则该K线不执行出场
// 2、如果最后一根K线发生过出场,则该K线不执行进场
// DESIGNED BY LIKAI
// 2010.07.20
//===========================================================
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
RUNMODE:0;
WARNING_DISABLE:4;
//设置一个等于空值的变量:
NUL:=BARSLAST(CLOSE<-99999999);
//15MIN均线:
REFM5:=STKINDI(STKLABEL,'MYMA.EMA5',0,3,-1);
M5:=(2*C+4*REFM5)/6,COLORRED;
REFM12:=STKINDI(STKLABEL,'MYMA.EMA12',0,3,-1);
M12:=(2*C+11*REFM12)/13,COLORYELLOW;
REFM20:=STKINDI(STKLABEL,'MYMA.EMA20',0,3,-1);
M20:=(2*C+19*REFM20)/21,COLORGREEN;
昨20.12.5:=(REF(M12,1)>REF(M5,1) AND REF(M20,1)>REF(M12,1));
昨12.20.5:=(REF(M20,1)>REF(M5,1) AND REF(M20,1)<REF(M12,1));
前12.20.5昨12.5.20:=(REF(M20,2)>REF(M5,2) AND REF(M20,2)<REF(M12,2) AND REF(M20,1)<REF(M5,1) AND REF(M20,1)<REF(M12,1));
昨空头排列:=昨20.12.5 OR 昨12.20.5 OR 前12.20.5昨12.5.20;
昨5.12.20:=(REF(M12,1)<REF(M5,1) AND REF(M20,1)<REF(M12,1));
昨12.5.20:=(REF(M20,1)<REF(M5,1) AND REF(M20,1)>REF(M12,1));
前5.20.12昨20.5.12:=(REF(M20,2)<REF(M5,2) AND REF(M20,2)>REF(M12,2) AND REF(M20,1)>REF(M5,1) AND REF(M20,1)>REF(M12,1));
昨多头排列:=昨5.12.20 OR 昨12.5.20 OR 前5.20.12昨20.5.12;
5叉12:= CROSS(M5,M12);
12叉5:= CROSS(M12,M5);
多头排列:=M5>M12 AND M12>M20 AND M5>REF(M5,1);
空头排列:=M5<M12 AND M12<M20 AND M5<REF(M5,1);
转上叉位:=(ROUND((66*REFM12-52*REFM5)/14/MINDIFF))*MINDIFF+MINDIFF;
转下叉位:=(ROUND((66*REFM12-52*REFM5)/14/MINDIFF))*MINDIFF-MINDIFF;
持:holding,nodraw;
品种名称:=STKNAME;
品种代码:=STKLABEL;
日期:=CURRENTDATE+1000000;
名日上叉时:=STRCAT(品种名称,STRCAT(日期,'C转上叉时间'));
名日上叉买价:=STRCAT(品种名称,STRCAT(日期,'C转上叉买入价'));//品种名称&日期&'C转上叉买入价';
名日下叉时:=STRCAT(品种名称,STRCAT(日期,'C转下叉时间'));//品种名称&日期&'C转下叉时间';
名日下叉卖价:=STRCAT(品种名称,STRCAT(日期,'C转下叉卖出价'));//品种名称&日期&'C转下叉卖出价';
C上穿转上叉位:=CROSS(CLOSE,转上叉位);
IF REFM5<REFM12 THEN BEGIN
IF C上穿转上叉位 THEN
BEGIN
EXTGBDATASET(名日上叉时,CURRENTTIME);
C转上叉时间:=EXTGBDATA(名日上叉时);
END
IF CURRENTTIME-C转上叉时间>60 OR TIME-CURRENTTIME<60 THEN
BEGIN
BUY(CLOSE>转上叉位,3,LIMITR,CLOSE);
EXTGBDATASET(名日上叉买价,CLOSE);
C转上叉买入价:EXTGBDATA(名日上叉买价),COLORRED;
END
END
C下穿转下叉位:=CROSS(转下叉位,CLOSE) ;
IF REFM5>REFM12 THEN BEGIN
IF C下穿转下叉位 THEN
BEGIN
EXTGBDATASET(名日下叉时,CURRENTTIME);
C转下叉时间:=EXTGBDATA(名日下叉时);
END
IF CURRENTTIME-C转下叉时间>60 OR TIME-CURRENTTIME<60 THEN
BEGIN
SELLSHORT(CLOSE<转下叉位,5,LIMITR,CLOSE);
EXTGBDATASET(名日下叉卖价,CLOSE);
C转下叉卖出价:EXTGBDATA(名日下叉卖价),COLORCYAN;
END
END
买开价:IF(HOLDING>0,C转上叉买入价,NUL),COLORRED;
卖开价:IF(HOLDING<0,C转下叉卖出价,NUL),COLORCYAN;
这个语句好像都没起作用呢?
EXTGBDATASET(名日上叉时,CURRENTTIME);
此主题相关图片如下:没起作用.png