2023年5月20日 星期六

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

EasyTrader TestNo 014

參考文章 日內動量指標 (Intraday Momentum Index)

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

// Parameter for all strategies use

vars:EntNum01(4.0),EntNum02(0.0),ExtNum01(74.0),ExtNum02(0.0),LenA1(20.0),LenB1(20.0),FracA(2.50),FracB(2.50),
HBar(20.0),LBar(20.0),TradeProfit(0.025),TradeStopLoss(0.015),NBarL(20.0),NBarS(20.0),LenA2(20.0),LenB2(20.0),
RatioL(2.50),RatioS(2.50),HighBand(20.0),LowBand(20.0),BuyMode(57),ShortMode(51),LE01(8),SE01(8);

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

// ************ for week High/Low box *****************
Vars:WH(0),WL(0);
WH = 近週高點;
WL = 近週低點 ;

// ************ for Day High/Low box *****************
Vars:DayLong(5),DayShort(5),DH(0),DL(0),HLDay(0),DCount(0) ;
DH = HighD(1) ;
DL = LowD(1) ;
DayLong = IntPortion(NBarL/5)*2+3 ;
DayShort= IntPortion(NBarS/5)*2+3 ;

for DCount = 0 to DayLong-1 Begin
   if HighD(DCount) > DH then DH = HighD(DCount) ;
end;
for DCount = 0 to DayShort-1 Begin
   if LowD(DCount) < DL then DL = LowD(DCount) ;
end;
HLDay = DH-DL ;

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

// ************* Time Set Up *************
if LE01 = 8 then TimeOK = ((time >= 1000 and time <= 2100)) or (time >= 0300 and time <= 0500 ) ;

//***************** BuyPrice & ShortPrice Setup *****************
if BuyMode = 57 then BuyPrice = DH-Range ;
if ShortMode = 51 then ShortPrice = WL ;

// ********** Main Strategy *********
vars:CandleBarNo(0),CandleHL(0),SumBar(0),SumIMI(0),IMI(0),IMIPlus(0),Strength(0);

CandleBarNo = MaxList(IntPortion(BarPass*1.6),25) ;

//計算區間平均動量
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 EntNum01 = 4 or EntNum02 = 4 then begin
   if TimeOK then begin

//動量強度大於50 且收盤價突破特定價格 進場做多
      if MP <> 1 and Strength > MaxList(HighBand*4,50) and Close < BuyPrice then 
         Buy ("LE_IMI_2T") next bar at BuyPrice stop ;

//動量強度小於50 且收盤價跌破特定價格 進場做空
      if MP <> -1 and Strength < MinList(LowBand*4,50) and Close > ShortPrice then 
         SellShort ("SE_IMI_2T") next bar at ShortPrice stop ;
   end ;
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 SE01 = 8 then begin
   if MP > 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
      then Sellshort ("WrongEntryL8") next bar at Close stop ;
end ;

// ************* rev N Day trade Exit *************
if ExtNum01 = 74 or ExtNum02 = 74 then begin

//收盤價自近日高點回落一段距離 再次突破該價格 反手做多
   if MP < 0 and Close < DH-(HLday*FracA*0.1) then 
      buy ("rLX_DTrade1") next bar at DH-(HLday*FracA*0.1)+Range Stop ;

//收盤價自近日低點反彈一段距離 再次跌破該價格 反手做空
   if MP > 0 and Close > DL+(HLday*FracB*0.1) then 
      sellshort ("rSX_DTrade1") next bar at DL+(HLday*FracB*0.1)-Range Stop ;
end ;

//結算日出場
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






沒有留言:

張貼留言