EMA 및 RSI 트렌드 거래 전략

저자:차오장, 날짜: 2023-09-20 14:21:16
태그:

전반적인 설명

이 전략은 트렌드 방향을 파악하고 거래 결정을 내리기 위해 EMA와 RSI 지표를 결합합니다. 가격이 EMA보다 높고 RSI가 구매 지점보다 낮을 때 길게 이동하고 가격이 EMA보다 낮고 RSI가 판매 지점보다 높을 때 짧게 이동합니다. 또한 트렌드 거래를위한 트렌드 방향을 결정하기 위해 마지막 두 촛불 폐쇄 사이의 관계를 사용합니다.

전략 논리

  1. 트렌드 라인 지표로 200일 EMA를 계산합니다. EMA는 가격 변화에 빠르게 반응하고 트렌드 방향을 효과적으로 결정합니다.

  2. 14일 RSI를 계산하여 과소매와 과소매 조건을 판단합니다. RSI 50 이하는 과소매로 간주되며, 50 이상은 과소매로 간주됩니다. 또한 RSI의 상승 또는 하락 추세를 사용하여 입출 및 출출 시기를 결정합니다.

  3. 트렌드 방향을 결정하기 위해 마지막 두 촛불 폐쇄 사이의 크기 관계를 비교하십시오. 증가하는 마지막 두 개의 폐쇄는 상승 추세를 나타내고 감소하는 마지막 두 개의 폐쇄는 하락 추세를 나타냅니다.

  4. 상승 추세에 있는 경우, 200일 EMA 이상의 가격, 50 이하의 RSI와 상승 시 구매 신호가 생성됩니다.

  5. 하락 추세에서 200일 EMA 이하의 가격과 50 이상의 RSI가 떨어지면 판매 신호가 생성됩니다.

  6. ATR 및 마지막 14개의 촛불의 최고/최저 가격은 스톱 로스를 계산하고 이윤을 취하기 위해 사용됩니다.

  7. 리스크 관리에 대한 트레일링 스톱 전략을 채택합니다.

이점 분석

  1. 두 가지 지표의 조합은 트렌드 방향을 결정하는 정확성을 향상시킵니다. 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))

더 많은