Strategy library

RSI Mean Reversion strategy in Pine Script

Buys a dip of the RSI in an uptrend and sells a spike in a downtrend, with a fixed take profit and stop.

RSI · EMA · long and short · by Sarahann999 · Mozilla Public License 2.0

What is its backtest worth?

A parameter sweep finds the best setting of the RSI Mean Reversion strategy. PineTester tells you if it holds up: whether the best result was only picked after the fact, what it makes with fees, what you could have known at the time (a walk-forward on data the pick did not see) and how deep it fell along the way. On the free start, your first own backtest gets the full evaluation; its trade list and result file come with the full report or a plan.

Creates a draft on Binance BTC/USDT 4h over about six and a half years with 0.1% commission per order and 2 ticks slippage; you start it yourself. A backtest uses credits; new accounts receive free credits.

The rules

  • Enters long when the close is above the EMA and the RSI (14) is below the oversold level; short when the close is below the EMA and the RSI is above the overbought level.
  • Exits at a fixed percentage take profit or stop loss.
  • The short trade’s take profit and stop loss are not varied here.

What to watch for

  • The default stop is wider than the take profit, so it needs to be right in most trades – check the share of trades with a profit.
  • Small fixed targets on 4h bars give many trades: fees weigh heavily.

How PineTester judges a backtest, with its sources: how we judge results.

The Pine Script code

The author's code with its licence notice (Mozilla Public License 2.0), with one change marked in the code: the same test conditions for every library strategy in its strategy() line – the whole account per trade (split over the entries where it adds to a position), 0.1% commission per order and 2 ticks slippage; the trading rules are unchanged. Pine Script v5; PineTester converts it to v6 automatically, so results can differ slightly from TradingView.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Sarahann999
// Modified by PineTester (2026-11-14): the same test conditions for every library strategy in strategy(): 100% of equity per entry,
// 0.1% commission per order, 2 ticks slippage, initial capital 10,000. The trading rules are unchanged.
//@version=5
strategy("RSI Strategy", shorttitle="RSI", overlay= false, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1, slippage=2)
//Inputs
long_entry = input(true, title='Long Entry')
short_entry = input(true, title='Short Entry')
emaSettings = input(100, 'EMA Length')
ema = ta.ema(close,emaSettings)
rsi = ta.rsi(close,14)
rsios = input(40, title= 'RSI Oversold Settings')
rsiob = input(60, title= 'RSI Overbought Settings')
//Conditions
uptrend = close > ema
downtrend = close < ema
OB = rsi > rsiob
OS = rsi < rsios
buySignal = uptrend and OS and strategy.position_size == 0
sellSignal = downtrend and OB and strategy.position_size == 0
//Calculate Take Profit Percentage
longProfitPerc = input.float(title="Long Take Profit", group='Take Profit Percentage',
     minval=0.0, step=0.1, defval=1) / 100
shortProfitPerc = input.float(title="Short Take Profit",
     minval=0.0, step=0.1, defval=1) / 100
// Figure out take profit price 1
longExitPrice  = strategy.position_avg_price * (1 + longProfitPerc)
shortExitPrice = strategy.position_avg_price * (1 - shortProfitPerc)
// Make inputs that set the stop %  1
longStopPerc = input.float(title="Long Stop Loss", group='Stop Percentage',
     minval=0.0, step=0.1, defval=1.5) / 100
shortStopPerc = input.float(title="Short Stop Loss",
     minval=0.0, step=0.1, defval=1.5) / 100
// Figure Out Stop Price
longStopPrice  = strategy.position_avg_price * (1 - longStopPerc)
shortStopPrice = strategy.position_avg_price * (1 + shortStopPerc)
// Submit entry orders
if buySignal and long_entry
    strategy.entry(id="Long", direction=strategy.long, alert_message="Enter Long")
    
if sellSignal and short_entry
    strategy.entry(id="Short", direction=strategy.short, alert_message="Enter Short")
    
//Submit exit orders based on take profit price
if (strategy.position_size > 0)
    strategy.exit(id="Long TP/SL", limit=longExitPrice, stop=longStopPrice, alert_message="Long Exit 1 at {{close}}")
if (strategy.position_size < 0)
    strategy.exit(id="Short TP/SL", limit=shortExitPrice, stop=shortStopPrice, alert_message="Short Exit 1 at {{close}}")
    
//note: for custom alert messages to read, "{{strategy.order.alert_message}}" must be placed into the alert dialogue box when the alert is set.
plot(rsi, color= color.gray)
hline(40, "RSI Lower Band")
hline(60, "RSI Upper Band")
  • Supertrend + RSI – Trades with the Supertrend direction, but only when the RSI crosses its middle line.
  • RSI + EMA – Buys an oversold RSI while a faster moving average is above a slower one.
  • Tendency EMA + RSI – Buys a crossover of two short EMAs above a long EMA and takes profit after a set number of bars.
  • Volatility System – Goes long or short when the close moves more than a multiple of the ATR in a single bar.
  • Bull Trend Filtered StochRSI – Buys while the Stochastic RSI is above a lower band and the close is above the highest EMA of the highs.

Open-source script by Sarahann999, shown under the Mozilla Public License 2.0. Not a recommendation; neither the author nor TradingView endorses PineTester. Backtests are hypothetical results on past data, not a forecast.