strategy · own pane · open source
RSI Mean Reversion
Running…
Loading chart…
About
Classic RSI mean reversion long-only strategy. Buy when RSI drops into oversold territory, exit when it recovers to the midline.
- Lower
Oversoldfor fewer, higher-conviction signals; raise it for more trades. Exit Levelaround 50 is the textbook mean; 55–60 holds trades longer.- Best in range-bound markets; combine with an ADX filter for trending ones.
Source code
//@version=5
strategy("RSI Mean Reversion", overlay=false, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.05)
len = input.int(14, "RSI Length", minval=2, maxval=50, step=1)
oversold = input.int(30, "Oversold", minval=5, maxval=45, step=1)
exitLevel = input.int(50, "Exit Level", minval=30, maxval=70, step=1)
stopPct = input.float(2, "Stop Loss %", minval=0.5, maxval=10, step=0.1)
tpPct = input.float(6, "Take Profit %", minval=0, maxval=20, step=0.1)
r = ta.rsi(close, len)
longSignal = strategy.position_size == 0 and r < oversold
exitSignal = strategy.position_size > 0 and r > exitLevel
if longSignal
strategy.entry("Long", strategy.long)
if strategy.position_size > 0
strategy.exit("Risk", stop=stopPct > 0 ? strategy.position_avg_price * (1 - stopPct / 100) : na, limit=tpPct > 0 ? strategy.position_avg_price * (1 + tpPct / 100) : na)
if exitSignal
strategy.close("Long", comment="RSI exit")
hline(70, "Overbought", color=color.gray, linestyle=hline.style_dashed)
hline(50, "Midline", color=color.gray, linestyle=hline.style_dotted)
hline(30, "Oversold", color=color.gray, linestyle=hline.style_dashed)
p_RSI = plot(r, "RSI", color=color.purple, linewidth=2)
plotshape(longSignal, "Long", style=shape.triangleup, location=location.belowbar, color=color.green, text="L")
plotshape(exitSignal, "Exit", style=shape.triangledown, location=location.abovebar, color=color.red, text="X")
alertcondition(longSignal, "RSI long entry", "RSI dropped below oversold")
alertcondition(exitSignal, "RSI exit", "RSI crossed above exit level")
No comments yet.
Sign in to comment.