三、每个小节都提前X分钟平仓
//实现功能:每个小节都可以提前平仓
//此范例适用于图表程序化交易,适用于分钟周期
//使用固定轮询模式
[PEL] 复制代码 N:BARSLAST(DATE<>REF(DATE,1))+1; //当日K线数量
CONKD:H>=HHV(H,2) AND C>=HHV(C,2) AND N>=3 AND TIME<=185700; //开多条件,最后一根K线上不开仓,加上时间控制,避免清仓后又再次开仓
CONKK:L<=LLV(L,2) AND C<=LLV(C,2) AND N>=3 AND TIME<=185700; //开空条件
CONPD:C<REF(L,1); //平多条件
CONPK:C>REF(H,1); //平空条件
SELL(CONPD AND HOLDING>0,HOLDING,MARKET);
SELLSHORT(CONPK AND HOLDING<0,HOLDING,MARKET);
BUY(CONKD,1,MARKET);
BUYSHORT(CONKK,1,MARKET);
//有夜盘品种各小节平仓
IF CLOSETIME(0)=CLOSETIME(4) THEN BEGIN
IF (ISLASTBAR AND T0TOTIME(TIMETOT0(CLOSETIME(1))-60)<=DYNAINFO(207)) OR (TIME=CLOSETIME(1) AND NOT(ISLASTBAR)) THEN BEGIN
夜盘平多:SELL(1,HOLDING,MARKET);
夜盘平空:SELLSHORT(1,HOLDING,MARKET); //夜盘品种,夜盘提前1分钟清仓
END
IF (ISLASTBAR AND T0TOTIME(TIMETOT0(CLOSETIME(3))-60)<=DYNAINFO(207)) OR (TIME=CLOSETIME(3) AND NOT(ISLASTBAR)) THEN BEGIN
午盘平多:SELL(1,HOLDING,MARKET);
午盘平空:SELLSHORT(1,HOLDING,MARKET); //夜盘品种,午盘提前1分钟清仓
END
IF (ISLASTBAR AND T0TOTIME(TIMETOT0(CLOSETIME(0))-60)<=DYNAINFO(207)) OR (TIME=CLOSETIME(0) AND NOT(ISLASTBAR)) THEN BEGIN
尾盘平多:SELL(1,HOLDING,MARKET);
尾盘平空:SELLSHORT(1,HOLDING,MARKET); //夜盘品种,尾盘提前1分钟清仓
END
END
//无夜盘品种各小节平仓
IF CLOSETIME(0)<>CLOSETIME(4) THEN BEGIN
IF (ISLASTBAR AND T0TOTIME(TIMETOT0(CLOSETIME(2))-60)<=DYNAINFO(207)) OR (TIME=CLOSETIME(2) AND NOT(ISLASTBAR)) THEN BEGIN
午盘平多1:SELL(1,HOLDING,MARKET);
午盘平空1:SELLSHORT(1,HOLDING,MARKET); //无夜盘品种,午盘提前1分钟清仓
END
IF (ISLASTBAR AND T0TOTIME(TIMETOT0(CLOSETIME(0))-60)<=DYNAINFO(207)) OR (TIME=CLOSETIME(0) AND NOT(ISLASTBAR)) THEN BEGIN
尾盘平多1:SELL(1,HOLDING,MARKET);
尾盘平空1:SELLSHORT(1,HOLDING,MARKET); //无夜盘品种,尾盘提前1分钟清仓
END
END |