2023年8月11日 星期五

台指期全時段交易回測篇 [22]

EasyTrader TestNo 022

參考文章  CCI 商品通道指標

// Public Variable
vars:MP(0),PF(0),PL(0),DayLast(1345),NightLast(0500),BuyPrice(0),ShortPrice(0),BasePF(150),BasePL(100),FilterA(0),FilterB(0) ;
vars:BarPass(5),HLRange(0),WinPoint(0),HBarPos(0),LBarPos(0),ExitH(0),ExitL(0),TimeOK(false),EntryExitCond(false),SelectNo(0) ;


//****************** Basic Setup ****************************************
MP = MarketPosition ;
if MP <> 0 then Begin
   PF = EntryPrice*TradeProfit ;
   PL = EntryPrice*TradeStopLoss ;
end else begin
   PF = AvgPrice*TradeProfit ;
   PL = AvgPrice*TradeStopLoss ;
end ;

if Close > 10000 then begin
   BasePF = BasePF+IntPortion((Close-10000)/1000)*30 ;
   BasePF = MinList(BasePF,450) ;
   BasePL = BasePL+IntPortion((Close-10000)/1000)*15 ;
   BasePL = MinList(BasePL,225) ;
end ;

PF = MinList(PF,BasePF) ;
PL = MinList(PL,BasePL)*iff(CurrentContracts=1,1,0.67) ;

//計算自訂 MACD 值  
// ************ Macd *****************
vars:vDif(0),vMacd(0),vOsc(0),uDif(0),uMacd(0),uOsc(0),MacdLen(0) ;
MacdLen = MaxList(LenA1,LenB1) ;
if MacdLen > 12 then MacdLen = 12 ;
uDif = Macd((High+Low+Close*2)/4,IntPortion(MacdLen*1.3),MacdLen*3) ;
uMacd = Xaverage(uDif,MacdLen) ;
uOsc = uDif-uMacd ;

//計算近60根K棒高低點區間
// ************Fix Length High or Low *****************
vars:FixLenH(High),FixLenL(Low),FixLenHL(0),FixLenHPB(0),FixLenLPB(0),FixLen33(0),FixLen67(0) ;
FixLenH = Highest(High,60) ;
FixLenL = Lowest(Low,60) ;
FixLenHL = FixLenH - FixLenL ;
FixLen33 = FixLenL+FixLenHL*0.33 ;
FixLen67 = FixLenL+FixLenHL*0.67 ;
FixLenHPB = _BarsLast(High = FixLenH) ;
FixLenLPB = _BarsLast(Low = FixLenL) ;

//計算均線
// ************* BreskThrough 3 Line *************
vars:Avg7(0),Avg13(0),Avg21(0),Avg60(0) ;
Avg7 = Average(Close,7) ;
Avg13 = Average(Close,13) ;
Avg21 = Average(Close,21) ;
Avg60 = Average(Close,60) ;

//計算 RSI 值
// ************ RSI *****************
vars:vRSIA(0),vRSIB(0),DivRSICondA(false),DivRSICondB(false) ;
vRSIA = RSI(Close,Minlist(LenA1,LenB1)) ;
vRSIB = RSI(Close,Maxlist(LenA1,LenB1)) ;

//計算一目均衡表區間值
// ************ Ichimoku Sanjin Syytem *****************
Vars: ShortTerm(9),MidTerm(26),LongTerm(52);
Vars: ConvertLine(0),BaseLine(0),AheadLine1(0),AheadLine2(0),BehindLine(0),HighCloud(0),LowCloud(0),Cloud(0) ;
Vars: BaseCond(false),ConvertCond(false),C_over_B(0),C_Under_B(0),H_Over_L(0),H_Under_L(0),Thick(100) ;

ShortTerm = LenA1 ;
if ShortTerm <= 8 then ShortTerm = 8 
   else if ShortTerm >= 20 then ShortTerm = 20 ;
MidTerm = ShortTerm*3 ;
LongTerm = ShortTerm*6 ;

ConvertLine = (highest(high,ShortTerm) + Lowest(Low,ShortTerm))/2 ;
BaseLine = (highest(high,MidTerm) + Lowest(Low,MidTerm))/2 ;
AheadLine1 = (ConvertLine + BaseLine)/2 ;
AheadLine2 = (highest(high,LongTerm) + Lowest(Low,LongTerm))/2 ;
BehindLine = Close ;
C_over_B = _BarsLast(ConvertLine cross over BaseLine) ;
C_Under_B = _BarsLast(ConvertLine cross under BaseLine) ;

HighCloud = AheadLine1[MidTerm] ;
LowCloud = AheadLine2[MidTerm] ;
Cloud = AbsValue(HighCloud-LowCloud) ;
H_Over_L = _BarsLast(HighCloud cross over LowCloud) ;
H_under_L = _BarsLast(HighCloud cross under LowCloud) ;

if BarNumber = 1 then begin
   Buy this bar on Close ;
   Sell this bar on Close ;
end ;

// ************* Time Set Up *************
TimeOK = ((time >= 1200 and time <= 2345)) ;

