2014年4月7日 星期一

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

EasyTrader ArtNo 133
請參照Wen大文章 VIX Stretched ETF交易策略 邏輯,轉為多空程式碼作台指歷史回測

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值都不錯

2 留言:

Unknown 提到...

板大 請問 data1 & data2 放什麼商品 周期

EasyTrader 提到...

data1為台指期,data2為台股Vix恐慌指數 日K

張貼留言

如果有私人問題想請教,請透過網站右方『與站長聯絡』之表單,謝謝!

----------------------------------------------------------------------------------------------------
網站聲明(Disclaimer)
本教學網站內所提供之程式碼(包括函數、指標、訊號)屬開放程式碼,用意在於讓使用者學習程式語法之撰寫,使用者可以任意修改語法內容並調整參數。本網站所有之內容(包括文章、影片、歷史紀錄、程式碼、教材)限用於個人學習使用,請勿轉寄、濫用,嚴禁私自串接帳戶交易。
-------------------------------------------------------------------------------------------------