2024年6月7日 星期五

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

EasyTrader TestNo 038

參考文章 
彈性指數動態平均線(VIDYA)

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

//計算CDP 區間價格
// ************ CDP *****************
vars:CDP(0),AH(0),NH(0),NL(0),AL(0),CDPrange(0),CrossA(false),CrossB(false),TH1(0),TL1(0),TH2(0),TL2(0) ;
if Date <> Date[1] then begin
   CDP = (HighD(1)+LowD(1)+2*CloseD(1))/4;
   AH = CDP + (HighD(1) - LowD(1));
   NH = CDP*2 - LowD(1);
   NL = 2*CDP - HighD(1);
   AL = CDP - (HighD(1) - LowD(1));
end ; 
CDPrange = HighD(1) - LowD(1) ;

//計算近三週 區間價格
// ************3 Week High or Low *****************
vars:WeekH(High),WeekL(Low),WeekHL(0),WeekHPB(0),WeekLPB(0),Week33(0),Week67(0) ;
WeekH = MaxList(HighW(0),HighW(1),HighW(2)) ;
WeekL = MinList(LowW(0),LowW(1),LowW(2)) ;
WeekHL = WeekH - WeekL ;
Week33 = WeekL+WeekHL*0.33 ;
Week67 = WeekL+WeekHL*0.67 ;
WeekHPB = _BarsLast(High = WeekH) ;
WeekLPB = _BarsLast(Low = WeekL) ;

//計算長短均線價格
// ************* 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) ;

// ************ for week High/Low box *****************
Vars:WeekLong(5),WeekShort(5),WH(0),WL(0),HLWeek(0),WCount(0),WBox33(0),WBox67(0),WBoxHPB(0),WBoxLPB(0) ;
WH = 近週高點 ;
WL = 近週低點 ;

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 ;

// ************* Entry/Exit Condition Set Up *************
SelectNo = IntPortion(RatioL+0.6) ;
if SelectNo < 1 or SelectNo > 5 then SelectNo = 1 ;
if SelectNo = 5 then EntryExitCond = True ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = WH ;
ShortPrice = OpenD(0)-OpenD(0)*RatioS/10000  ;

// ********** Main Strategy *********
// ********** Entry Method
//計算 彈性指數動態平均線(VIDYA)
vars:Vidya1(1),Vidya2(1) ;

Vidya1 = _SS_Vidya(LenA1,FracA) ;
Vidya2 = _SS_Vidya(LenB1,FracB) ;

   if TimeOK then begin
// 連續3根K棒 VIDYA值 上升且收盤價大於 NH值 則收盤價突破設定價位做多
      if MP <> 1 and Countif(Vidya1 > Vidya1[1],3) = 3 and Close > NH and Close < BuyPrice then 
         Buy ("LE_Vidya2T") next bar at BuyPrice stop;

// 連續3根K棒 VIDYA值 下降且收盤價小於 NL值 則收盤價跌破設定價位做空
      if MP <> -1 and Countif(Vidya2 < Vidya2[1],3) = 3 and Close < NL and Close > ShortPrice then 
         SellShort ("SE_Vidya2T") next bar at ShortPrice stop;
   end ;

   if EntryExitCond then begin
// 連續3根K棒 VIDYA值 上升且收盤價大於近3週區間價2/3 則收盤價突破設定價位做多
      if MP <> 1 and Countif(Vidya1 > Vidya1[1],3) = 3 and Close > Week67 and WeekHPB > LenB1 and Close < BuyPrice then  Buy ("LE_Vidya7") next bar at BuyPrice stop;

// 連續3根K棒 VIDYA值 下降且收盤價小於 近3週區間價1/3 則收盤價跌破設定價位做空
      if MP <> -1 and Countif(Vidya2 < Vidya2[1],3) = 3 and Close < Week33 and WeekLPB > LenB1 and Close > ShortPrice then  SellShort ("SE_Vidya7") next bar at ShortPrice stop;
   end ;       

//基本停利+移動停損出場
// ************* Base Exit *************
if MP > 0 then Sell ("PL1_"+NumtoStr(PL,0)) next bar at HighSinceEntry-MinList(PL,PF) 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+MinList(PL,PF) 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 buy ("WrongEntryS8") next bar at Close stop ;


// ************* BreakThrough 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
       Sell ("LX_BK3L") next bar at Min3L stop;

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

// ************* N Week trade Exit *************
// 持有空單 收盤價自近週高點下跌一段距離後 再次突破價位平倉出場
   if MP < 0 and Close < WH-(HLWeek*FracA*0.1) then 
      buytocover ("LX_WTrade1") next bar at WH-(HLWeek*FracA*0.1)+Range Stop ;

// 持有多單 收盤價自近週低點上升一段距離後 再次跌破價位平倉出場
   if MP > 0 and Close > WL+(HLWeek*FracB*0.1) then 
      Sell ("SX_WTrade1") next bar at WL+(HLWeek*FracB*0.1)-Range 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







沒有留言:

張貼留言