2024年1月20日 星期六

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

EasyTrader TestNo 030

參考文章 【震盪+趨勢混合策略】恆溫器策略

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

//計算RSI值
// ************ RSI *****************
vars:vRSIA(0),vRSIB(0),DivRSICondA(false),DivRSICondB(false) ;
vRSIA = RSI(Close,Minlist(LenA1,LenB1)) ;
vRSIB = RSI(Close,Maxlist(LenA1,LenB1)) ;

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

// ************* Entry/Exit Condition Set Up *************
EntryExitCond = BarsSinceExit(1) > MinList(LenA1,LenB1) ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = ConvertLine ;
ShortPrice = Minlist(LowD(0),LowD(1),LowD(2))+Range*0.5 ;

// ********** Main Strategy *********
// ********** Entry Method
//計算恆溫器趨勢與震盪進場價格
Vars: bollingerLengths(50),trendLiqLength(50),numStdDevs(2),swingPrcnt1(0.50),swingPrcnt2(0.75),atrLength(10),swingTrendSwitch(20);
Vars:cmiVal(0),buyEasierDay(0),sellEasierDay(0),trendLokBuy(0),trendLokSell(0),keyOfDay(0),swingBuyPt(0),swingSellPt(0),trendBuyPt(0),trendSellPt(0),swingProtStop(0);

cmiVal = (AbsValue(Close - Close[LenA1-1])/(Highest(High,LenA1) -Lowest(Low,LenA1))*100);
buyEasierDay = 0;
sellEasierDay = 0;
trendLokBuy = Average(Low,LenB1);
trendLokSell = Average(High,LenB1);
keyOfDay = TypicalPrice;
if Close > Highest(keyOfDay,HBar) then sellEasierDay = 1;
if Close < Lowest(keyOfDay,LBar) then buyEasierDay = 1;

swingPrcnt1 = FracA ;
swingPrcnt2 = FracB ;

if(buyEasierDay = 1) then begin
   swingBuyPt = OpenD(0) + swingPrcnt1*AvgTrueRange(atrLength);
   swingSellPt = CloseD(1) - swingPrcnt2*AvgTrueRange(atrLength);
end;

if(sellEasierDay = 1) then begin
   swingBuyPt = CloseD(1) + swingPrcnt2*AvgTrueRange(atrLength);
   swingSellPt = OpenD(0) - swingPrcnt1*AvgTrueRange(atrLength);
end;

swingBuyPt = MaxList(swingBuyPt,trendLokBuy);
swingSellPt = MinList(swingSellPt,trendLokSell);
trendBuyPt = BollingerBand(Close,bollingerLengths,numStdDevs);
trendSellPt = BollingerBand(Close,bollingerLengths,numStdDevs);

   if EntryExitCond then begin

// CMI 值處於震盪狀態
      if(cmiVal < swingTrendSwitch) then begin

//RSI 值小於設定值 且收盤價小於買方震盪值 則突破震盪值做多
         if MP <> 1 and vRSIA < LowBand*4 and Close < swingBuyPt then Buy ("LE_SwB3") next bar at swingBuyPt stop;

//RSI 值大於設定值 且收盤價大於賣方震盪值 則跌破震盪值做空
         if MP <> -1 and vRSIB > HighBand*4 and Close > swingSellPt then SellShort ("SE_SwS3") next bar at swingSellPt stop;
      end ;
   end ;


   if EntryExitCond then begin
// CMI 值處於趨勢狀態
      if(cmiVal >= swingTrendSwitch) then begin

// 均線多頭排列 且收盤價小於買方趨勢價 則突破趨勢值做多
         if MP <> 1 and Avg7 > AVg21 and Avg21 > Avg60 and Close < trendBuyPt then Buy("LE_TB3") next bar at trendBuyPt stop;

// 均線空頭排列 且收盤價大於賣方趨勢價 則跌破趨勢值做空
         if MP <> -1 and Avg7 < AVg21 and Avg21 < Avg60 and Close > trendSellPt then SellShort("SE_TB3") next bar at trendSellPt stop;
      end;
   end ;
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 Sell ("WrongEntryL9") next bar at Close stop ;

//空單部位獲利小於預期 反手做多
   if MP < 0 and (BarsSinceEntry >= 300/Barinterval and MaxPositionprofit/currentcontracts < 5000) 
      then Buy ("WrongEntryS9") next bar at Close stop ;


// ************* Rev high/low Swing *************

//持有多單 且在盤整區高位 則跌破盤整區下緣 反手做空
    if MP > 0 and MRO(Close Cross over (Highest(High[1],34)+Lowest(Low[1],34))*0.5+6,34,4) > -1 then  Sellshort ("rLX_FixL") next bar at Lowest(Low[1],34)-TrueRange stop;

//持有空單 且在盤整區低位 則突破盤整區上緣 反手做多
    if MP < 0 and MRO(Close Cross under (Highest(High[1],34)+Lowest(Low[1],34))*0.5-6,34,4) > -1 then  Buy ("rSX_FixH") next bar at Highest(High[1],34)+TrueRange 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







沒有留言:

張貼留言