// ************* Entry/Exit Condition Set Up *************
EntryExitCond = BarsSinceExit(1) > MinList(LenA1,LenB1) ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = MaxList(OpenW(0),CloseW(0))+Range ;
ShortPrice = LowW(1) ;

//多空趨勢設定
//*********** Trend filter setup
vars:UpTrendNo(1),DnTrendNo(1),UpTrend_High(false),UpTrend_Low(false),DnTrend_High(false),DnTrend_Low(false) ;
vars:Trend4Long(false),Trend4Short(false) ;

UpTrendNo = Mod(LenA1,5)+1 ;
DnTrendNo = Mod(LenB1,5)+1 ;

UpTrend_High = 多方趨勢
DnTrend_Low = 空方趨勢

if UpTrendNo = 1 then Trend4Long = UpTrend_High ;
if  DnTrendNo = 2 then Trend4Short = DnTrend_Low ;
   
// ********** Main Strategy *********
// ********** Entry Method
vars:CCI_A(0),CCI_B(0);

//計算 2組 CCI 值
CCI_A = CCI(LenA1) ;
CCI_B = CCI(LenB1) ;

   if EntryExitCond then begin

//CCI 值突破設定值 且 多方趨勢 市價做多
   if MP <> 1 and CCI_A Cross over HighBand*10 and Trend4Long and Avg7 > Avg21 and Avg21 > Avg60 then  Buy ("LE_CCI_4") next bar at Market ;

//CCI 值跌破設定值 且 空方趨勢 市價做空
   if MP <> -1 and CCI_A Cross under LowBand*10 and Trend4Short and Avg7 < Avg21 and Avg21 < Avg60 then  SellShort ("SE_CCI_4") next bar at Market ;
   end ;

   if EntryExitCond then begin
// CCI_A 與 CCI_B向上交叉 且 多方趨勢 且 Dif 值與 Osc 值均向上 則於近期高點做多
   if MP <> 1 and CCI_A Cross over CCI_B and Trend4Long and uDif > 0 and uOsc > uOsc[1] then 
      Buy ("LE_CCI_10") next bar at Highest(High,HBar) stop ;

// CCI_A 與 CCI_B向下交叉 且 空方趨勢 且 Dif 值與 Osc 值均向下 則於近期低點做空
   if MP <> -1 and CCI_A Cross under CCI_B and Trend4Short and uDif < 0 and uOsc < uOsc[1] then 
      SellShort ("SE_CCI_10") next bar at Lowest(Low,LBar) stop ;
   end ;


//基本停利+移動停損出場
// ************* Base Exit *************
if MP > 0 then Sell ("PL1_"+NumtoStr(PL,0)) next bar at HighSinceEntry-PL stop ;
if MP > 0 then Sell ("PF1_"+NumtoStr(PF,0)) next bar at EntryPrice+PF limit ;
if MP < 0 then BuytoCover ("PL2_"+NumtoStr(PL,0)) next bar at LowSinceEntry+PL stop ;
if MP < 0 then BuytoCover ("PF2_"+NumtoStr(PF,0)) next bar at EntryPrice-PF limit ;

//空單部位獲利小於預期 平倉出場
   if MP < 0 and (BarsSinceEntry >= 300/Barinterval and MaxPositionprofit/currentcontracts < 5000) 
      then Buytocover ("WrongEntryS5") next bar at Close stop ;

//空單部位 K棒最低價突破藍色雲層高點 且雲層高點大於60均線值 則市價反手作多
   if MP < 0 and Low Cross over HighCloud and HighCloud > LowCloud and HighCloud > Avg60 then 
      Buy ("rSX_MoKu7") next bar at Market ;

//多單部位 K棒最高價跌破紅色雲層高點 且雲層高點小於60均線值 則市價反手作空
   if MP > 0 and High Cross under HighCloud and HighCloud < LowCloud and HighCloud < Avg60 then  SellShort ("rLX_MoKu7") next bar at Market ;   

//空單部位 且RSI值 在近8根K棒有4根以上大於65 且區間高點拉回3根K以上 則收盤價再次突破區間高點平倉
   if MP < 0 and Countif(vRSIA > MaxList(HighBand*4,65),8) >= 4 and FixLenHPB >= 3 then 
      BuytoCover ("SX_RSI4") next bar at FixLenH stop ;

//多單部位 且RSI值 在近8根K棒有4根以上小於35 且區間低點反彈3根K以上 則收盤價再次跌破區間低點平倉
   if MP > 0 and Countif(vRSIB < MinList(LowBand*4,35),8) >= 4 and FixLenLPB >= 3 then 
      Sell ("LX_RSI4") next bar at FixLenL stop ;

//結算日出場
if _IsSettlementDay and time >= CalcTime(1300,-1*BarInterVal) and Time <= 1330 then begin
   if MP > 0 then Sell ("LX_Bal") next bar at market ;
   if MP < 0 then BuyToCover ("SX_Bal") next bar at market ;
end;

台指期  留倉 交易週期 2018/1/1 ~ 2022/12/31 交易成本 1200






沒有留言:

張貼留言