2023年8月1日 星期二

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

EasyTrader TestNo 021

參考文章 不一樣的
MACD 交易模型 MACD BBand

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

//計算近期高低點
// ************ High/Low data *****************
BarPass = MaxList(HBar,LBar) ;
HLRange = Highest(High,BarPass)-Lowest(Low,BarPass) ;
if MP <> 0 then WinPoint = MaxPositionProfit/BigPointValue else WinPoint = 0 ;
HBarPos = HighestBar(High,HBar) ;
LBarPos = LowestBar(Low,LBar) ;

//計算 CDP 價格區間
// ************ CDP *****************
vars:CDP(0),AH(0),NH(0),NL(0),AL(0),CDPrange(0),CrossA(false),CrossB(false),TH1(0),TL1(0),TH2(0),TL2(0) ;
if Date <> Date[1] then begin
   CDP = (HighD(1)+LowD(1)+2*CloseD(1))/4;
   AH = CDP + (HighD(1) - LowD(1));
   NH = CDP*2 - LowD(1);
   NL = 2*CDP - HighD(1);
   AL = CDP - (HighD(1) - LowD(1));
end ; 
CDPrange = HighD(1) - LowD(1) ;

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

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

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = High ;
ShortPrice = (Highest(High,Lbar)+Lowest(Low,Lbar))*0.5 ;
   
// ********** Main Strategy *********
// ********** Entry Method
//計算以 MACD值為基礎的布林通道
vars: MacdType(1),AvgLen(8),SHB(1.25),SLB(1),HB(16),LB(28);
VARS:MacdBB(0),Avg(0),SDev(0),RatioLen(0),BBGap(0);
vars:UBuy(0),BuyStop(0),USell(0),SellStop(0),movAvg(0);

MacdType = Mod(NBarL,4)+1 ;
if MacdType = 1 then MacdBB = MACD(Close,12,26) 
   else if MacdType = 2 then MacdBB = uDif 
      else if MacdType = 3 then MacdBB = uMacd  
         else if MacdType = 4 then MacdBB = uOsc ; 
         
Avg = XAverage(MacdBB,NBarS); 
SDev = StdDev( MacdBB,NBarS) ; 
if FracA*0.5 >= 2 then SHB = 2 
   else if FracA*0.5 <= 0.5 then SHB = 0.5
      else SHB = FracA*0.5 ;
if FracB*0.5 >= 2 then SLB = 2 
   else if FracB*0.5 <= 0.5 then SLB = 0.5
      else SLB = FracB*0.5 ;
HB = HighBand ;
LB = LowBand ;
    
UBuy = Avg + SHB*SDev ; 
USell = Avg - SLB*SDev ; 
BBGap = UBuy - USell ;

   if TimeOK then begin

// MacdBB突破上通道 且 uDif > 0 及 uOsc > 0 則市價做多
   if MP <> 1 and MacdBB cross over UBuy and uDif > 0 and uOsc > 0 then 
      Buy ("LE_MacdBB_1T") next bar at Market ;

// MacdBB跌破下通道 且 uMacd < 0 及 uOsc < 0 則市價做空
   if MP <> -1 and MacdBB Cross under USell and uMacd < 0 and uOsc < 0  then 
      SellShort ("SE_MacdBB_1T") next bar at Market ;

//MacdBB突破下通道且近3日高點距離小於20根K棒 則收盤價突破設定價位做多
   if MP <> 1 and MacdBB cross over USell and DayHPB < LenA1 and Close < BuyPrice then 
      Buy ("LE_MacdBB_7T") next bar at BuyPrice Stop ;

//MacdBB跌破上通道且近3日低點距離小於20根K棒 則收盤價跌破設定價位做空
   if MP <> -1 and MacdBB Cross under UBuy and DayLPB < LenB1 and Close > ShortPrice then 
      SellShort ("SE_MacdBB_7T") 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 buytocover ("WrongEntryS6") next bar at Close stop ;

// ************* Rev CDP exit *************
// 多單部位 CDP區間範圍大於特定值 收盤價由上向下跌破 AH值 反手做空
   if MP > 0 and CDPrange > MaxList(89,PF*0.382) and Close > AH then 
      SellShort ("rLX_CDP") next bar at AH-Minlist(Range,HBarPos*MinMove/PriceScale) stop ;
// 空單部位 CDP區間範圍大於特定值 收盤價由下向上突破 AL值 反手做多
   if MP < 0 and CDPrange > MaxList(89,PF*0.382) and Close < AL then 
      Buy ("rSX_CDP") next bar at AL+Minlist(Range,LBarPos*MinMove/PriceScale) 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






沒有留言:

張貼留言