2024年1月1日 星期一

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

EasyTrader TestNo 029

參考文章 線性回歸在交易策略的應用
// 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) ;
if MP <> MP[1] and MP > 0 then ExitL = EntryPrice-PL ;
if MP <> MP[1] and MP < 0 then ExitH = EntryPrice+PL ;

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 *************
vars:MoneyLoss(false),LossCount(false),MoneyProfit(false),ProfitCount(false),ProfitCond(false),LossCond(false),ExitLen(0) ;
if Totaltrades > 6 then begin
   MoneyLoss = positionprofit(1)+positionprofit(2) < -1*(Close*0.01)*200 ;
   LossCount = positionprofit(1) < 0 and positionprofit(2) < 0 and positionprofit(3) < 0 ;
   LossCond = MoneyLoss or LossCount ;
   MoneyProfit = positionprofit(1)+positionprofit(2) > (Close*0.02)*200 ;
   ProfitCount = positionprofit(1) > 0 and positionprofit(2) > 0 and positionprofit(3) > 0 ;
   ProfitCond = MoneyProfit or ProfitCount ;
end else begin
   LossCond = true ;
   ProfitCond = true ;
end ;   
ExitLen = iff(ProfitCond,1080/BarInterval,iff(LossCond,2160/BarInterval,MaxList(LenA1,LenB1))) ;

EntryExitCond = BarsSinceExit(1) > ExitLen  ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = Maxlist(HighD(0),HighD(1),HighD(2))-Range*0.5 ;
ShortPrice = HighD(0)+OpenD(0)*RatioS/10000 ;

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

//計算回歸均線值與信號線
vars:TriggerLine(0),SignalLine(0),TwoLineGap(0),UpTrend(false),DnTrend(false) ;

TriggerLine = LinearRegvalue(TypicalPrice,LenA1,0) ;
SignalLine = Xaverage(TriggerLine,LenB1) ;
TwoLineGap = TriggerLine - SignalLine ;

   if EntryExitCond then begin

//回歸均線值與信號線差值大於設定值 則收盤價突破設定價位做多
   if MP <> 1 and TwoLineGap > HighBand and Close < BuyPrice then 
      Buy ("LE_Linear_5") next bar at BuyPrice Stop ;

//回歸均線值與信號線差值小於設定值 則收盤價跌破設定價位做空
   if MP <> -1 and TwoLineGap < LowBand and Close > ShortPrice then 
      SellShort ("SE_Linear_5") 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 MaxPositionprofit/currentcontracts < 5000) 
      then Buytocover ("WrongEntryS5") next bar at Close stop ;


// ************* General Exit *************
// 持倉部位 依進場時間長短及不同獲利程度 平倉出場
   if WinPoint < PL and BarsSinceEntry < BarPass then begin 
if MP > 0 and Close > (EntryPrice-PL) then Sell ("LX_GE0") next bar EntryPrice-PL stop
else if MP < 0 and Close < (EntryPrice+PL) then BuytoCover ("SX_GE0") next bar EntryPrice+PL stop ; 
   end else 
   if WinPoint < PL and BarsSinceEntry >= BarPass then begin 
if MP > 0  and Close > maxlist(EntryPrice-PL ,Lowest(Low,BarPass)) then 
Sell ("LX_GE1") next bar maxlist(EntryPrice-PL ,Lowest(Low,BarPass)) stop
else if MP < 0 and Close < minlist(EntryPrice+PL ,Highest(High,BarPass)) then 
   BuytoCover ("SX_GE1") next bar minlist(EntryPrice+PL ,Highest(High,BarPass)) stop ; 
   end ; 

//結算日出場
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







7 則留言:

  1. ExitLen = iff(ProfitCond,1080/BarInterval,iff(LossCond,2160/BarInterval,MaxList(LenA1,LenB1))) ;關於這段程式,我的老師說太老舊,沒人再用,請問你使用的原因是甚麼?其實我沒有細問,因為我沒看懂

    回覆刪除
    回覆
    1. 所有的技術指標更老舊 只要能用依然有人使用
      此外 不曉得您的老師說的老舊是指甚麼? 好奇ㄟ !!

      刪除
    2. 請問您是在學學生嗎 ? 或是學校老師建議學生到我的部落格瀏覽 ? 因為這幾天部落格瀏覽量暴增 ^^ ! 如果是後者 麻煩代我向老師說謝謝喔!

      刪除
    3. 改成巢狀 if 寫法如下
      if ProfitCond then ExitLen = 1080/BarInterval
      else if LossCond then ExitLen = 2160/BarInterval
      else ExitLen = MaxList(LenA1,LenB1) ;

      刪除
  2. 作者已經移除這則留言。

    回覆刪除
  3. 哈哈我是在學學生,也確實是老師推薦這裡沒錯喔!也非常感謝您百忙中願意抽空回覆!!

    回覆刪除