以文本方式查看主题

-  金字塔客服中心 - 专业程序化交易软件提供商  (http://222.73.7.161/bbs/index.asp)
--  公式模型编写问题提交  (http://222.73.7.161/bbs/list.asp?boardid=4)
----  [求助]系统自带的《定制的海龟交易系统V1.0后台版本》问题  (http://222.73.7.161/bbs/dispbbs.asp?boardid=4&id=188431)

--  作者:agebar
--  发布时间:2021/5/23 1:50:25
--  [求助]系统自带的《定制的海龟交易系统V1.0后台版本》问题
//该模型为简单示范模型,用户需根据自己交易经验,修改完善后再实际应用!!!
//该模型运行于后台程序化模式

//===========================================================
//《定制的海龟交易系统V1.0后台版本》
// 适用于多时间框架图表
// 仅能工作于金字塔的后台
// 工作模式:轮询法,建议设置1秒,数据长度<300根
// 说明:
// 1、如果最后一根K线发生过进场或者加仓,则该K线不执行出场
// 2、如果最后一根K线发生过出场,则该K线不执行进场
// DESIGNED BY LIKAI
// 2010.07.20
//===========================================================

//声明参数
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线发出的。
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




请帮忙解释一下,分两次完整交易代码是什么原因?分下面二次:

1,//如果当前棒是最后一根K线 的情况
2,//不是最后一根K线的情形

上面这二次的交易代码几乎一样,为什么这样分出来?特别是 第2项,什么时候不是最后一根K线的情形(是不是因为当天最后收盘价刚好达到开仓价位没法开,后一天开盘时检测到而开这种情形)?





--  作者:FireScript
--  发布时间:2021/5/24 13:37:53
--  
 其实是因为这个代码是一个混搭的代码模式,后台模式只需要管最新K或者说是最后一个K。而后面部分则是图表代码,图表逻辑是包含历史的。   再进一步的解析就没办法做到了,这都是很久之前的代码了,目前也仅供参考了。