2024年5月3日 星期五

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

EasyTrader TestNo 035

參考文章 隨機相對強弱指標 Stoch RSI

// 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) ;
if MP <> MP[1] and MP > 0 then ExitL = EntryPrice-PL ;
if MP <> MP[1] and MP < 0 then ExitH = EntryPrice+PL ;

//計算布林通道區間價格
// ************ BollingerBand *****************
vars:BBUP(0),BBDN(0),BBRange(0),BBLen(0),DataBase(Close),DataType(1) ;
BBLen = MaxList(LenA2,LenB2) ;
DataType = Mod(BBLen,5)+1 ;
if DataType < 1 or DataType > 5 then DataType = 1 ; 
if DataType = 1 then DataBase = Close ;
   
BBUP = BollingerBand(DataBase,BBLen,2) ; 
BBDN = BollingerBand(DataBase,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 ; 
CDPrange = HighD(1) - LowD(1) ;
CrossA = Highest(High,2) > AH and Lowest(Low,2) < NH ;
CrossB = Highest(High,2) > NL and Lowest(Low,2) < AL ;


// ************ 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 >= 0900 and time <= 1200) or (time >= 2130)) ;

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

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = Highest(High,Hbar) ;
ShortPrice = WL+Range ;
   
// ********** Main Strategy *********
// ********** Entry Method
vars:SDLen(9),RSILen(5),HLRSI(0),StochRSI(0) ;
SDLen = LenA1 ;
RSILen = LenB1 ;

HLRSI = (Highest(RSI(Close,RSILen),SDLen)-Lowest(RSI(Close,RSILen),SDLen)) ;
if HLRSI <> 0 then StochRSI = (RSI(Close,RSILen)-Lowest(RSI(Close,RSILen),SDLen))/HLRSI*100 ;

   if TimeOK then begin

//隨機RSI值 大於設定值 且連續3次布林通道上軌上升 收盤價突破設定價位做多
      if MP <> 1 and StochRSI > HighBand*4 and Countif(BBUP > BBUP[1],3) = 3 and Close < BuyPrice then  Buy ("LE_StochRSI1T") next bar at BuyPrice stop;

//隨機RSI值 小於設定值 且連續3次布林通道下軌下降 收盤價跌破設定價位做空
      if MP <> -1 and StochRSI < LowBand*4 and Countif(BBDn < BBDn[1],3) = 3 and Close > ShortPrice then  SellShort ("SE_StochRSI1T") next bar at ShortPrice stop;
   end ;

   if TimeOK then begin

//隨機RSI值 大於設定值 且收盤價大於 NH值 且突破設定價位做多
      if MP <> 1 and StochRSI > HighBand*4 and Close > NH and Close < BuyPrice then 
         Buy ("LE_StochRSI2T") next bar at BuyPrice stop;

//隨機RSI值 小於設定值 且收盤價小於 NL值 且跌破設定價位做空
      if MP <> -1 and StochRSI < LowBand*4 and Close < NL and Close > ShortPrice then 
         SellShort ("SE_StochRSI2T") 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 maxpositionloss/currentcontracts < -20000)
      then buytocover ("WrongEntryS6") next bar at Close stop ;

// ************* Rev PullBack Exit after BigProfit  ************* 
vars:BigPFcond(false),LXprice(0),SXprice(99999),LXvalue(0),SXvalue(99999) ;

   if MP <> MP[1] then begin
      BigPFcond = false ;
      LXprice = 0 ;
      SXprice = 99999 ;
   end ;

   if WinPoint > PF*0.8 then begin
      BigPFcond = true ;
      if MP > 0 then LXprice = EntryPrice+WinPoint*0.5 ;
      if MP < 0 then SXprice = EntryPrice-WinPoint*0.5 ;
   end ;

   if BigPFcond then begin
      LXvalue = iff(EntriesToday(Date)=1,LXprice,MinList(LXprice,TypicalPrice)-MinList(8,LBarPos)*MinMove/PriceScale) ;

//持有多單 獲利達初步目標 收盤價跌破設定價位 反手做空
      if MP > 0 and Close > LXvalue then SellShort ("rLX_BPF") next bar at LXvalue stop ;
       
      SXvalue = iff(EntriesToday(Date)=1,SXprice,MaxList(SXprice,TypicalPrice)+MinList(8,HBarPos)*MinMove/PriceScale) ;

//持有空單 獲利達初步目標 收盤價突破設定價位 反手做多
      if MP < 0 and Close < SXvalue then Buy ("rSX_BPF") next bar at SXvalue 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






沒有留言:

張貼留言