2023年6月11日 星期日

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

EasyTrader TestNo 016

參考文章 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) ;

// ************Fix Length High or Low *****************
//計算近 60 根K高低點區間價
vars:FixLenH(High),FixLenL(Low),FixLenHL(0),FixLenHPB(0),FixLenLPB(0),FixLen33(0),FixLen67(0) ;
FixLenH = Highest(High,60) ;
FixLenL = Lowest(Low,60) ;
FixLenHL = FixLenH - FixLenL ;
FixLen33 = FixLenL+FixLenHL*0.33 ;
FixLen67 = FixLenL+FixLenHL*0.67 ;
FixLenHPB = _BarsLast(High = FixLenH) ;
FixLenLPB = _BarsLast(Low = FixLenL) ;

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

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

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 *************
vars:MoneyLoss(false),LossCount(false),MoneyProfit(false),ProfitCount(false),ProfitCond(false),LossCond(false),ExitLen(0) ;
if Totaltrades > 6 then begin // 交易次數大於6

//近兩次交易虧損總額超過 收盤價的 1%
   MoneyLoss = positionprofit(1)+positionprofit(2) < -1*(Close*0.01)*200 ;
//連續虧損3次
   LossCount = positionprofit(1) < 0 and positionprofit(2) < 0 and positionprofit(3) < 0 ;
//虧損條件
   LossCond = MoneyLoss or LossCount ;

//近兩次交易獲利總額超過 收盤價的 2%
   MoneyProfit = positionprofit(1)+positionprofit(2) > (Close*0.02)*200 ;
//連續獲利3次
   ProfitCount = positionprofit(1) > 0 and positionprofit(2) > 0 and positionprofit(3) > 0 ;
//獲利條件
   ProfitCond = MoneyProfit or ProfitCount ;
end else begin
   LossCond = true ;
   ProfitCond = true ;

end ;   

//計算符合上述條件出場後K棒數
ExitLen = iff(ProfitCond,1080/BarInterval,iff(LossCond,2160/BarInterval,MaxList(LenA1,LenB1))) ;
EntryExitCond = BarsSinceExit(1) > ExitLen ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = HighD(1)+Range*0.5 ;
ShortPrice = Minlist2(OpenD(0),OpenD(1),OpenD(2)) ;
   
// ********** Main Strategy *********
// ********** Entry Method  只做多單

   if EntryExitCond then begin

//短期RSI向上穿越長期RSI 且長期RSI值小於特定值 則收盤價突破特定價位作多
   if MP <> 1 and vRSIA Cross over vRSIB and vRSIB < HighBand*4 and Close < BuyPrice then 
      Buy ("LE_RSI3") next bar at BuyPrice stop ;

   end ;

   if EntryExitCond then begin

//短期RSI值 在近8根K棒有4根以上大於65 且區間高點拉回3根K以上 則收盤價再次突破區間高點作多
   if MP <> 1 and Countif(vRSIA > MaxList(HighBand*4,65),8) >= 4 and FixLenHPB >= 3 then 
      Buy ("LE_RSI8") next bar at FixLenH 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 Sell ("WrongEntryL6") next bar at Market ;

// ************* FixTime Exit *************
vars:FT033(0),Condition33(false),FT050(0),Condition50(false),FT067(0),Condition67(false) ;
FT033 = IntPortion((1200/Barinterval)*0.33) ;
FT050 = IntPortion((1200/Barinterval)*0.50) ;
FT067 = IntPortion((1200/Barinterval)*0.67) ;

Condition33 = (time = CalcTime(0000,FT033*BarInterVal)) ;
Condition50 = (time = CalcTime(0000,FT050*BarInterVal)) ;
Condition67 = (time = CalcTime(0000,FT067*BarInterVal)) ;

//多單部位 每日固定時間若收盤價小於前日低點 則平倉出場
   if MP > 0 and Condition67 and Close < LowD(1) then Sell ("LX_FT067_1") next bar at Low stop;

//多單部位 收盤價自雲層上跌落雲層下則平倉出場
   if MP > 0 and Close Cross under MinList(HighCloud,LowCloud) and Close Cross under MaxList(HighCloud,LowCloud) then 
      Sell ("LX_MoKu3") next bar at Market ;   

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






沒有留言:

張貼留言