2024年5月25日 星期六

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

EasyTrader TestNo 037

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

//計算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 Day High or Low *****************
vars:DayH(High),DayL(Low),DayHL(0),DayHPB(0),DayLPB(0),Day33(0),Day67(0) ;
DayH = MaxList(HighD(0),HighD(1),HighD(2)) ;
DayL = MinList(LowD(0),LowD(1),LowD(2)) ;
DayHL = DayH - DayL ;
Day33 = DayL+DayHL*0.33 ;
Day67 = DayL+DayHL*0.67 ;
DayHPB = _BarsLast(High = DayH) ;
DayLPB = _BarsLast(Low = DayL) ;

//計算一目均衡表 區間價格
// ************ 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 ;

//計算波動率變動範圍
Vars: Deviations(2), Length(18),BarLength(3);
Vars: CloseToClose(0), CTCDeviations(0), RangeDeviations(0), BigVolatility(False);

Length = MaxList(LenA1,LenB1) ;

{Setup calculations}
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 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 = (EntriesToday(Date)=0 and ExitsToday(Date)=0) ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = Maxlist(OpenD(0),OpenD(1),OpenD(2))+Range*0.5 ;
ShortPrice = CloseD(1)-Range*0.5  ;
   
// ********** Main Strategy *********
// ********** Entry Method
//計算 彈性指數動態平均線(VIDYA)
vars:Vidya1(1),Vidya2(1) ;

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

   if EntryExitCond 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_Vidya2") 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_Vidya2") 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 > Day67 and DayHPB > LenB1 and Close < BuyPrice then  Buy ("LE_Vidya6") next bar at BuyPrice stop;

// 連續3根K棒 VIDYA值 下降且收盤價小於近3日區間1/3價 則收盤價跌破設定價位做空
      if MP <> -1 and Countif(Vidya2 < Vidya2[1],3) = 3 and Close < Day33 and DayLPB > LenB1 and Close > ShortPrice then SellShort ("SE_Vidya6") 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 MaxPositionprofit/currentcontracts < 5000) 
      then Buy ("WrongEntryS7") next bar at Close stop ;

//持有空單且波動率變大 且收盤價在藍色雲層之上 突破設定價位平倉出場
   if MP < 0 and BigVolatility and CondL and Close < BuyPrice then 
      buytocover ("SX_Cluster10T") next bar at BuyPrice stop;
//持有多單且波動率變大 且收盤價在紅色雲層之下 跌破設定價位平倉出場
   if MP > 0 and BigVolatility and CondS and Close > ShortPrice then 
      Sell ("LX_Cluster10T") 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






沒有留言:

張貼留言