Skip to main content

RSI Mean Reversion — Pine Script v5 strategy

REVERSALRSIPine Script v5

This script applies a classic RSI mean reversion idea to the close price of the chart. It buys when the 14-period RSI crosses back above 30 and closes the position when RSI crosses below 70.

Full code (free to copy)

//@version=5
strategy("RSI Mean Reversion", overlay=true, pyramiding=0, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1, process_orders_on_close=true, margin_long=0)
rsiLen = input.int(14)
rsiVal = ta.rsi(close, rsiLen)
if (ta.crossover(rsiVal, 30))
    strategy.entry("Long", strategy.long)
if (ta.crossunder(rsiVal, 70))
    strategy.close("Long")

Judge this code on BTCUSDT 1D →Free, in your browser, on the same candles as the table below.

Online judge verdict on BTCUSDT (measured 29 November 2026)

TimeframeSealed periodTradesReturnBuy & holdVerdict
1d2024-12-17 → 2026-10-16325.34 %-23.25 %Not enough sealed trades
4h2026-03-12 → 2026-10-174-3.41 %15.83 %Not enough sealed trades
1h2026-08-05 → 2026-10-17810.98 %27.14 %Not enough sealed trades

Past data, 10 bps per fill, last 20% of the bars sealed. Windows: full history on 1d, last 3 years on 4h, last year on 1h. Research, not advice: no result is promised.

Compared with a real TradingView run on 31 October 2026 (BINANCE:BTCUSDT, this code): identical trades 14/14 on 1D, 26/26 on 4h, same prices on all matching trades.

Measured on 29 November 2026 with the online judge’s Pine v5 engine (sa-pine-vm 0.2.1), on Binance BTCUSDT candles up to 16/10/2026 on 1D and up to 17/10/2026 on 4h and 1h. Differences with TradingView’s Strategy Tester remain possible. Binance data, CC BY-NC-SA 4.0.

Open this code in the free judge (1 click) →

Replay each verdict yourself, in your browser: 1d · 4h · 1h. The script is filled in; nothing runs until you click “Judge”.

How the script works

The header sets //@version=5 and calls strategy() with the name RSI Mean Reversion and overlay=true, so the script runs directly on the price chart. rsiLen is created with input.int(14), which exposes the length as a setting whose default is 14. rsiVal is then calculated by ta.rsi(close, rsiLen), so the indicator is built from the closing price of each bar.

The entry block tests ta.crossover(rsiVal, 30), which is true only on the bar where RSI moves from below 30 to above it. When that condition is met, strategy.entry("Long", strategy.long) sends a market long order carrying the identifier Long. The script declares no pyramiding rule and no position size call, so the strategy falls back to its default order quantity.

The second block tests ta.crossunder(rsiVal, 70), which fires when RSI drops from above 70 to below it. On that bar strategy.close("Long") closes the position opened under the same identifier. The exit therefore depends only on the RSI crossing 70 and ignores price, so the code contains no stop loss, no take profit and no trailing rule.

Parameters in the code

ParameterValueRole
rsiLen14Length of the RSI, set with input.int(14) and applied to close.
oversold threshold30Hardcoded level in ta.crossover(rsiVal, 30) that triggers the long entry.
overbought threshold70Hardcoded level in ta.crossunder(rsiVal, 70) that triggers the exit.
overlaytruestrategy() argument that draws the strategy on the price chart.
entry identifierLongOrder id shared by strategy.entry and strategy.close so the exit targets the entry.
order directionstrategy.longDirection passed to strategy.entry; the script never sends a short order.
input sourceclosePrice series fed to ta.rsi; it is not configurable in the code.
commission and slippagenot setThe script defines no commission or slippage, so strategy defaults apply.

Known limits of this rule

Before you trust it: test it on data it never saw

A backtest in TradingView's Strategy Tester shows one past path with the settings you picked. Strategy Arena Lab (free, Windows and Linux) replays this Pine on your PC, compares its trades one by one with TradingView's for supported Pine, counts the variants you try, then judges the rule on a period kept sealed during the search: survives, no evidence of an edge or inconclusive.

Get the free Lab →

FAQ

Does it work on crypto?

The code reads only close and the RSI, so it can be applied to crypto pairs the same way as to any other symbol. You can replay it on a period it has never seen in Strategy Arena Lab, which is free.

How do I add a stop loss?

Add a strategy.exit call with a stop parameter for the Long identifier, or replace strategy.close with an exit that carries a stop level. As written, the position stays open until RSI crosses below 70.

Other free Pine Script v5 strategies

EMA Crossover 9/21EMA Ribbon 8/21/55Bollinger Band BounceMACD MomentumSuperTrendStochastic RSIATR BreakoutVWAP BounceRSI + EMA ComboFaber 10-Month TimingGolden Cross SMA 50/200Bollinger Squeeze BreakoutKeltner Channel Breakout 20/1.5Donchian Turtle 20/10ADX 25 & DMI 14 TrendParabolic SAR 0.02/0.2Hull Moving Average 55ALMA 20 Gaussian TrendCCI 20 (+100) BreakoutMoney Flow Index MFI 14 (20/80)ATR 14 Chandelier StopIchimoku Tenkan 9 / Kijun 26Linear Regression 20ROC 14 + EMA 200 FilterWilliams %R 14 PullbackNo-Leak Weekly EMA FilterPivot High/Low BreakoutAI-style ATR Breakout + EMA 200
TradingView Starter
Choose the account path. Prove the strategy separately.
Your first chart and price alert do not require the Lab. Strategy Arena proof remains required before trusting or exporting a strategy.
Partner attribution is for first-time users and web purchases only. Strategy Arena may earn a commission on eligible purchases; starting with a free account needs no payment.
I have never had an account · Start free I already have an account Check a strategy
More TradingView paths
Evidence-gated Pine review

Educational code on past data. No investment advice; no result is promised.