2014年6月25日 星期三

HMA移動平均線交易策略(續) [程式碼]


EasyTrader ArtNo 169
近期曾介紹隨機相對強弱指標 ,其中RSI的修改版是使用 SlowD替換,本篇介紹一個利用 HMA均線的RSI函數 - RSI(HMA( HMAPrice, HMALength ),RSILength) ,這是一個在 TS論壇 2010年所討論的主題,使用HMA 搭配 RSI 作策略,不過原作只有多單進場範例,我另外依樣畫葫蘆加上反向空單進場
多單進場邏輯
  • 四日HMA 均線轉折向上 
  • 收盤價在長期均線 MA(50) 之上 
  • RSI (HMA,N)小於 50 
  • 收盤價大於 59日前收盤價 
多單出場 
  • RSI (HMA,N)大於 90 
  • 低於進場價 5% 停損出場 
  • 高於進場價 15% 停利出場 
空單進場邏輯
  • 四日HMA 均線轉折向下 
  • 收盤價在長期均線 MA(50) 之下 
  • RSI (HMA,N)大於 50 
  • 收盤價小於 59日前收盤價 
空單出場 
  • RSI (HMA,N)小於 10 
  • 低於進場價 5% 停損出場 
  • 高於進場價 15% 停利出場 

策略程式碼 (藍字部份為另外自行加入的空單邏輯)
inputs: RSILength(9), HMALength(9), HMAPrice(Close), ExitPercent(15), StopLossPercent(5) ; 
vars: EP(0), MP(0), HullMA(0), HMAForTurnUp(0), RSIofHMA(0), HMATurnUp( false ),
HMATurnDn( false), ExitTarget(0), StopLoss(0),IsBalanceDay(False) ; 

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

HullMA = HMA( HMAPrice, HMALength ) ;
HMAForTurnUp = HMA( HMAPrice, 4 ) ;
RSIofHMA = RSI( HullMA, RSILength ) ;

{ HMA 轉折向上 }
HMATurnUp = CurrentBar > 2 and HMAForTurnUp > HMAForTurnUp[1] 
and HMAForTurnUp[1] <= HMAForTurnUp[2] ;

{ HMA 轉折向下 }
HMATurnDn = CurrentBar > 2 and HMAForTurnUp < HMAForTurnUp[1] 
and HMAForTurnUp[1] >= HMAForTurnUp[2] ;

{ 停損利計算 }
ExitTarget = Iff( MP = 1, EP * ( 1 + ExitPercent * 0.01 ), Close * ( 1 + ExitPercent * 0.01 ) ) ;
StopLoss = Iff( MP = 1, EP * ( 1 - StopLossPercent * 0.01 ), Close * ( 1 - StopLossPercent * 0.01 ) ) ;
{ Entry Long }
If Close > Average( Close, 50 ) and RSIofHMA <=50 and Close > Close[59] and
HMATurnUp and MP = 0 then Buy next bar market ;

{ EXITS }
{ RSI exit delayed for RSI calculation convergence }
If CurrentBar >= 10 * RSILength and RSIofHMA >= 90 then
ExitLong {Sell} ( "RSI LX" ) next bar market ;

{實測 Mark掉比較好
ExitLong {Sell} ( "Target LX" ) next bar at ExitTarget Limit ; { Profit Target }
ExitLong {Sell} ( "StopLoss LX" ) next bar at StopLoss Stop ; { Stop Loss }
}

{ Entry Short}
If Close < Average( Close, 50 ) and RSIofHMA > =50 and Close < Close[59] and
HMATurnDn and MP = 0 then Sell next bar market ;

{ EXITS }
{ RSI exit delayed for RSI calculation convergence }
If CurrentBar >= 10 * RSILength and RSIofHMA <= 10 then
ExitShort {BuytoCover} ( "RSI SX" ) next bar market ;


{ 實測 Mark掉比較好
ExitShort {BuytoCover} ( "Target SX" ) next bar at ExitTarget Limit ; { Profit Target }
ExitShort {BuytoCover} ( "StopLoss SX" ) next bar at StopLoss Stop ; { Stop Loss }
}

if IsBalanceDay then setExitonClose ;

台指期 日K 留倉 2004/5/31 ~ 2014/5/30 交易成本 1200



相當不錯的勝率,如果改用隨機參數搭配常用測試模組測試呢 ?



應用好的方式,能加速找到好的變數範圍 ~~
MagicQS091

沒有留言:

張貼留言