Strategy · Pine Script v5 · On the chart

RSI Mean Reversion

by LePotoRico2105

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.

View on GitHub

GitHub stars4on the repository
Pine versionv5runs as is
Lines of code53
Settings6inputs
LicenceMIT
Last update6 months ago3 Apr 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 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.

Details

Published by

LePotoRico2105 on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Mean reversion, Oscillators & momentum, Moving averages

Markets

Forex

Timeframes

H4 and above

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

algorithmic-tradingbacktestingforexictpinescriptsmcstrategytradingtradingview

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
RSI Lengthint14rsiLen
RSI Oversold Levelint30rsiOversold
RSI Overbought Levelint70rsiOverbought
Trend EMA Lengthint100trendLen
Exit EMA Lengthint21exitLen
Max Stop Loss (pips)float40maxSLPips
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.

Similar Pine Scripts

330 stars

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

Strategy v5 Mean reversionMoving averages
Updated 2 months ago · None stated GitHub repository of MEMA + BB + RSI [Alorse]

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,

Strategy v5 Oscillators & momentumMoving averages Repaints
Updated 2 years ago · GPL-3.0 GitHub repository of 3 Indicator Strategy (StochRSI, MFI & EMA) With Safety Orders
53 stars

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

Strategy v6 Oscillators & momentumMean reversion
Updated 2 months ago · MPL-2.0 GitHub repository of Larry Connors 2-Period RSI Strategy

FVG RSI Strategy (Optimized)

by skywalker0803r
42 stars

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

Strategy v5 SMC / ICT / price actionOscillators & momentum
Updated 2 months ago · None stated GitHub repository of FVG RSI Strategy (Optimized)

Burgle Bounce

by benso87
24 stars

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

Strategy v5 Moving averagesOscillators & momentum Repaints
Updated 4 years ago · MPL-2.0 GitHub repository of Burgle Bounce

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers