RSI Mean Reversion — Pine Script v5 strategy
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
| Parameter | Value | Role |
|---|---|---|
rsiLen | 14 | Length of the RSI, set with input.int(14) and applied to close. |
oversold threshold | 30 | Hardcoded level in ta.crossover(rsiVal, 30) that triggers the long entry. |
overbought threshold | 70 | Hardcoded level in ta.crossunder(rsiVal, 70) that triggers the exit. |
overlay | true | strategy() argument that draws the strategy on the price chart. |
entry identifier | Long | Order id shared by strategy.entry and strategy.close so the exit targets the entry. |
order direction | strategy.long | Direction passed to strategy.entry; the script never sends a short order. |
input source | close | Price series fed to ta.rsi; it is not configurable in the code. |
commission and slippage | not set | The script defines no commission or slippage, so strategy defaults apply. |
Known limits of this rule
- Mean reversion with RSI is weak in strong trends: the indicator can stay above 70 or below 30 for a long stretch, so the entry may fire against a move that keeps going.
- Fixed thresholds produce false signals in ranging or noisy markets, particularly on low timeframes where RSI oscillates around 30 and 70 and the position closes almost immediately.
- The script sets no commission, slippage or position sizing and has no stop loss, so the entry can be followed by an open loss until RSI finally crosses below 70.
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.
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
More TradingView paths
Educational code on past data. No investment advice; no result is promised.