2023年11月1日 星期三

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

EasyTrader TestNo 027

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

//計算近日高低點區間及價位
// ************ for Day High/Low box *****************
Vars:DayLong(5),DayShort(5),DH(0),DL(0),HLDay(0),DCount(0),DBox33(0),DBox67(0),DBoxHPB(0),DBoxLPB(0) ;
DH = 近日高點;
DL = 近日低點 ;

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

// ************* Time Set Up *************
TimeOK = ((time >= 1000 and time <= 2100)) 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 = HighW(0)+MaxList(TrueRange*RatioL/10,6) ;
ShortPrice = HighM(0)-MaxList(TrueRange*RatioL/10,6) ;

//判斷多空趨勢
//*********** 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 = 3 then Trend4Long = 多方趨勢 ;
if DnTrendNo = 1 then Trend4Short = 空方趨勢  ;
   
// ********** Main Strategy *********
// ********** Entry Method
vars:DMIupA(0),DMIdnA(0),ADX_A(0),DMIupB(0),DMIdnB(0),ADX_B(0) ;

//計算DMI/ADX值
DMIupA = DMIPlus(LenA1) ;
DMIdnA = DMIMinus(LenA1) ;
ADX_A = ADX(LenA1) ;
DMIupB = DMIPlus(LenB1) ;
DMIdnB = DMIMinus(LenB1) ;
ADX_B = ADX(LenB1) ;


   if EntryExitCond then begin
//連續3根 DI+ 大於 DI- 則收盤價突破設定價位做多
   if MP <> 1 and Countif(DMIupA > DMIDnA,3) = 3 and Trend4Long and Close < BuyPrice then 
      Buy ("LE_DMI_9") next bar at BuyPrice Stop ;

//連續3根 DI+ 小於 DI- 且空方趨勢 則收盤價跌破設定價位做空
   if MP <> -1 and Countif(DMIupB < DMIDnB,3) = 3 and Trend4Short and Close > ShortPrice then 
      SellShort ("SE_DMI_9") next bar at ShortPrice Stop ;
   end ;


//基本停利+移動停損出場
// ************* Base Exit *************
if MP > 0 then Sell ("Trail_PL1_"+NumtoStr(Absvalue(EntryPrice-(HighSinceEntry-MinList(PL,PF))),2))  next bar at HighSinceEntry-MinList(PL,PF) stop ;
if MP > 0 then Sell ("PF1_"+NumtoStr(PF,0)) All Contracts next bar at EntryPrice+PF limit ;
if MP < 0 then BuytoCover ("Trail_PL2_"+NumtoStr(Absvalue(EntryPrice-(LowSinceEntry+MinList(PL,PF))),2))  next bar at LowSinceEntry+MinList(PL,PF) stop ;
if MP < 0 then BuytoCover ("PF2_"+NumtoStr(PF,0)) All Contracts next bar at EntryPrice-PF limit ;

//多單部位獲利小於預期 平倉出場
   if MP > 0 and (BarsSinceEntry >= 300/Barinterval and MaxPositionprofit/currentcontracts < 5000) 
      then sell ("WrongEntryL15") next bar at Close stop ;

//多單部位虧損大於預期 反手做空
   if MP > 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
      then sellshort ("WrongEntryL16") next bar at Close stop ;

// ************* rev N Day H/L Exit *************
// 持有空單 突破近日高點 反手做多
   if MP < 0 and Close < DH then buy ("rLX_DTrade2") next bar at DH Stop ;
// 持有多單 突破近日高點 反手做空
   if MP > 0 and Close > DL then sellshort ("rSX_DTrade2") next bar at DL 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






沒有留言:

張貼留言