2014年3月26日 星期三

線性回歸在交易策略的應用 (續) [程式碼]

EasyTrader ArtNo 126
在上一篇我們利用了 TraiggerLine 與 SignalLine 之間的差距作為交易策略的元素, 而從指標的柱狀副圖上,也發現它跟 MACD 的圖有一些類似 ,因此也可以利用柱形的變化來作策略開發(本範例為利用柱子上升或下降的次數),回測績效如下圖


接下來將兩篇文章的元素結合測試



看起來是有機會作更好的調整

測試程式碼( 台指期 60 min K 留倉策略 2004/1~2014/2 交易成本 1200 )
inputs: EntryType(1),ExitType(1),TradeProfit(0.05),TradeStopLoss(0.03),NBarL(2),NBarS(2);
input: BarNo(3),AvgLen(9),HighBand(90),LowBand(10);
inputs:LRlength(20),EMAlength(8),TargetBar(0);
vars:TriggerLine(0),SignalLine(0),TwoLineGap(0),UpTrend(false),DnTrend(false) ;
vars: IsBalanceDay(False),MP(0),PF(0),PL(0);

MP = MarketPosition ;

if DAYofMonth(Date) > 14 and DAYofMonth(Date) < 22 and DAYofWeek(Date)= 3 then isBalanceDay = True else isBalanceDay =False ;

PF = AvgPrice*TradeProfit ;
PL = AvgPrice*TradeStopLoss ;

TriggerLine = LinearRegvalue(TypicalPrice,LRlength,TargetBar) ;
SignalLine = Xaverage( TriggerLine , EMAlength ) ;
TwoLineGap = TriggerLine - SignalLine ;

{計算柱圖上升次數 , 假設 AvgLen = 5 ,表示 5 根 Bar 內要有 4次以上大於前一根柱 ,且最近一根 是大於前一根柱 }

UpTrend = Countif(TwoLineGap > TwoLineGap[1],AvgLen) >= AvgLen-1 and TwoLineGap > TwoLineGap[1];

{計算柱圖下降次數 , 假設 AvgLen = 5 ,表示 5 根 Bar 內要有 4次以上小於前一根柱 ,且最近一根 是小於前一根柱 }

DnTrend = Countif(TwoLineGap < TwoLineGap[1],AvgLen) >= AvgLen-1 and TwoLineGap < TwoLineGap[1];

if EntryType = 1 then Begin
   if TwoLineGap > HighBand then Buy next bar at Highest(High,BarNo) stop ;
   if TwoLineGap < LowBand then Sell next bar at Lowest(Low,BarNo) stop ;
end;

{柱圖上升/下降次數}

if EntryType = 2 then Begin
   if MP = 0 and UpTrend then Buy next bar at Highest(High,BarNo) stop ;
   if MP = 0 and DnTrend then Sell next bar at Lowest(Low,BarNo) stop ;
end;

{兩種元素結合}

if EntryType = 3 then Begin
   if MP = 0 and (UpTrend or TwoLineGap > HighBand) then Buy next bar at Highest(High,BarNo) stop ;
   if MP = 0 and (DnTrend or TwoLineGap < LowBand) then Sell next bar at Lowest(Low,BarNo) stop ;
end;

if ExitType = 1 then SetStopLoss(PL * BigPointValue) ;

if ExitType = 2 then Begin
   SetStopLoss(PL * BigPointValue) ;
  SetProfitTarget(PF * BigPointValue) ;
end;

if ExitType = 3 then Begin
   if MP > 0 and BarsSinceEntry = NBarL then ExitLong next bar at Market ;
   if MP < 0 and BarsSinceEntry = NBarS then ExitShort next bar at Market ;
end;

if ExitType = 4 then Begin
   SetStopLoss(PL * BigPointValue) ;
   SetProfitTarget(PF * BigPointValue) ;
   if MP > 0 and BarsSinceEntry = NBarL then ExitLong next bar at Market ;
   if MP < 0 and BarsSinceEntry = NBarS then ExitShort next bar at Market ;
end;

if IsBalanceDay then setExitonClose ;

加入如意多空網的圖測試結果



主要在績效上升的同時 , 交易次數與 MDD下降
MagicQS048

沒有留言:

張貼留言