2023年6月21日 星期三

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

EasyTrader TestNo 017

參考文章 最高價與最低價的交易策略

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

Avg60 = Average(Close,60) ;

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

//計算一目均衡表的區間價格
// ************ 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) ;

//計算前日波動範圍
// ************ 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 = True ;

// ************* Entry/Exit Condition Set Up *************
EntryExitCond = EntriesToday(Date)=0 ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = DH  ;
ShortPrice = DL ;
   
// ********** Main Strategy *********
// ********** Entry Method
vars:BuyPoint(0),SellPoint(99999),HLRed(false),HLGreen(false);

if date <> date[1] then begin
   HLRed = false ;
   HLGreen = false ;
   BuyPoint = 0 ;
   SellPoint = 99999 ;
end else begin
//多單進場條件 - 當日高低波動範圍大於當日開盤價一定比例且紅K
   HLRed = (HighD(0) - lowD(0)) >= MaxList((RatioS/500)*OpenD(0),100) and CloseD(0) - OpenD(0) > 0 ;
//空單進場條件 - 當日高低波動範圍大於當日開盤價一定比例且黑K
   HLGreen = (HighD(0) - lowD(0)) >= MaxList((RatioL/500)*OpenD(0),100) and CloseD(0) - OpenD(0) < 0 ;
   if HLGreen then BuyPoint = HighD(0) ;
   if HLRed then SellPoint = LowD(0);
end ;

   if EntryExitCond then begin
//符合多單進場條件收盤價突破設定價位作多
   if MP <> 1 and HLRed and Close < BuyPrice then 
      Buy ("LE_HLValue3") next bar at BuyPrice stop ;
//符合空單進場條件收盤價跌破設定價位作空
   if MP <> -1 and HLGreen and Close > ShortPrice then 
      SellShort ("SE_HLValue3") 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 ;


//持有空單前一日波動範圍小於150,K棒實體比例大於 0.65,收盤價突破設定價位反手做多
   if MP < 0 and VED < 150 and DBody > 0.65 and Close < BuyPrice then
      Buy ("rSX_VED1T") next bar at BuyPrice stop ;
//持有多單前一日波動範圍小於150,K棒實體比例大於 0.65,收盤價跌破設定價位反手做空
   if MP > 0 and VED < 150 and DBody > 0.65 and Close > ShortPrice then 
      SellShort ("rLX_VED1T") next bar at ShortPrice stop ;

//持有空單,收盤價突破雲層上界雲層上界大於60均線,突破當根K棒高點反手做多
   if MP < 0 and Close Cross over HighCloud and HighCloud > LowCloud and HighCloud > Avg60 then  Buy ("rSX_MoKu6") next bar at High Stop ;
/持有多單,收盤價跌破雲層下界雲層界小於60均線,跌破當根K棒低反手做空
   if MP > 0 and Close Cross under HighCloud and HighCloud < LowCloud and HighCloud < Avg60 then  SellShort ("rLX_MoKu6") 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





沒有留言:

張貼留言