2024年4月10日 星期三

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

EasyTrader TestNo 034

參考文章 波動叢聚(Volatility Cluster)之交易策略

// 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) ;

//計算近3日區間價格
// ************3 Day High or Low *****************
vars:DayH(High),DayL(Low),DayHL(0),DayHPB(0),DayLPB(0),Day33(0),Day67(0) ;
DayH = 近3日高點;
DayL = 近3日低點 ;

//計算近3週區間價格
// ************3 Week High or Low *****************
vars:WeekH(High),WeekL(Low),WeekHL(0),WeekHPB(0),WeekLPB(0),Week33(0),Week67(0) ;
WH = 近週高點;
WL = 近週低點 ;

計算不同均線值
// ************* 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) ;

//計算一目均衡表區間價格
// ************ 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) ;
vars: CondL(false),CondS(false) ;

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) ;
CondL = Close > HighCloud and HighCloud > LowCloud ;
CondS = Close < HighCloud and HighCloud < LowCloud ;

// 計算真實區間ATR 變化比例
// ********** ATR Ratio *********
vars:TRratioA(0),TRratioB(0),RA(0),RB(0) ;
RA = FracA+1 ;
RB = FracB+1 ;
  
if AvgTrueRange(IntPortion(LenA1*RA)) <> 0 then TRratioA = AvgTrueRange(LenA1)/AvgTrueRange(IntPortion(LenA1*RA)) ;
if AvgTrueRange(IntPortion(LenB1*RB)) <> 0 then TRratioB = AvgTrueRange(LenB1)/AvgTrueRange(IntPortion(LenB1*RB)) ;

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)) or (time >= 0300 and time <= 0500 ) ;

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

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = Highest(High,Hbar) ;
ShortPrice = LowW(1) ;

// ********** Main Strategy *********
// ********** Entry Method
Vars: Deviations(2), Length(18),BarLength(3);
Vars: CloseToClose(0), CTCDeviations(0), RangeDeviations(0), BigVolatility(False);

Length = MaxList(LenA1,LenB1) ;

{Setup calculations}
//計算兩根K棒收盤價的價差變化區間以及振幅 以判斷波動狀況
CloseToClose = AbsValue(Close - Close[1]);
CTCDeviations = Average(CloseToClose, Length) + StdDev(CloseToClose, Length) * Deviations;
RangeDeviations = Average(Range, Length) + StdDev(Range, Length) * Deviations;
BigVolatility = CloseToClose > CTCDeviations[1] OR Range > RangeDeviations[1];

   if EntryExitCond then begin
//價格波動變大 且收盤價在近週區間2/3之上 且自近週高點拉回一段時間 收盤價突破設定價位做多
      if MP <> 1 and BigVolatility and Close > Week67 and WeekHPB > LenB1 and Close < BuyPrice then  Buy ("LE_LUX7") next bar at BuyPrice stop;

//價格波動變大 且收盤價在近週區間1/3之下 且自近週低點反彈一段時間 收盤價跌破設定價位做空
      if MP <> -1 and BigVolatility and Close < Week33 and WeekLPB > LenB1 and Close > ShortPrice then  SellShort ("SE_LUX7") next bar at ShortPrice stop;
   end ;       

   if EntryExitCond then begin
//價格波動變大 且收盤價在藍色雲層之上 收盤價突破設定價位做多
      if MP <> 1 and BigVolatility and CondL and Close < BuyPrice then 
         Buy ("LE_LUX10") next bar at BuyPrice stop;

//價格波動變大 且收盤價在紅色雲層之下 收盤價跌破設定價位做空
      if MP <> -1 and BigVolatility and CondS and Close > ShortPrice then 
         SellShort ("SE_LUX10") next bar at ShortPrice 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 maxpositionloss/currentcontracts < -20000)
      then Sell ("WrongEntryL2") next bar at Close stop ;
//空單部位虧損大於預期 平倉出場
   if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
      then buytocover ("WrongEntryS2") next bar at Close stop ;


// ************* BreskThrough 3 Line *************
vars:Max3L(0),Min3L(0);
Max3L = MaxList(Avg7,Avg13,Avg21) ;
Min3L = MinList(Avg7,Avg13,Avg21) ;

//持有多單 進場大於3根K棒 且近四根K收盤價大於短中期均線 則跌破短中期均線反手作空
    if MP > 0 and Countif(Close > Max3L,4) = 4 and BarsSinceEntry >= 3 then
       SellShort ("rLX_BK3L") next bar at Min3L stop;

//持有空單 進場大於3根K棒 且近四根K收盤價小於短中期均線 則突破短中期均線反手作多
    if MP < 0 and Countif(Close < Min3L,4) = 4 and BarsSinceEntry >= 3 then
       Buy ("rSX_BK3L") next bar at Max3L stop;


//持有空單 區間波動比例小於設定值 且近日高點拉回一段時間 收盤價突破設定價位反手做多
   if MP < 0 and TRratioA < RatioL and DayHPB < LenA1 and Close < BuyPrice then 
      Buy ("rSX_ATR_2") next bar at BuyPrice Stop ;

//持有多單 區間波動比例大於設定值 且近日低點反彈一段時間 收盤價跌破設定價位反手做空
   if MP > 0 and TRratioB > RatioS and DayLPB > LenB1 and Close > ShortPrice then 
      SellShort ("rLX_ATR_2") next bar at ShortPrice 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












沒有留言:

張貼留言