2023年12月3日 星期日

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

EasyTrader TestNo 028

參考文章 布林帶寬度的交易模型

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

// Parameter for all strategies use
vars:EntNum01(3.0),EntNum02(4.0),ExtNum01(24.0),ExtNum02(72.0),LenA1(20.0),LenB1(20.0),FracA(2.50),FracB(2.50),
HBar(20.0),LBar(20.0),TradeProfit(0.025),TradeStopLoss(0.015),NBarL(20.0),NBarS(20.0),LenA2(20.0),LenB2(20.0),
RatioL(2.50),RatioS(2.50),HighBand(20.0),LowBand(20.0),BuyMode(53),ShortMode(31),LE01(9),SE01(4);

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

//計算布林通道區間及價位
// ************ BollingerBand *****************
vars:BBUP(0),BBDN(0),BBRange(0),BBLen(0),DataBase(Close),DataType(1) ;
BBLen = MaxList(LenA2,LenB2) ;
DataType = IntPortion(BBLen/5) ;
if DataType < 1 or DataType > 4 then DataType = 1 ; 
if DataType = 3 then DataBase = Highest(High,BBLen)-Lowest(Low,BBLen) ;
   
BBUP = BollingerBand(DataBase,BBLen,2) ; 
BBDN = BollingerBand(DataBase,BBLen,-2) ;
BBRange = BBUP-BBDN ;

//計算均線價位
// ************* 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 = 近週低點 ;
HLWeek = WH-WL ;

//計算近日高低點區間及價位
// ************ for Day High/Low box *****************
Vars:DayLong(5),DayShort(5),DH(0),DL(0),HLDay(0),DCount(0),DBox33(0),DBox67(0),DBoxHPB(0),DBoxLPB(0) ;
DH = 近日高點;
DL = 近日低點 ;

if BarNumber = 1 then begin
   Buy this bar on Close ;
   Sell this bar on Close ;
end ;

// ************* Time Set Up *************
TimeOK = ((time >= 1200 and time <= 2345)) or (time >= 0300 and time <= 0500 ) ;

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

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = DH ;
ShortPrice = LowD(0)-OpenD(0)*RatioS/10000  ;

// ********** Main Strategy *********
// ********** Entry Method

   if EntryExitCond then begin
//均線多頭趨勢 且布林通道大於3根K棒前寬度 則收盤價突破設定價位做多
   if MP <> 1 and BBRange > BBRange[3]*MaxList(RatioL,1) and Avg7 > Avg21 and Avg21 > Avg60 and Close < BuyPrice then  Buy ("LE_BBMix_2") next bar at BuyPrice Stop ;

//均線空頭趨勢 且布林通道大於3根K棒前寬度 則收盤價跌破設定價位做空
   if MP <> -1 and BBRange > BBRange[3]*MaxList(RatioS,1) and Avg7 < Avg21 and Avg21 < Avg60 and Close > ShortPrice then  SellShort ("SE_BBMix_2") next bar at ShortPrice Stop ;
   end ;

   if TimeOK then begin
//均線多頭趨勢 且布林通道大於3根K棒前寬度 則收盤價突破設定價位做多
   if MP <> 1 and BBRange > BBRange[3]*MaxList(RatioL,1) and Avg7 > Avg21 and Avg21 > Avg60 and Close < BuyPrice then  Buy ("LE_BBMix_2T") next bar at BuyPrice Stop ;

//均線空頭趨勢 且布林通道大於3根K棒前寬度 則收盤價跌破設定價位做空
   if MP <> -1 and BBRange > BBRange[3]*MaxList(RatioS,1)and Avg7 < Avg21 and Avg21 < Avg60 and Close > ShortPrice then  SellShort ("SE_BBMix_2T") 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 sellshort ("WrongEntryL4") next bar at Close stop ;

//空單部位虧損大於預期 反手做多
   if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
      then buy ("WrongEntryS4") next bar at Close stop ;

// ************* Rev AV Turn Exit ************* 
var:Len(8),Level(55),LEcondition(False),SEcondition(False),TempH(99999),TempL(0),ACond(false),Vcond(false);


  ACond = A轉條件  ;
  if ACond then TempL = Low ;
  SEcondition = MRO(ACond,5,1) > -1 and Close > (TempL-MinList(Range,Len*MinMove/PriceScale)) and Close < Low[1] ; 

  VCond = V轉條件 ;
  if VCond then TempH = High ;
  LEcondition = MRO(VCond,5,1) > -1 and Close < (TempH+MinList(Range,Len*MinMove/PriceScale)) and Close > High[1] ; 
  
// 持有多單 符合 A轉條件 市價賣出做空
  if MP > 0 and BarsSinceEntry > 0 and SEcondition then SellShort ("rLX_ATurn") next bar at Market ;
// 持有空單 符合 V轉條件 市價買進做多
  if MP < 0 and BarsSinceEntry > 0 and LEcondition then Buy ("rSX_VTurn") next bar at Market ;


// ************* rev N Week trade Exit *************
// 持有空單 收盤價自近週高點下跌一段距離後 再次突破價位作多
   if MP < 0 and Close < WH-(HLWeek*FracA*0.1) then 
      Buy ("rLX_WTrade1") next bar at WH-(HLWeek*FracA*0.1)+Range Stop ;
// 持有多單 收盤價自近週低點上升一段距離後 再次跌破價位作空
   if MP > 0 and Close > WL+(HLWeek*FracB*0.1) then 
      SellShort ("rSX_WTrade1") next bar at WL+(HLWeek*FracB*0.1)-Range 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






沒有留言:

張貼留言