2024年6月21日 星期五

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

EasyTrader TestNo 039

參考文章 Larry Williams - 最大振盪值 GSV


// Public Variablevars: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) ;
if MP <> MP[1] and MP > 0 then ExitL = EntryPrice-PL ;
if MP <> MP[1] and MP < 0 then ExitH = EntryPrice+PL ;

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

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

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

// ************* Entry/Exit Condition Set Up *************
SelectNo = IntPortion(RatioL+0.6) ;
if SelectNo < 1 or SelectNo > 5 then SelectNo = 1 ;
if SelectNo = 5 then EntryExitCond = True ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = HighD(0)-OpenD(0)*RatioL/10000  ;
ShortPrice = OpenD(1)-Range*0.5 ;

// ********** Main Strategy *********
// ********** Entry Method
//計算多空振盪值
vars:GSVLong(0),GSVShort(0),CondGSVL(false),CondGSVS(false) ;

GSVLong = Average(High[1]-Open[1],LenA1) ;
GSVShort = Average(Open[1]-Low[1],LenB1) ;

CondGSVL = Close < Close[LenA1] ;
CondGSVS = Close > Close[LenB1] ;

   if EntryExitCond then begin
//多方條件成立 且收盤價大於近60根K區間價2/3 則收盤價突破次根K棒開盤價+振盪值做多
      if MP <> 1 and CondGSVL and Close > FixLen67 and FixLenHPB > LenB1 and Close < Open next bar+GSVLong then  Buy ("LE_GSV3") next bar at Open next bar+GSVLong stop;

//空方條件成立 且收盤價小於近60根K區間價12/3 則收盤價跌破次根K棒開盤價-振盪值做空
      if MP <> -1 and CondGSVS and Close < FixLen33 and FixLenLPB > LenB1 and Close > Open next bar-GSVShort then  SellShort ("SE_GSV3") next bar at Open next bar-GSVShort stop;
   end ;


//基本停利+移動停損出場
// ************* Base Exit *************
if MP > 0 then Sell ("PL1_"+NumtoStr(PL,0)) next bar at HighSinceEntry-MinList(PL,PF) 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+MinList(PL,PF) 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 Sellshort ("WrongEntryL3") next bar at Close stop ;

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


// ************* no more high or low *************
//持有多單且自高點拉回一段時間 則於近期低點平倉出場
   if MP > 0 and HLRange > PL and HBarPos > HBar*0.7 then 
       Sell ("LX_NoHigh") next bar at Lowest(Low,LBar) stop;
//持有空單且自低點反彈一段時間 則於近期高點平倉出場
   if MP < 0 and HLRange > PL and LBarPos > LBar*0.7 then 
       BuytoCover ("SX_NoLow") next bar at Highest(High,HBar) stop;

// ************* Rev High Low SinceEntry *************
//多單進場過3根K棒後 收盤價小於21均線 自高點拉回一段距離反手做空
    if MP > 0 and Close < Avg21 and BarsSinceEntry >= 3 and Close > HighSinceEntry-OpenD(0)*0.01 then  Sellshort ("rLX_HLEntry") next bar at HighSinceEntry-OpenD(0)*0.01 stop;

//空單進場過3根K棒後 收盤價大於21均線 自低點反彈一段距離反手做多
    if MP < 0 and Close > Avg21 and BarsSinceEntry >= 3 and Close < LowSinceEntry+OpenD(0)*0.01 then  Buy ("rSX_HLEntry") next bar at LowSinceEntry+OpenD(0)*0.01 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






沒有留言:

張貼留言