This strategy executes mean-reversion trades when the Relative Strength Index rebounds from overbought or oversold zones in the direction of a higher-period trend EMA.
1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.
Source Code
53 lines of Pine Script v5 from strategies/rsi_mean_reversion.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.
rsi_mean_reversion.pine
// This Pine Script v5 strategy is licensed under the MIT License
// Tested with Backtrex (https://backtrex.com) on 10 years of M1 data
// Anti-repainting: all references use close[1]
//@version=5
strategy("RSI Mean Reversion", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=2)
// Inputs
rsiLen = input.int(14, "RSI Length")
rsiOversold = input.int(30, "RSI Oversold Level")
rsiOverbought = input.int(70, "RSI Overbought Level")
trendLen = input.int(100, "Trend EMA Length")
exitLen = input.int(21, "Exit EMA Length")
maxSLPips = input.float(40, "Max Stop Loss (pips)")
// Indicators (using close[1] to prevent repainting)
rsi = ta.rsi(close[1], rsiLen)
rsiPrev = ta.rsi(close[2], rsiLen)
trendEMA = ta.ema(close[1], trendLen)
exitEMA = ta.ema(close[1], exitLen)
// Pip value
pipValue = syminfo.mintick * 10
// Entry signals
longSignal = rsiPrev < rsiOversold and rsi >= rsiOversold and close[1] > trendEMA
shortSignal = rsiPrev > rsiOverbought and rsi <= rsiOverbought and close[1] < trendEMA
// Find recent swing low/high for stop loss
swingLow = ta.lowest(low[1], 10)
swingHigh = ta.highest(high[1], 10)
// Execute trades
if longSignal
sl = math.max(swingLow, close - maxSLPips * pipValue)
strategy.entry("Long", strategy.long)
strategy.exit("Long SL", "Long", stop=sl)
if shortSignal
sl = math.min(swingHigh, close + maxSLPips * pipValue)
strategy.entry("Short", strategy.short)
strategy.exit("Short SL", "Short", stop=sl)
// Exit at EMA 21 touch
if strategy.position_size > 0 and close[1] >= exitEMA
strategy.close("Long", comment="EMA Exit")
if strategy.position_size < 0 and close[1] <= exitEMA
strategy.close("Short", comment="EMA Exit")
// Plots
plot(trendEMA, "Trend EMA 100", color=color.gray, linewidth=2)
plot(exitEMA, "Exit EMA 21", color=color.orange)
Overview
RSI Mean Reversion calculates indicators using confirmed bar closes to prevent live-bar recalculation. Entries trigger on RSI threshold re-crosses aligned with the trend EMA. Positions use a stop loss set at the recent 10-bar swing high or low capped by a maximum pip distance, and exit when the price reaches an exit EMA. Moving averages are plotted on the main chart.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
RSI Length
int
14
rsiLen
RSI Oversold Level
int
30
rsiOversold
RSI Overbought Level
int
70
rsiOverbought
Trend EMA Length
int
100
trendLen
Exit EMA Length
int
21
exitLen
Max Stop Loss (pips)
float
40
maxSLPips
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.
This mean-reversion strategy combines multi-timeframe exponential moving averages, Bollinger Bands, and the Relative Strength Index. It generates long and optional short trade signals when price interacts with the Bollinger Bands alongside RSI threshold confir
This strategy draws on the price chart and combines Stochastic RSI, the Money Flow Index, and an exponential moving average to open long or short trades. Longs use the lower oscillator thresholds and shorts use the upper ones, with direction limited to longs,
This strategy implements Larry Connors' 2-period RSI mean-reversion algorithm overlaid on the price chart. It filters trades using a long-term Simple Moving Average (SMA), entering long positions when the price trades above the SMA and the short-term RSI drops
This strategy combines multi-timeframe RSI analysis with exponential moving average filters to execute trades on price bounces. It evaluates daily and weekly RSI levels alongside pip-distance thresholds between fast, slow, and long-term EMAs. Position entries
This strategy plots on the price chart and trades fair value gaps filtered by RSI and two EMAs. It opens a long when a bullish gap, a rising candle, RSI below the oversold level, and price under both EMAs align, and a short on the opposite conditions. Each tra
Burgle Bounce is a price-chart strategy that treats a move toward one of up to four simple or exponential moving averages as a test when price comes within a user-set percentage of ATR, then exits toward a target average under a separate error margin. Longs an