2023年7月1日 星期六

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

EasyTrader TestNo 018

參考文章 
最高價與最低價的另一個策略

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

//計算前日波動範圍
// ************ Volatility ExPansion System  *****************
vars: VED(1),DBody(1) ;
VED = HighD(1)-LowD(1) ;
if absvalue(CloseD(1)-OpenD(1)) <> 0 and VED <> 0 then DBody = absvalue(CloseD(1)-OpenD(1))/VED ;

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

// ************* Entry/Exit Condition Set Up *************
EntryExitCond = True ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = CloseD(1)+Range*0.5 ;
ShortPrice = (LowW(0)+LowM(0))*0.5-TrueRange ;
   
// ********** Main Strategy *********
// ********** Entry Method
vars:SQHigh(0),SQLow(0),LongRatio(0),ShortRatio(0),UpRatio(1),DnRatio(1) ;
vars:BuyPoint(0),SellPoint(99999),HLRed(false),HLGreen(false) ;

UpRatio = FracA/3 ;
DnRatio = FracB/3 ;

if HighD(1) > 0 then SQHigh = SquareRoot(HighD(1));
if LowD(1) > 0 then SQLow = SquareRoot(LowD(1));

if SQHigh <>0 and SQLow <>0 then begin
   LongRatio=SQHigh/SQLow;
   ShortRatio=SQLow/SQHigh;
end;

if date <> date[1] then begin
   HLRed = false ;
   HLGreen = false ;
   BuyPoint = 0 ;
   SellPoint = 99999 ;
end else begin
//多單進場條件 - 前日高低波動範圍大於前一日收盤價一定比例且收紅K
   HLRed = (HighD(1) - lowD(1)) >= MaxList((RatioS/500)*CloseD(1),100) and CloseD(1) - OpenD(1) > 0 ;
//空單進場條件 - 前日高低波動範圍大於前一日收盤價一定比例且收黑K
   HLGreen = (HighD(1) - lowD(1)) >= MaxList((RatioL/500)*CloseD(1),100) and CloseD(1) - OpenD(1) < 0 ;
   BuyPoint=LongRatio*CloseD(1)*upratio;
   SellPoint=ShortRatio*CloseD(1)*dnratio;
end ;


   if EntryExitCond then begin
//符合多單進場條件收盤價突破設定價位作多
   if MP <> 1 and HLRed and Close < BuyPrice then 
      Buy ("LE_HLValue3") next bar at BuyPrice stop ;
//符合空單進場條件收盤價跌破設定價位作空
   if MP <> -1 and HLGreen and Close > ShortPrice then 
      SellShort ("SE_HLValue3") 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 ("WrongEntryL12") next bar at Close stop ;
   if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
      then buytocover ("WrongEntryS12") next bar at Close stop ;


// ************* Rev 5 Day Avg Range *************
vars:Day5Range(0) ;
Day5Range = ((HighD(1)-LowD(1))+(HighD(2)-LowD(2))+(HighD(3)-LowD(3))+(HighD(4)-LowD(4))+(HighD(5)-LowD(5)))/5 ;

//持有多單且收盤價大於進場價格+五日波動均價*1.618,跌破近期低點反手做空
    if MP > 0 and Close > EntryPrice+Day5Range*1.618 and BarsSinceEntry >= 5 then
       Sellshort ("rLX_D5Range") next bar at Lowest(Low,LenA1) stop;
//持有空單且收盤價小於進場價格-五日波動均價*1.618,突破近期高點反手做多
    if MP < 0 and Close < EntryPrice-Day5Range*1.618 and BarsSinceEntry >= 5 then
       Buy ("rSX_D5Range") next bar at Highest(High,LenB1) stop;

//持有空單前一日波動範圍小於150,K棒實體比例大於 0.65,收盤價突破設定價位反手做多
   if MP < 0 and VED < 150 and DBody > 0.65 and Close < BuyPrice then
      Buy ("rSX_VED1T") next bar at BuyPrice stop ;
//持有多單前一日波動範圍小於150,K棒實體比例大於 0.65,收盤價跌破設定價位反手做空
   if MP > 0 and VED < 150 and DBody > 0.65 and Close > ShortPrice then 
      SellShort ("rLX_VED1T") next bar at ShortPrice 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






沒有留言:

張貼留言