2024年4月2日 星期二

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

EasyTrader TestNo 033

參考文章 一個外匯交易系統 - LUXOR
// 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) ;

//計算60根K棒區間價格
// ************Fix Length High or Low *****************
vars:FixLenH(High),FixLenL(Low),FixLenHL(0),FixLenHPB(0),FixLenLPB(0),FixLen33(0),FixLen67(0) ;
FixLenH = Highest(High,60) ;
FixLenL = Lowest(Low,60) ;
FixLenHL = FixLenH - FixLenL ;
FixLen33 = FixLenL+FixLenHL*0.33 ;
FixLen67 = FixLenL+FixLenHL*0.67 ;
FixLenHPB = _BarsLast(High = FixLenH) ;
FixLenLPB = _BarsLast(Low = FixLenL) ;

//計算近幾日區間價格
// ************ 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 >= 1000 and time <= 2100)) or (time >= 0300 and time <= 0500 ) ;

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

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = MaxList(OpenW(0),CloseW(0))+Range ;
ShortPrice = MinList(OpenW(0),CloseW(0))-Range ;
   
// ********** Main Strategy *********
// ********** Entry Method

vars:FastLength(5),SlowLength(30) ;
Vars: Fast(0),Slow(0),GoLong(False),GoShort(False),BuyEnt(0),ShortEnt(0) ;

FastLength = MinList(LenA1,LenB1) ;
SlowLength = MaxList(LenA1,LenB1) ;

//計算快速線與慢速線
Fast = TriAverage(Close,FastLength);
Slow = TriAverage(Close,SlowLength);

//判斷做多或做空
GoLong = Fast > Slow ;
GoShort = Fast < Slow  ;

//計算多空進場價格
If Fast crosses above Slow Then
BuyEnt = High + Range ;

If Fast crosses below Slow Then
ShortEnt = Low - Range ;

   if EntryExitCond then begin
// 收盤價在區間價2/3 位階之上 且自近60根高點拉回一段時間 突破多方進場價格做多
      if MP <> 1 and GoLong and Close > FixLen67 and FixLenHPB > LenB1 and Close < BuyEnt then 
         Buy ("LE_LUX3") next bar at BuyEnt stop;
// 收盤價在區間價1/3 位階之下 且自近60根低點反彈一段時間 跌破空方進場價格做空
      if MP <> -1 and GoShort and Close < FixLen33 and FixLenLPB > LenB1 and Close > ShortEnt then  SellShort ("SE_LUX3") next bar at ShortEnt stop;
   end ;

   if EntryExitCond then begin

//判斷做多 且最高價大於近日最高價 則突破多方進場價格做多
      if MP <> 1 and GoLong and High >= DH[1] and Close < BuyEnt then 
         Buy ("LE_LUX5") next bar at BuyEnt stop;

//判斷做空 且最低價小於近日最低價 則跌破空方進場價格做空
      if MP <> -1 and GoShort and Low <= DL[1] and Close > ShortEnt then 
         SellShort ("SE_LUX5") next bar at ShortEnt 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 Sell ("WrongEntryL2") next bar at Close stop ;
//空單部位虧損大於預期 平倉出場
   if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
      then buytocover ("WrongEntryS2") next bar at Close 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







沒有留言:

張貼留言