2023年9月16日 星期六

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

 EasyTrader TestNo 024

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

//計算 均線 值 
// ************* BreskThrough 3 Line *************
vars:Avg7(0),Avg13(0),Avg21(0),Avg60(0) ;
Avg7 = Average(Close,7) ;
Avg13 = Average(Close,13) ;
Avg21 = Average(Close,21) ;
Avg60 = Average(Close,60) ;

//計算 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 ;


// ************ for week High/Low box *****************
WH = 近週高點 ;
WL = 近週低點 ;

//計算 一目均衡表區間 值 
// ************ 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 >= 0900 and time <= 1200) or (time >= 2130) or time <= 0300) ;

// ************* Entry/Exit Condition Set Up *************
EntryExitCond = True ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = Maxlist(HighD(0),HighD(1),HighD(2))-Range*0.5 ;
ShortPrice = WL ;

//多空趨勢設定
//*********** 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
   if EntryExitCond then begin

// K值 向上交叉 D值 且多方趨勢 收盤價突破設定價位作多
   if MP <> 1 and vKA cross over vDA and Trend4Long and Close < BuyPrice then 
      Buy ("LE_KD_1") next bar at BuyPrice Stop ;

// K值 向下交叉 D值 且空方趨勢 收盤價跌破設定價位作空
   if MP <> -1 and vKA Cross under vDA and Trend4Short and Close > ShortPrice then 
      SellShort ("SE_KD_1") next bar at ShortPrice Stop ;
   end ;


   if TimeOK then begin
// K值與D值都大於設定值 且多方趨勢 收盤價突破設定價位作多
   if MP <> 1 and vKA > HighBand*4 and vDA > HighBand*4 and Trend4Long and Close < BuyPrice then  Buy ("LE_KD_3T") next bar at BuyPrice Stop ;

// K值與D值都小於設定值 且空方趨勢 收盤價跌破設定價位作空
   if MP <> -1 and vKA < LowBand*4 and vDA < LowBand*4 and Trend4Short and Close > ShortPrice then  SellShort ("SE_KD_3T") 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 Sell ("WrongEntryL10") next bar at Close stop ;
//空單部位虧損大於預期 反手做多
   if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
      then buy ("WrongEntryS10") next bar at Close stop ;

// ************* Rev 5 Day Avg Range *************
vars:Day5Range(0) ;
Day5Range = ((HighD(1)-LowD(1))+(HighD(2)-LowD(2))+(HighD(3)-LowD(3))+(HighD(4)-LowD(4))+(HighD(5)-LowD(5)))/5 ;

//多單部位進場經過5根K棒 且收盤價大於進場價+近五日波動範圍*1.618 則於近期低點反手做空
    if MP > 0 and Close > EntryPrice+Day5Range*1.618 and BarsSinceEntry >= 5 then
       Sellshort ("rLX_D5Range") next bar at Lowest(Low,LenA1) stop;

//空單部位進場經過5根K棒 且收盤價小於進場價-近五日波動範圍*1.618 則於近期高點反手做多
    if MP < 0 and Close < EntryPrice-Day5Range*1.618 and BarsSinceEntry >= 5 then
       Buy ("rSX_D5Range") next bar at Highest(High,LenB1) stop;

//空單部位且 收盤價向上交叉60均線 且收盤價大於藍色雲層高點 則在K棒高點反手做多
   if MP < 0 and Close Cross over Avg60 and HighCloud > LowCloud and Close > HighCloud then 
      Buy ("rSX_MoKu4") next bar at High Stop ;

//多單部位且 收盤價向下交叉60均線 且收盤價小於紅色雲層高點 則在K棒低點反手做空
   if MP > 0 and Close Cross under Avg60 and HighCloud < LowCloud and Close < HighCloud then 
      SellShort ("rLX_MoKu4") next bar at Low 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






沒有留言:

張貼留言