2023年10月1日 星期日

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

EasyTrader TestNo 025


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

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

// ************* Time Set Up *************
TimeOK = ((time >= 1200 and time <= 2345)) or (time >= 0300 and time <= 0500 ) ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = OpenD(0)+Range*0.5 ;
ShortPrice = OpenD(1)-AvgTrueRange(LenB1)*FracB ;

//多空趨勢設定
//*********** Trend filter setup
vars:UpTrendNo(1),DnTrendNo(1),UpTrend_High(false),UpTrend_Low(false),DnTrend_High(false),DnTrend_Low(false) ;
vars:Trend4Long(false),Trend4Short(false) ;

UpTrendNo = Mod(LenA1,5)+1 ;
DnTrendNo = Mod(LenB1,5)+1 ;

if  UpTrendNo = 2 then Trend4Long = 多方趨勢 ;
if  DnTrendNo = 1 then Trend4Short = 空方趨勢 ;
   
// ********** Main Strategy *********
// ********** Entry Method
vars:RangeHighR(0),RangeLowR(0),ChartRangeR(0),FibUpR(0),FibDnR(0);

//計算近期 RSI 高低區間及比例值
if BarNumber > LenB1 then begin
   RangeHighR = Highest( vRSIB,LenB1 ) ;
   RangeLowR = Lowest( vRSIB, LenB1 ) ;
   ChartRangeR = RangeHighR - RangeLowR ;
   FibUpR = (HighBand/30) * ChartRangeR + RangeLowR ;
   FibDnR = (LowBand/30) * ChartRangeR + RangeLowR ;
end ;

   if TimeOK then begin

// RSI 值大於設定值 且多方趨勢 則收盤價突破設定價位作多
   if MP <> 1 and vRSIB > FibUpR and Trend4Long and Close < BuyPrice then 
      Buy ("LE_RSI_2T") next bar at BuyPrice Stop ;

// RSI 值小於設定值 且空方趨勢 則收盤價跌破設定價位作空
   if MP <> -1 and vRSIB < FibDnR and Trend4Short and Close > ShortPrice then 
      SellShort ("SE_RSI_2T") 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 buy ("WrongEntryS8") next bar at Close stop ;


//持有空單 且近8根K有4根K的 RSI值大於65 則收盤價突破設定價位反手作多
   if MP < 0 and Countif(vRSIA > MaxList(HighBand*4,65),8) >= 4 and Close < BuyPrice then 
      Buy ("rSX_RSI3") next bar at BuyPrice stop ;
//持有多單 且近8根K有4根K的 RSI值小於35 則收盤價跌破設定價位反手作空
   if MP > 0 and Countif(vRSIB < MinList(LowBand*4,35),8) >= 4 and Close > ShortPrice then 
      SellShort ("rLX_RSI3") 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






沒有留言:

張貼留言