2023年9月1日 星期五

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

EasyTrader TestNo 023
參考文章 KD 隨機指標交易策略
// 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) ;
 
//計算自訂 MACD 值   
// ************ Macd *****************
vars:vDif(0),vMacd(0),vOsc(0),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 ;

//計算近3日高低區間值 
// ************3 Day High or Low *****************
vars:DayH(High),DayL(Low),DayHL(0),DayHPB(0),DayLPB(0),Day33(0),Day67(0) ;
DayH = MaxList(HighD(0),HighD(1),HighD(2)) ;
DayL = MinList(LowD(0),LowD(1),LowD(2)) ;
DayHL = DayH - DayL ;
Day33 = DayL+DayHL*0.33 ;
Day67 = DayL+DayHL*0.67 ;
DayHPB = _BarsLast(High = DayH) ;
DayLPB = _BarsLast(Low = DayL) ;

//計算 KD 值 
// ************ KD *****************
vars:vKA(0),vKB(0),vDA(0),vDB(0),vJA(0),vJB(0),DivSlowKCondA(false),DivSlowKCondB(false) ;
vKA = SlowK(LenA2) ;
vDA = SlowD(LenA2) ;
vJA = 3*vDA-2*vKA ;
vKB = FastK(LenB2) ;
vDB = FastD(LenB2) ;
vJB = 3*vDB-2*vKB ;


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 *************
EntryExitCond = EntriesToday(Date)=0 ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = HighD(0) ;
ShortPrice = MinList(OpenM(0),CloseM(0))+Range ;

//多空趨勢設定
//*********** 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 = 5 then Trend4Short = 空方趨勢 ;
   
// ********** Main Strategy *********
// ********** Entry Method

// J 值大於設定值 且 近3日高點拉回一段時間 突破設定價位做多
   if EntryExitCond then begin
   if MP <> 1 and vJB > HighBand*4 and Trend4Long and DayHPB < LenA1 and Close < BuyPrice then 
      Buy ("LE_KD_7") next bar at BuyPrice Stop ;

// J 值小於設定值 且 趨勢收斂 且 近3日低點反彈一段時間 跌破設定價位做空
   if MP <> -1 and vJB < LowBand*4 and DayLPB < LenB1 and Trend4Short and Close > ShortPrice then 
      SellShort ("SE_KD_7") next bar at ShortPrice Stop ;
   end ;       

   if EntryExitCond then begin

//  KD 向上交叉 且 Dif 值與 Osc 值均向上 則於近期高點做多 
   if MP <> 1 and vKB Cross over vDB and Trend4Long and uDif > 0 and uOsc > uOsc[1] then 
      Buy ("LE_KD_10") next bar at Highest(High,HBar) stop ;

//  KD 向下交叉 且 趨勢收斂 且 Dif 值與 Osc 值均向下 則於近期低點做空
   if MP <> -1 and vKB Cross under vDB and Trend4Short and uDif < 0 and uOsc < uOsc[1] then 
      SellShort ("SE_KD_10") next bar at Lowest(Low,LBar) 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 Close 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






沒有留言:

張貼留言