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)
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")

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: holds, mirage 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 Combo
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.