399330深证100指数的一个策略
通达信代码:
dma_week指标:
BARS:COUNT(C>0,0) NODRAW;
START_C_CONS :=REF(C,BARSCOUNT(C)); ROC1 :=100* (C/REF(C,1)-1) ;
PRICE:=(H+L+C)/3;
IC:="399330$C"; IA:="399330$AMOUNT";
IDMA8:= DMA(IC, IA/SUM(IA,N) ) COLORWHITE;
DMA8: DMA(C, AMOUNT/SUM(AMOUNT,N) ) COLORWHITE;
{
DMA8: DMA(C, AMOUNT/SUM(AMOUNT,N) ) COLORWHITE;
DMA8: MA(PRICE, N);
}
DMA13: DMA(C, AMOUNT/SUM(AMOUNT,20) ) COLORMAGENTA;
B1:= C>DMA8; S1:=C<DMA8;
IB1:= IC>IDMA8; IS1:=IC<IDMA8;
BUY(B1, L/1.05);
SELL(S1, H*1.05);
AUTOFILTER;
BBARS:=BUYBARS NODRAW COLORWHITE;
SBARS:=SELLBARS NODRAW COLORWHITE;
POS:= IF(BBARS<SBARS,1,0);
EQ:MULAR(1+ROC1*POS/100, 0) * START_C_CONS COLORCYAN LINETHICK2;
DD:= (1-EQ/HHV(EQ,0))*100;
MDD:=HHV(DD,0) NODRAW COLORWHITE;
BP:=BUYPRICE;
SP:=SELLPRICE;
PNL:=IF(SELLBARS=1, (SP/BP -1)*100, DRAWNULL) NODRAW COLORWHITE;
TOTAL_D:= IF(BARS=1, DAYSTOTODAY, DRAWNULL);
TOT_YEARS:= REF(TOTAL_D/365, BARSCOUNT(C));
N_WIN:=COUNT(PNL>0,0); N_LOSS:=COUNT(PNL<0,0); WIN_RATE:=N_WIN/(N_WIN+N_LOSS) * 100;
NUM_TRADES:=COUNT(SELLBARS=1,0);
NTLINE:= VALUEWHEN(SELLBARS=1, NUM_TRADES);
{
DRAWTEXT(SELLBARS=1, H*1.1, VARCAT(VAR2STR(PNL,2), '%'));
DRAWTEXT(SELLBARS=1, H*1.15, VARCAT(VAR2STR(NUM_TRADES,0), '次'));
}
HOLD_BARS_PER_T:= IF(SELLBARS=1, BUYBARS, DRAWNULL) ;
TOTAL_HOLD_BARS:= SUM(HOLD_BARS_PER_T,0) NODRAW;
FINAL_GR:=IF(CURRBARSCOUNT=1, EQ/START_C_CONS, DRAWNULL);
CAGR:= (POW(FINAL_GR, 1/TOT_YEARS)-1)*100;
NDATE:=DATE+19000000 NODRAW; STEP:=0.02;
DRAWTEXT_FIX(BARS=1 , 0.2,STEP*0,0, VARCAT('代码:',CODE) );
DRAWTEXT_FIX(BARS=1 , 0.3,STEP*0,0, VARCAT('品种名称:',STKNAME ));
DRAWTEXT_FIX(BARS=1 , 0.1,STEP*0,0, VARCAT('开始日期 = ', VAR2STR(NDATE,0)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*1,0, VARCAT('结束日期 = ', VAR2STR(NDATE,0)) );
DRAWTEXT_FIX(BARS=1 ,0.1,STEP*2,0, VARCAT('期间年数 = ', VAR2STR(DAYSTOTODAY/365,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*4,0, VARCAT('收益指数 = ', VAR2STR(EQ/START_C_CONS,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*5,0, VARCAT('CAGR(%)= ', VAR2STR(CAGR,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*6,0, VARCAT('最大回撤 = ', VAR2STR(MDD,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*8,0, VARCAT('交易次数 = ', VAR2STR(NTLINE,0)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*9,0, VARCAT('盈利次数 = ', VAR2STR(N_WIN,0)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*10,0, VARCAT('亏损次数 = ', VAR2STR(N_LOSS,0)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*11,0, VARCAT('年均次数 = ', VAR2STR(NTLINE/TOT_YEARS,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*12,0, VARCAT('胜率(%) = ', VAR2STR(WIN_RATE,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*13,0, VARCAT('持仓率% = ', VAR2STR(TOTAL_HOLD_BARS/BARS*100,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*14,0, VARCAT('平均持期 = ', VAR2STR(TOTAL_HOLD_BARS/NTLINE, 2)) );
HHHLLL指标:
周期高点:REF(HHV(H,N),1);
周期低点:REF(LLV(L,4),1);
平空开多:=HIGH>=周期高点;
平多开空:=LOW<=周期低点;
BUY(平空开多,LOW);
SELL(平多开空,HIGH);
AUTOFILTER;
BARS:COUNT(C>0,0) NODRAW COLORWHITE;
START_C_CONS := REF(C,BARSCOUNT(C)); ROC1 :=100* (C/REF(C,1)-1) ;
POS:= IF(BUYBARS<SELLBARS,1,0);
EQ:MULAR(1+ROC1*POS/100, 0) * START_C_CONS COLORCYAN LINETHICK2;
DD:= (1-EQ/HHV(EQ,0))*100;
MDD:=HHV(DD,0) NODRAW COLORWHITE;
PNL:=IF(SELLBARS=1, (SELLPRICE/BUYPRICE -1)*100, DRAWNULL) NODRAW COLORWHITE;
TOTAL_D:= IF(BARS=1, DAYSTOTODAY, DRAWNULL);
TOT_YEARS:= REF(TOTAL_D/365, BARSCOUNT(C));
N_WIN:=COUNT(PNL>0,0); N_LOSS:=COUNT(PNL<0,0); WIN_RATE:=N_WIN/(N_WIN+N_LOSS) * 100;
NUM_TRADES:=COUNT(SELLBARS=1,0);
NTLINE:= VALUEWHEN(SELLBARS=1, NUM_TRADES);
{
DRAWTEXT(SELLBARS=1, H1.1, VARCAT(VAR2STR(PNL,2), '%'));
DRAWTEXT(SELLBARS=1, H1.15, VARCAT(VAR2STR(NUM_TRADES,0), '次'));
}
HOLD_BARS_PER_T:= IF(SELLBARS=1, BUYBARS, DRAWNULL) ;
TOTAL_HOLD_BARS:= SUM(HOLD_BARS_PER_T,0) NODRAW;
FINAL_GR:=IF(CURRBARSCOUNT=1, EQ/START_C_CONS, DRAWNULL);
CAGR:= (POW(FINAL_GR, 1/TOT_YEARS)-1)*100;
NDATE:=DATE+19000000 NODRAW; STEP:=0.02;
DRAWTEXT_FIX(BARS=1 , 0.2,STEP*0,0, VARCAT('代码:',CODE) );
DRAWTEXT_FIX(BARS=1 , 0.3,STEP*0,0, VARCAT('品种名称:',STKNAME ));
DRAWTEXT_FIX(BARS=1 , 0.1,STEP*0,0, VARCAT('开始日期 = ', VAR2STR(NDATE,0)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*1,0, VARCAT('结束日期 = ', VAR2STR(NDATE,0)) );
DRAWTEXT_FIX(BARS=1 ,0.1,STEP*2,0, VARCAT('期间年数 = ', VAR2STR(DAYSTOTODAY/365,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*4,0, VARCAT('收益指数 = ', VAR2STR(EQ/START_C_CONS,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*5,0, VARCAT('CAGR(%)= ', VAR2STR(CAGR,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*6,0, VARCAT('最大回撤 = ', VAR2STR(MDD,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*8,0, VARCAT('交易次数 = ', VAR2STR(NTLINE,0)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*9,0, VARCAT('盈利次数 = ', VAR2STR(N_WIN,0)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*10,0, VARCAT('亏损次数 = ', VAR2STR(N_LOSS,0)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*11,0, VARCAT('年均次数 = ', VAR2STR(NTLINE/TOT_YEARS,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*12,0, VARCAT('胜率(%) = ', VAR2STR(WIN_RATE,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*13,0, VARCAT('持仓率% = ', VAR2STR(TOTAL_HOLD_BARS/BARS*100,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*14,0, VARCAT('平均持期 = ', VAR2STR(TOTAL_HOLD_BARS/NTLINE, 2)) );
绩效指标:
BARS: COUNT(C>0,0) NODRAW COLORWHITE;
START_C_CONS := REF(C,BARSCOUNT(C)); ROC1 :=100* (C/REF(C,1)-1) ;
POS:= IF(BUYBARS<SELLBARS,1,0);
EQ:MULAR(1+ROC1*POS/100, 0) * START_C_CONS COLORCYAN LINETHICK2;
NUM_TRADES:=COUNT(SELLBARS=1,0);
NTLINE:= VALUEWHEN(SELLBARS=1, NUM_TRADES);
DRAWTEXT(SELLBARS=1, H*1.15, VARCAT(VAR2STR(NUM_TRADES,0), '次'));
{
DD:= (1-EQ/HHV(EQ,0))*100; MDD:=HHV(DD,0) NODRAW COLORWHITE;
PNL:=IF(SELLBARS=1, (SELLPRICE/BUYPRICE -1)*100, DRAWNULL) NODRAW COLORWHITE;
DRAWTEXT(SELLBARS=1, H*1.1, VARCAT(VAR2STR(PNL,2), '%'));
N_WIN:=COUNT(PNL>0,0); N_LOSS:=COUNT(PNL<0,0); WIN_RATE:=N_WIN/(N_WIN+N_LOSS) * 100;
HOLD_BARS_PER_T:= IF(SELLBARS=1, BUYBARS, DRAWNULL) ;
TOTAL_HOLD_BARS:= SUM(HOLD_BARS_PER_T,0) NODRAW;
FINAL_GR:=IF(CURRBARSCOUNT=1, EQ/START_C_CONS, DRAWNULL);
TOTAL_D:= IF(BARS=1, DAYSTOTODAY, DRAWNULL); TOT_YEARS:= REF(TOTAL_D/365, BARSCOUNT(C));
CAGR:= (POW(FINAL_GR, 1/TOT_YEARS)-1)*100;
};
{说明语句的右大括号后面也最好添加一个分号, 以表名语句的结束};
{
NDATE:=DATE+19000000 NODRAW; STEP:=0.02;
DRAWTEXT_FIX(BARS=1 , 0.2,STEP*0,0, VARCAT('代码:',CODE) );
DRAWTEXT_FIX(BARS=1 , 0.3,STEP*0,0, VARCAT('品种名称:',STKNAME ));
DRAWTEXT_FIX(BARS=1 , 0.1,STEP*0,0, VARCAT('开始日期 = ', VAR2STR(NDATE,0)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*1,0, VARCAT('结束日期 = ', VAR2STR(NDATE,0)) );
DRAWTEXT_FIX(BARS=1 ,0.1,STEP*2,0, VARCAT('期间年数 = ', VAR2STR(DAYSTOTODAY/365,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*4,0, VARCAT('收益指数 = ', VAR2STR(EQ/START_C_CONS,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*5,0, VARCAT('CAGR(%)= ', VAR2STR(CAGR,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*6,0, VARCAT('最大回撤 = ', VAR2STR(MDD,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*8,0, VARCAT('交易次数 = ', VAR2STR(NTLINE,0)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*9,0, VARCAT('盈利次数 = ', VAR2STR(N_WIN,0)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*10,0, VARCAT('亏损次数 = ', VAR2STR(N_LOSS,0)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*11,0, VARCAT('年均次数 = ', VAR2STR(NTLINE/TOT_YEARS,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*12,0, VARCAT('胜率(%) = ', VAR2STR(WIN_RATE,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*13,0, VARCAT('持仓率% = ', VAR2STR(TOTAL_HOLD_BARS/BARS*100,2)) );
DRAWTEXT_FIX(CURRBARSCOUNT=1 ,0.1,STEP*14,0, VARCAT('平均持期 = ', VAR2STR(TOTAL_HOLD_BARS/NTLINE, 2)) );
};
结果:
-
在指数上应用该策略:
-
应用该策略, 操作159901深100ETF的结果
-
依据指数产生的信号, 操作对应的ETF(159901)的结果:
-
分析和结论:
case 2 带来的变化:
item | case 1 | case 2 |
---|---|---|
收益指数 | 13.61 | 14.54 |
CAGR(%) | 19.8 | 20.4 |
MDD(%) | 28.7 | 27.4 |
胜率(%) | 41.9 | 44.8 |
交易次数 | 62 | 58 |
结论: 依据指数发生的信号, 操作其ETF标的, 其结果更好(比用etf自身的数据).
- HHHLLL策略的截图: