2023年10月15日 星期日

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

EasyTrader TestNo 026

參考文章  Larry Williams - 價格的循環型態

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

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

//計算3週高低點區間價
// ************3 Week High or Low *****************
vars:WeekH(High),WeekL(Low),WeekHL(0),WeekHPB(0),WeekLPB(0),Week33(0),Week67(0) ;
WeekH = MaxList(HighW(0),HighW(1),HighW(2)) ;
WeekL = MinList(LowW(0),LowW(1),LowW(2)) ;
WeekHL = WeekH - WeekL ;
Week33 = WeekL+WeekHL*0.33 ;
Week67 = WeekL+WeekHL*0.67 ;
WeekHPB = _BarsLast(High = WeekH) ;
WeekLPB = _BarsLast(Low = WeekL) ;

//計算均線價
// ************* 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) ;

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

if DataCompression > 1 then begin
   Condition1 = Close > Open and Open > Close[1] ;
   Condition2 = Close < Open and Open < Close[1] ;
end else begin
   Condition1 = Close > OpenD(0) and OpenD(0) > CloseD(1) ;
   Condition2 = Close < OpenD(0) and OpenD(0) < CloseD(1) ;
end;

// ************* Time Set Up *************
TimeOK = ((time >= 0900 and time <= 1200) or (time >= 2130)) ;

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

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = WH-TrueRange ;
ShortPrice = WL  ;

// ********** Main Strategy *********
// ********** Entry Method
vars:TRratioA(0),TRratioB(0),RA(0),RB(0) ;

RA = FracA+1 ;
RB = FracB+1 ;
  
//計算短期/長期真實區間比值
if AvgTrueRange(IntPortion(LenA1*RA)) <> 0 then TRratioA = AvgTrueRange(LenA1)/AvgTrueRange(IntPortion(LenA1*RA)) ;
if AvgTrueRange(IntPortion(LenB1*RB)) <> 0 then TRratioB = AvgTrueRange(LenB1)/AvgTrueRange(IntPortion(LenB1*RB)) ;

   if EntryExitCond then begin

// 真實區間比例大於設定值 且收盤價偏多 則收盤價突破設定價格做多
   if MP <> 1 and TRratioA > RatioL and Condition1 and Close < BuyPrice then 
      Buy ("LE_ATR_1") next bar at BuyPrice Stop ;

// 真實區間比例大於設定值 且收盤價偏空 則收盤價跌破設定價格做空
   if MP <> -1 and TRratioB > RatioS and Condition2 and Close > ShortPrice then 
      SellShort ("SE_ATR_1") next bar at ShortPrice Stop ;
   end ;


   if EntryExitCond then begin

// 真實區間比例小於設定值 且自近3日高點拉回一段時間 則收盤價突破設定價格做多
   if MP <> 1 and TRratioA < RatioL and DayHPB < LenA1 and Close < BuyPrice then 
      Buy ("LE_ATR_8") next bar at BuyPrice Stop ;

// 真實區間比例小於設定值 且自近3日低點反彈一段時間 則收盤價跌破設定價格做空
   if MP <> -1 and TRratioB < RatioS and DayLPB < LenB1 and Close > ShortPrice then 
      SellShort ("SE_ATR_8") 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 ;

// ************* WeeK high or low *************

//多單部位進場價格大於3週區間價2/3 之上 則收盤價跌破近3週低點 平倉
    if MP > 0 and EntryPrice > WeekL+WeekHL*0.67 and Close > WeekL then
       Sell ("LX_WeekL") next bar at WeekL stop;

//空單部位進場價格小於3週區間價1/3 之下 則收盤價突破近3週高點 平倉
    if MP < 0 and EntryPrice < WeekL+WeekHL*0.33 and Close < WeekH  then
       BuytoCover ("SX_WeekH") next bar at WeekH stop;

//空單部位 最低價向上突破紅色雲層高點 且大於60均線 則反手市價買進做多
   if MP < 0 and Low Cross over HighCloud and HighCloud > LowCloud and HighCloud > Avg60 then 
      Buy ("rSX_MoKu7") next bar at Market ;

//多單部位 最高價向下跌破藍色雲層低點 且小於60均線 則反手市價賣出做空
   if MP > 0 and High Cross under HighCloud and HighCloud < LowCloud and HighCloud < Avg60 then  SellShort ("rLX_MoKu7") next bar at Market ;   

//結算日出場
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






沒有留言:

張貼留言