2023年6月1日 星期四

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

EasyTrader TestNo 015

參考文章 CDP 逆勢操作系統

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

// ************ 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 ;
   
// ************ Macd *****************
//計算 MACD
vars:uDif(0),uMacd(0),uOsc(0),MacdLen(0) ;
MacdLen = MaxList(LenA1,LenB1) ;
if MacdLen > 12 then MacdLen = 12 ;
uDif = Macd((High+Low+Close*2)/4,IntPortion(MacdLen*1.3),MacdLen*3) ;
uMacd = Xaverage(uDif,MacdLen) ;
uOsc = uDif-uMacd ;

// ************ for Day High/Low box *****************
//計算近日高低點
Vars:DayLong(5),DayShort(5),DH(0),DL(0),HLDay(0),DCount(0) ;
DH = 近日高點;
DL = 近日低點 ;

// ************ Volatility ExPansion System  *****************
//計算前日波動量
vars: VED(1),DBody(1) ;
VED = HighD(1)-LowD(1) ;
if absvalue(CloseD(1)-OpenD(1)) <> 0 and VED <> 0 then DBody = absvalue(CloseD(1)-OpenD(1))/VED ;

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

// ************* Time Set Up *************
TimeOK = ((time >= 0900 and time <= 1300)) ;

// ************* 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 = OpenD(0)+MaxList(VED*FracA/6,21) ;
ShortPrice = DL ;
   
// ********** Main Strategy *********
   if TimeOK then begin
// 收盤價大於 NL 且 MACD 偏多 且柱狀圖上升 則突破特定價格 進場做多
   if MP <> 1 and Close > NL and uDif > 0 and uOsc > uOsc[1] and Close < BuyPrice then 
      Buy ("LE_CDP3T") next bar at BuyPrice stop ;

// 收盤價小於 NH 且 MACD 偏空 且柱狀圖下降 則跌破特定價格 進場做空
   if MP <> -1 and Close < NH and uDif < 0 and uOsc < uOsc[1] and Close > ShortPrice then 
      SellShort ("SE_CDP3T") next bar at ShortPrice stop ;
   end ;

   if EntryExitCond then begin

// 收盤價小於 NH 且 近3根K棒出現 跨越 NL/AL值 則突破 NH+真實區間 做多
   if MP <> 1 and Close < NH and MRO(CrossB,3,1) > -1 then 
      Buy ("LE_CDP7") next bar at NH+TrueRange stop ;

// 收盤價大於 NL  且 近3根K棒出現 跨越 NH/AH值 則跌破 NL-真實區間 做空
   if MP <> -1 and Close > NL and MRO(CrossA,3,1) > -1 then 
      SellShort ("SE_CDP7") next bar at NL-TrueRange 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 MaxPositionprofit/currentcontracts < 5000) 
      then buytocover ("WrongEntryS15") next bar at Close stop ;
   if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
      then buy ("WrongEntryS16") next bar at Close stop ;

// ************* no more high or low *************

//連續6根K棒破低 且6根K棒高低區間大於 100 則近期低點平倉
    if MP > 0 and countif(Low < Low[1],6) >= 6 and Highest(High,6)-Lowest(Low,6) > 100 then
       Sell ("LX_6Low") next bar at Lowest(Low,LBar) stop;

//連續6根K棒過高 且6根K棒高低區間大於 100 則近期高點平倉
    if MP < 0 and countif(High > High[1],6) >= 6 and Highest(High,6)-Lowest(Low,6) > 100 then
       BuytoCover ("SX_6High") next bar at Highest(High,HBar) 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






4 則留言:

  1. 版大請問 HighSinceEntry 是內建函數嗎? 我的MC好像沒這函數

    回覆刪除
    回覆
    1. HighSinceEntry / LowSinceEntry 是自訂函數用來計算進場後的高低點

      刪除