2023年7月12日 星期三

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

EasyTrader TestNo 019

參考文章 國外知名指標 Dual Thrust 交易模型

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

//計算布林通道
// ************ BollingerBand *****************
vars:BBUP(0),BBDN(0),BBRange(0),BBLen(0) ;
BBLen = MaxList(LenA2,LenB2) ;
BBUP = BollingerBand(Close,BBLen,2) ; 
BBDN = BollingerBand(Close,BBLen,-2) ;
BBRange = BBUP-BBDN ;

//計算 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 ; 

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

//計算日內動量價格
// ********** Intraday IMI *********
vars:CandleBarNo(0),CandleHL(0),SumBar(0),SumIMI(0),IMI(0),IMIPlus(0),Strength(0);

CandleBarNo = MaxList(IntPortion(BarPass*1.6),25) ;
CandleHL = Highest(High,CandleBarNo)-Lowest(Low,CandleBarNo) ;

SumBar = Summation(AbsValue(Close-Open),CandleBarNo) ;
if SumBar <> 0 then IMI = (Summation(iff(Close > Open ,Close-Open,0),CandleBarNo)/SumBar)*100 ;

SumIMI = Summation(AbsValue(IMI-50),CandleBarNo) ;
IMIPlus = Summation(iff((IMI-50)>0,(IMI-50),0),CandleBarNo) ;
if SumIMI <> 0 then Strength = IMIPlus/SumIMI * 100 ;

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

// ************* Time Set Up *************
TimeOK = ((time >= 1000 and time <= 2000)) ;

// ************* Entry/Exit Condition Set Up *************
EntryExitCond = EntriesToday(Date)=0 ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = LowCloud ;   
ShortPrice = (OpenW(0)+CloseW(1))*0.5  ;
   
// ********** Main Strategy *********
// ********** Entry Method
//計算 Dual Thrust 價格區間
vars:Mday(2),NDay(2),K1(0.56),K2(0.34);
vars:HH(0),HC(0),LC(0),LL(0),SellRange(0),BuyRange(0),Count(0) ,BuyTrig(0),SellTrig(0) ;

Mday = IntPortion(LenA1/5)+1 ;
Nday = IntPortion(LenB1/5)+1 ;
K1 = FracA*0.5 ;
K2 = FracB*0.5 ;

If CurrentBar > 1 Then Begin
   HH = HighD(1);
   HC = CloseD(1);
   LL = LowD(1);
   LC = CloseD(1);
   For Count = 1 to Mday Begin
       HH = iff(HighD(Count) > HH ,HighD(Count) , HH);
       HC = iff(CloseD(Count) > HC ,CloseD(Count) , HC);
       LL = iff(LowD(Count) < LL ,LowD(Count) , LL);
       LC = iff(CloseD(Count) < LC ,CloseD(Count) , LC);;
   End ;
   If (HH - LC) >= (HC - LL) Then Begin
      SellRange = HH - LC;
   End Else Begin
      SellRange = HC - LL;
   End;

   HH = HighD(1);
   HC = CloseD(1);
   LL = LowD(1);
   LC = CloseD(1);
   For Count = 1 to Nday Begin
       HH = iff(HighD(Count) > HH ,HighD(Count) , HH);
       HC = iff(CloseD(Count) > HC ,CloseD(Count) , HC);
       LL = iff(LowD(Count) < LL ,LowD(Count) , LL);
       LC = iff(CloseD(Count) < LC ,CloseD(Count) , LC);;
   End ;

   If (HH - LC) >= (HC - LL) Then Begin
      BuyRange = HH - LC;
   End Else Begin
      BuyRange = HC - LL;
   End;
End;

//買方震盪價
BuyTrig = K1*BuyRange;
賣方震盪價
SellTrig = K2*SellRange;

   if EntryExitCond then begin
//最高價大於 布林通道上界值 收盤價突破當日開盤價+買方震盪價 進場做多
   if MP <> 1 and High > BBUP and Close < OpenD(0)+BuyTrig then 
      Buy ("LE_Dual_4") next bar at OpenD(0)+BuyTrig stop ;
//最低價小於 布林通道下界值 收盤價跌破當日開盤價-賣方震盪價 進場做空
   if MP <> -1 and Low < BBDn and Close > OpenD(0)-SellTrig then 
      SellShort ("SE_Dual_4") next bar at OpenD(0)-SellTrig stop ;
   end ;

   if TimeOK then begin
//最高價大於 AH 值 收盤價突破設定價位+買方震盪價 進場做多
   if MP <> 1 and High > AH and Close < BuyPrice+BuyTrig then 
      Buy ("LE_Dual_10T") next bar at BuyPrice+BuyTrig stop ;
//最低價小於 AL 值 收盤價跌破設定價位-賣方震盪價 進場做空
   if MP <> -1 and Low < AL and Close > ShortPrice-SellTrig then 
      SellShort ("SE_Dual_10T") next bar at ShortPrice-SellTrig 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 Sell ("WrongEntryL9") next bar at Close stop ;
   if MP < 0 and (BarsSinceEntry >= 300/Barinterval and MaxPositionprofit/currentcontracts < 5000) 
      then Buy ("WrongEntryS9") next bar at Close stop ;

// ****** Intraday Momentum Index
// 空單部位 且日內動量強度大於 50  突破設定價位平倉
   if MP < 0 and Strength > MaxList(HighBand*4,50) and Close < BuyPrice then 
      buytocover ("SX_IMI") next bar at BuyPrice stop ;
/ 多單部位 且日內動量強度小於 50  跌破設定價位平倉
   if MP > 0 and Strength < MinList(LowBand*4,50) and Close > ShortPrice and CandleHL < 100 then 
      Sell ("LX_IMI") 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







沒有留言:

張貼留言