2014年4月7日 星期一

VIX Stretched ETF交易策略(續) [程式碼]

EasyTrader ArtNo 133
請參照Wen大文章 VIX Stretched ETF交易策略 

進場邏輯:
1. 當SPY (S&P500 ETF) 在200日移動平均線之上。
2. 此時VIX指數距離10日移動平均線超過5%或連續3天站上,就進場買進。
3. 使用移動停利。(原作上是寫說當RSI漲超過65就找點出場,勝率>80%)
轉為多空程式碼作台指歷史回測
inputs:ExitType(1),AvgLenL(200),VixLenL(10),BiasL(5),BarLastL(3),TradeProfit(0.05),TradeStopLoss(0.03),NBarL(2),NBarS(2);
inputs:AvgLenS(200),VixLenS(10),BiasS(5),BarLastS(3);
vars: IsBalanceDay(False),MP(0),PF(0),PL(0);
Vars: Vix(0),VixAvgL(0),VixBiasL(0),VixAvgS(0),VixBiasS(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 ;

{台股 VIX }
Vix = Close of Data2 ;

{計算多方 VIX 均線及乖離參考基準}
VixAvgL = Average(Vix,VixLenL) ;
if VixAvgL > 0 then VixBiasL = (Vix/VixAvgL-1)*100 ;

{計算空方 VIX 均線及乖離參考基準}
VixAvgS = Average(Vix,VixLenS) ;
if VixAvgS > 0 then VixBiasS = (Vix/VixAvgS-1)*100 ;

{多方進場條件}
if Close > Average(Close, AvgLenL) and Countif(VixBiasL >= BiasL, BarLastL) = BarLastL
then Buy this bar on Close ;

{空方進場條件}
if Close < Average(Close, AvgLenS) and Countif(VixBiasS <= -BiasS, BarLastS) = BarLastS
then Sell this bar on Close ;

{不同出場條件}
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 Sell {ExitLong} next bar at Market ;
   if MP < 0 and BarsSinceEntry = NBarS then Buy {ExitShort} next bar at Market ;
end;

if ExitType = 5 then Begin
   Inputs: ATRs_L(3);
   Variables: PosHigh(0), ATRVal_L(0);
   ATRVal_L = AvgTrueRange(10) * ATRs_L;
   If BarsSinceEntry = 0 Then PosHigh = High;
   If MarketPosition = 1 Then Begin
      If High > PosHigh Then PosHigh = High;
     ExitLong ("ATR") Next Bar at PosHigh - ATRVal_L Stop;
  End else ExitLong ("ATR eb") Next bar at High - ATRVal_L Stop;
 
   Inputs: ATRs_S(3);
   Variables: PosLow(0), ATRVal_S(0);
   ATRVal_S = AvgTrueRange(10) * ATRs_S;
   If BarsSinceEntry = 0 Then PosLow = Low;
   If MarketPosition = -1 Then Begin
      If Low < PosLow Then PosLow = Low;
      ExitShort ("ATR_1") Next Bar at PosLow + ATRVal_S Stop;
  End else ExitShort ("ATR_1 eb") Next bar at Low + ATRVal_S Stop;
end;

if ExitType = 6 then Begin
   inputs: RSILen_LX(2),RSILen_SX(2),UPBand(65),DnBand(35) ;
   if MP > 0 and RSI(Close,RSILen_LX) > UpBand then ExitLong next bar at Market ;
   if MP < 0 and RSI(Close,RSILen_SX) < DnBand then ExitShort next bar at Market ;
end;

if IsBalanceDay then setExitonClose ;

台指期 日K 2004/3/31~2014/3/31 交易成本 1200
出場條件 2 [停利+停損]



出場條件 3 [N 根 Bar離場]



出場條件 5 [ATR移動停損利]



出場條件 6 [RSI 離場]


若應用在台灣50,結果如下
出場條件 2 [停利+停損]



出場條件 6 [RSI 離場]



勝率與 PF值都不錯

沒有留言:

張貼留言