2014年6月11日 星期三

隨機相對強弱指標 Stoch RSI [程式碼]

EasyTrader ArtNo 163
以隨機指標KD結合相對強弱指標RSI可導出另一種短線振盪指標,稱為隨機相對強弱指標(Stoch Relative Strength Index, RSI),為1994年錢德(Tushar S. Chande)與可羅(Stanley Kroll)所提出,以計算RSI在9日內的能量變化,再從極端值找出超買區或超賣區,做為投資策略的參考。為顯示RSI的短線極端值,Stoch RSI公式如下:

A = RSI(Close,N) ;
Amax = Highest(A,M) ;
Amin = Lowest(A,M) ;
StochRSI =(A-Amin)/(Amax-Amin)*100 ;

StochRSI 計算出來的值也是介於 0 ~100之間 ,由於它結合了 KD指標的敏銳反應,因此StochRSI 可彌補傳統 RSI指標較為延滯的缺點。
由於這樣的概念,我從另一角度結合 RSI 與 KD ,就是將傳統 RSI的參數是收盤價 ,改以 SlowD的函數代替,用來比較在應用上有無差異

指標程式碼
inputs:SDLen(17),RSILen(7),HB(70),LB(30) ;
vars: RSID(0),StochRSI(0) ;

{ 傳統RSI 的修改版 }
RSID = RSI(SlowD(SDLen),RSILen) ;

{ 隨機相對強弱指標 }
Value1 = (Highest(RSI(Close,RSILen),SDLen)-Lowest(RSI(Close,RSILen),SDLen)) ;
if Value1 <> 0 then
StochRSI = (RSI(Close,RSILen)-Lowest(RSI(Close,RSILen),SDLen))/Value1*100 ;

Plot1(HB,"HB") ;
Plot2(LB,"LB") ;
Plot3(RSID,"RSID") ;
Plot4(Average(StochRSI,RSILen),"StochRSI") ;



台指期 30分K 留倉 最近 3000 交易日 ,交易成本 1200
StochRSI 測試



RSID 測試



兩者的表現其實都很接近,從績效曲線來看是有愈來愈不錯的表現!

[策略程式碼]
input:EntryType(1),ExitType(0),TradeProfit(0.05),TradeStopLoss(0.02) ;
vars: IsBalanceDay(False),MP(0),PF(0),PL(0);

input: SDLen(9),RSILen(5),HB(70),LB(30),ADXValue(10),ATRs_L(3),ATRs_S(3),HLRange(200),ATR_Len(20),ATR_Ratio(1.5);
vars: RSID(0),ATREn(0),UBuy(0),USell(0),StochRSI(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 ;

RSID = RSI(SlowD(SDLen),RSILen) ;

{ 高低差濾網 }
Value1 = Highest(High,RSILen)-Lowest(Low,RSILen) ;
Value2 = Highest(High,SDLen)-Lowest(Low,SDLen) ;

{ StochRSI 計算 }
Value3 = (Highest(RSI(Close,RSILen),SDLen)-Lowest(RSI(Close,RSILen),SDLen)) ;
if Value3 <> 0 then StochRSI = (RSI(Close,RSILen)-Lowest(RSI(Close,RSILen),SDLen))/Value3*100 ;

{ RSID 進場方式 }
if EntryType = 1 then Begin
   If Value2 > HLRange and ADX(SDLen) > ADXValue then Begin { 加兩個濾網確認 }
      if RSID > {Cross over} HB then Buy next bar at Market ;
      if RSID < {Cross under} LB then Sell next bar at Market ;
   end;
end;

{ StochRSI 進場方式 }
if EntryType = 2 then Begin
   If Value2 > HLRange and ADX(SDLen) > ADXValue then Begin
      if Average(StochRSI,RSILen) > {Cross over} HB then Buy next bar at Market ;
      if Average(StochRSI,RSILen) < {Cross under} LB then Sell next bar at Market ;
   end;
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 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 ;
MagicQS082

沒有留言:

張貼留言