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, 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)
| Timeframe | Sealed period | Trades | Return | Buy & hold | Verdict |
|---|---|---|---|---|---|
| 1d | 2024-12-17 → 2026-10-16 | 3 | 25.34 % | -23.25 % | Not enough sealed trades |
| 4h | 2026-03-12 → 2026-10-17 | 4 | -3.41 % | 15.83 % | Not enough sealed trades |
| 1h | 2026-08-05 → 2026-10-17 | 8 | 10.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
| 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: survives, no evidence of an edge 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.