2023年7月21日 星期五

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

EasyTrader TestNo 020

參考文章 國外知名指標 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) ;

//計算近期高低點
// ************ High/Low data *****************
BarPass = MaxList(HBar,LBar) ;
HLRange = Highest(High,BarPass)-Lowest(Low,BarPass) ;
if MP <> 0 then WinPoint = MaxPositionProfit/BigPointValue else WinPoint = 0 ;
HBarPos = HighestBar(High,HBar) ;
LBarPos = LowestBar(Low,LBar) ;

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

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

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 = EntriesToday(Date)=0  ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = Highest(High,Hbar) ;  
ShortPrice = OpenD(0)-OpenD(0)*RatioS/10000  ;
   
// ********** 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 TimeOK then begin
//Macd > 0 且柱狀圖上升 收盤價突破當日開盤價+買方震盪價 進場做多
   if MP <> 1 and uMacd > 0 and uOsc > uOsc[1] and Close < OpenD(0)+BuyTrig then 
      Buy ("LE_Dual_1T") next bar at OpenD(0)+BuyTrig stop ;
//Macd < 0 且柱狀圖下降 收盤價跌破當日開盤價-賣方震盪價 進場做空
   if MP <> -1 and uMacd < 0 and uOsc < uOsc[1] and Close > OpenD(0)-SellTrig then 
      SellShort ("SE_Dual_1T") next bar at OpenD(0)-SellTrig stop ;
   end ;


   if EntryExitCond then begin

//Macd > 0 且柱狀圖上升 收盤價突破設定價位+買方震盪價 進場做多
   if MP <> 1 and uMacd > 0 and uOsc > uOsc[1] and Close < BuyPrice+BuyTrig then 
      Buy ("LE_Dual_2") next bar at BuyPrice+BuyTrig stop ;

//Macd < 0 且柱狀圖下降 收盤價跌破設定價位-賣方震盪價 進場做空
   if MP <> -1 and uMacd < 0 and uOsc < uOsc[1] and Close > ShortPrice-SellTrig then 
      SellShort ("SE_Dual_2") 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 maxpositionloss/currentcontracts < -20000)
      then buy ("WrongEntryS8") next bar at Close stop ;

// ************* Rev RSI Divergence Exit ************* 
   DivRSICondA = 近期RSI 高位背離 ;
   if DivRSICondA then ExitL = Lowest(Low,2) ;

// 多單部位 近期K棒出現 RSI 高位背離 則跌破近2根K棒低點 反手做空
   if MP > 0 and MRO(DivRSICondA,HBar,1) > 1 and Close > (ExitL-Range) then 
      SellShort ("rLX_DivRSI") next bar at ExitL-Range stop ;
   
   DivRSICondB = 近期RSI 低位背離 ;
   if DivRSICondB then ExitH = Highest(High,2) ;

// 空單部位 近期K棒出現 RSI 低位背離 則突破近2根K棒高點 反手做多
   if MP < 0 and MRO(DivRSICondB,LBar,1) > 1 and Close < (ExitH+Range) then 
      Buy ("rSX_DivRSI") next bar at ExitH+Range stop ;


// ************* Rev Entry High/Low PullBack *************

//多單部位 自進場價格跌落一段距離 反手做空
   if MP > 0 and BarsSinceEntry > NBarL then 
      SellShort ("rLX_EntryHPB") next bar at EntryPrice - MaxList(FracA*AvgTrueRange(BarPass),100) stop;

//空單部位 自進場價格上升一段距離 反手做多
   if MP < 0 and BarsSinceEntry > NBarS then 
      Buy ("rSX_EntryLPB") next bar at EntryPrice + MaxList(FracB*AvgTrueRange(BarPass),100) 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






2 則留言: