EMA と RSI トレンド トレーディング 戦略

作者: リン・ハーンチャオチャン, 日付: 2023-09-20 14:21:16
タグ:

概要

この戦略は,EMAとRSIインジケーターを組み合わせてトレンド指向を特定し,取引決定を行う.価格はEMAを超え,RSIは購入ポイント以下で,価格はEMAを下回り,RSIは販売ポイント以上になるとショートになります.また,トレンド取引のトレンド指向を決定するために,最後の2つのキャンドル終了間の関係を使用します.

戦略の論理

  1. 傾向線指標として 200 日間の EMA を計算します. EMA は価格変化に迅速に対応し,傾向の方向性を効果的に決定します.

  2. 14日間のRSIを計算すると,過剰購入と過剰販売の状況が判断されます.RSI50未満は過剰販売,50以上は過剰購入とみなされます.また,RSIの上昇または下落傾向を使用して,エントリーと出口タイミングを決定します.

  3. 最後の2つのキャンドルの閉じる間のサイズ関係を比較して,トレンド方向を決定します. 増加する最後の2つの閉じるが上昇傾向を示し,減少する最後の2つの閉じるが下落傾向を示します.

  4. 上昇傾向にあり,価格が200日間のEMAを超え,RSIが50を下回り上昇すると,買い信号が生成されます.

  5. ダウントレンドで価格が200日間のEMAを下回り,RSIが50を超えて下落すると,セールシグナルが生成されます.

  6. ATRと最後の14個のキャンドルの最高/最低価格は,ストップ・ロストと得益を計算するために使用されます.

  7. リスク管理のためのストップ戦略を採用する.

利点分析

  1. この2つの指標の組み合わせにより,トレンドの方向性を決定する精度が向上します.EMAは主要なトレンドを判断し,RSI&Candle関係はローカルトレンドとエントリー/エグジットのタイミングを判断します.

  2. RSIは誤ったブレイクを効果的に回避する.RSIの買い過ぎ/売過ぎ状態は,EMAの遅れによって引き起こされる不必要な取引を回避する.

  3. トレイリングストップは 時折の大幅波動による損失を効果的に制御します

  4. パラメータの組み合わせが最適化され 頑丈性が向上します

リスク分析

  1. EMAとRSIは,大きな幅の横向市場で誤った信号を生成する可能性があり,これを避けるべきです.

  2. ストップ損失が狭すぎると頻繁に停止する一方,ストップ損失が広すぎると損失を制御できない.ATRパラメータは適切に調整する必要があります.

  3. 突破後に引き下がる確率は高い.RSIパラメータは,見逃し傾向を避けるために緩やかであるべきである.

改善 の 方向

  1. ATRパラメータとストップ距離を調整して より良いストップ・ロスト・ポイントを見つけます

  2. EMAとRSIのパラメータを最適化して,より良いパラメータの組み合わせを見つけます.

  3. フィルタリングのための他の指標,MACD,ボリンジャー帯などを追加して,信号の精度を向上させる.

  4. 耐久性をさらに高めるため,異なる製品間のテストパラメータの違い.

  5. 特定の時間帯で戦略を無効にしようとします

概要

この戦略は,安定したリターン,最大引き下げ,シャープ比率でかなり安定している.パラメータ最適化とストップ損失調整によりさらに改善することができる.また,特定の市場状況下で間違った信号を注意し,補助指標または時間フィルターを通じてそれらを避ける必要があります.この戦略は,継続的な最適化によって長期的な安定した戦略になる可能性があります.


/*backtest
start: 2023-01-01 00:00:00
end: 2023-08-10 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
// strategy("EMA RSI Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=1)


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Author       : AJ Rupasinghege
// Date         : 06/11/2022
// Release      : v6.0
// Description  : If the last two closes are in ascending order, the rsi is below 50 and ascending, and the current candle is above 200 ema, then LONG. 
//                If the last two closes are in descending order, the rsi is above 50 and descending, and the current candle is below 200 ema, then SHORT. 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////// INPUTS //////////////////////////////////////////////////////////////

ema_length = input(200, "EMA Length")
rsi_buy_value = input(50, "RSI Buy Value")
rsi_sell_value = input(50, "RSI Sell Value")
show_data = input.bool(0, "Show Data")


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////// VARIABLES //////////////////////////////////////////////////////////

var stop_loss = 0.0
var last_trade_entry_price = 0.0
var low_value= 0.0
var atr = 0.0
var high_value = 0.0
var stop_loss_points = 0.0
var limit = 0.0
var bar_id_entry = 0


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////// FUNCTIONS //////////////////////////////////////////////////////////

getTradeConditionsLong() =>
    //@function         Used to calculate stop_loss, stop_loss points, limit and label values for long trades
    //@param direction  (float) // strategy.poistion.size
    //@returns          stop_loss, stop_loss_points, limit
    //@Dependancies     low_value, atr, last_trade_entry_price,bar_id_entry
    _stop_loss = low_value - atr
    _stop_lossPoints = (last_trade_entry_price - _stop_loss) *100000
    _limit = last_trade_entry_price + (last_trade_entry_price - low_value + atr) 
    value = "OpenValue: " + str.tostring(last_trade_entry_price) + 
         "\n OpenBarIndex: " + str.tostring(bar_id_entry) + 
         "\n LowValue: " + str.tostring(low_value) + 
         "\n atr: " + str.tostring(atr) + "\n stop_loss: " + 
         str.tostring(_stop_loss) + "\n Limit: " +
         str.tostring(_limit)

    [_stop_loss,_stop_lossPoints,_limit, value]

getTradeConditionsShort() =>
    //@function         Used to calculate stop_loss, stop_loss points, limit and label values for short trades
    //@param direction  (float) // strategy.poistion.size
    //@returns          stop_loss, stop_loss_points, limit
    //@Dependancies     high_value, atr, last_trade_entry_price,bar_id_entry
    _stop_loss = high_value + atr
    _stop_lossPoints = (_stop_loss  -last_trade_entry_price) * 100000
    _limit = last_trade_entry_price - (high_value - last_trade_entry_price + atr)
    value = "OpenValue: " + str.tostring(last_trade_entry_price) + 
         "\n OpenBarIndex: " + str.tostring(bar_id_entry) + 
         "\n HighValue: " + str.tostring(high_value) + 
         "\n atr: " + str.tostring(atr) + "\n stop_loss: " + 
         str.tostring(_stop_loss)  + "\n Limit: " +
         str.tostring(_limit)
    [_stop_loss,_stop_lossPoints,_limit, value]




/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////// SIGNALS //////////////////////////////////////////////////////////

ema = ta.ema(close,ema_length)
rsi = ta.rsi(close,14)

ema_buy_signal = ema < low
ema_sell_signal = ema > high


rsi_buy_signal = rsi < rsi_buy_value and rsi[1] < rsi[0]
rsi_sell_signal = rsi > rsi_sell_value and rsi[1] > rsi[0]

trend_buy_signal = close[2] < close[1] and close[1] < close[0]
trend_sell_signal = close[2] > close[1] and close[1] > close[0]

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////// TRADES //////////////////////////////////////////////////////////
long = trend_buy_signal 
         and ema_buy_signal 
         and rsi_buy_signal
short = trend_sell_signal 
         and ema_sell_signal  
         and rsi_sell_signal

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////// STRATEGY //////////////////////////////////////////////////////////



if long 
    strategy.entry("Long", strategy.long)

if short 
    strategy.entry("Short", strategy.short)
   

// Calculate Trade Entry Variables
last_trade_entry_price := strategy.opentrades.entry_price(strategy.opentrades - 1)
bar_id_entry := strategy.opentrades.entry_bar_index(strategy.opentrades - 1) 
atr := ta.atr(14) 
low_value := ta.lowest(14)
high_value := ta.highest(14)


// Exit Strategy for Long Positions 
if (strategy.position_size[1] != strategy.position_size and strategy.position_size>0)
    [_stop_loss,_stop_loss_points, _limit, value] = getTradeConditionsLong()
    stop_loss := _stop_loss
    stop_loss_points := _stop_loss_points
    limit := _limit
    

    if show_data
        label.new(bar_id_entry,stop_loss - 0.005,str.tostring(value),xloc = xloc.bar_index,yloc = yloc.price,style = label.style_none) 
    strategy.exit("Long Exit", "Long", trail_offset = stop_loss_points/2, trail_points = stop_loss_points/2 , stop = stop_loss , limit = limit   )


// Exit Strategy for Short Positions 
if (strategy.position_size[1] != strategy.position_size and strategy.position_size<0)
    [_stop_loss,_stop_loss_points, _limit, value] = getTradeConditionsShort()
    stop_loss := _stop_loss
    stop_loss_points := _stop_loss_points
    limit := _limit

    if show_data
        label.new(bar_id_entry,stop_loss + 0.005,str.tostring(value),xloc = xloc.bar_index,yloc = yloc.price,style = label.style_none) 
    strategy.exit("Short Exit", "Short", trail_offset = stop_loss_points/2, trail_points = stop_loss_points/2 , stop = stop_loss , limit = limit )


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////// PLOTS //////////////////////////////////////////////////////////

plot(ema, "SMA", color = color.blue, linewidth = 2 )


p1 = plot(strategy.position_size>0? stop_loss:na, style = plot.style_linebr , color = color.rgb(82, 240, 42) )
p2 = plot(strategy.position_size<0? stop_loss:na, style = plot.style_linebr , color = color.rgb(223, 85, 85) )
p3 = plot(strategy.position_size!=0?limit : na, style = plot.style_linebr , color = color.rgb(94, 85, 223, 100) )


fill(p1, p3, color = color.new(color.green, 90))
fill(p2, p3, color = color.new(#e98787, 90))

もっと