Pine Script v5 · Strategy
RSI Mean Reversion Strategy
A Pine Script v5 strategy built on EMA (close, 100) and RSI (close, 14). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade is one contract and pays no commission. 9 settings; defaults include EMA Length 100, RSI Oversold Settings 40 and RSI Overbought Settings 60.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does RSI Mean Reversion Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 8 of 8 trades.
- How did RSI Mean Reversion Strategy do on BTCUSDT 1D with default settings?
- +0.81%
- From 26 Jan 2024 to 21 Oct 2026, over 12 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does RSI Mean Reversion Strategy run in Pine Script v6?
- No
- The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 12 before, 20 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
- Does RSI Mean Reversion Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 45, 47.
- Does RSI Mean Reversion Strategy run in Zeal Terminal?
- Yes
- It compiles in 13 ms and runs 1,000 bars in 33 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- RSI Lower Band
- RSI Upper Band
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +0.81%
- $8,060.63
- Buy and hold
- +102.5%
- same period
- Closed trades
- 12
- 9 won · 3 lost
- Profitable
- 75.0%
- of closed trades
- Profit factor
- 3.22
- gross profit ÷ gross loss
- Max drawdown
- 0.25%
- −$2,510.50
One run with the author's default settings, $1,000,000 starting capital, commission as the script sets it. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Long Entry | true | bool | entry Long line 37 |
| Short Entry | true | bool | entry Short line 40 |
| EMA Length | 100 | int | ema |
| RSI Oversold Settings | 40 | int | OS |
| RSI Overbought Settings | 60 | int | OB |
| Short Take Profit | 1 | float · min 0.0 · step 0.1 | shortExitPrice |
| Short Stop Loss | 1.5 | float · min 0.0 · step 0.1 | shortStopPrice |
| Take Profit Percentage | |||
| Long Take Profit | 1 | float · min 0.0 · step 0.1 | longExitPrice |
| Stop Percentage | |||
| Long Stop Loss | 1.5 | float · min 0.0 · step 0.1 | longStopPrice |
What it calculates
- EMAclose, EMA Length (100)
ema · line 9 - RSIclose, 14
rsi · line 10
Signal flow
How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Long long | buySignal and long_entry | On | 38 |
| Open Short short | sellSignal and short_entry | On | 41 |
| Exit Long TP/SL stop and limit | (strategy.position_size > 0) | On | 45 |
| Exit Short TP/SL stop and limit | (strategy.position_size < 0) | On | 47 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
No
No repainting patterns in the source, and the live replay matched history.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
- ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | +0.8% | +102.5% | 12 | 75% | 3.22 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.2% | +5.5% | 9 | 44% | 0.62 | 0.3% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.0% | +9.8% | 23 | 52% | 1.41 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.0% | +8.4% | 8 | 75% | 2.27 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 18 | 67% | 1.47 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.0% | +21.5% | 11 | 55% | 0.71 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.0% | +148.3% | 11 | 45% | 0.36 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 10 | 70% | 1.58 | 0.0% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 2 | 100% | $6,032.53 |
| Sideways market | 5 | 60% | $72.27 |
| Falling market | 5 | 80% | $1,955.83 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
0.3%
The 95th percentile of the worst drawdown when the same 12 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 0.2% to 0.3%; the tick is what actually happened, 0.2%. Order luck alone moves it this much.
All 12 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 19 Jun 2024 65,175.30 | 20 Jun 2024 65,827.10 | 1 | $651.76 |
| long | 21 Jun 2024 64,870.00 | 23 Jun 2024 63,896.90 | 2 | −$973.05 |
| long | 23 Jun 2025 100,964.00 | 24 Jun 2025 105,334.00 | 1 | $4,370.07 |
| long | 3 Aug 2025 112,546.00 | 4 Aug 2025 114,209.00 | 1 | $1,662.46 |
| short | 6 Jan 2026 93,859.70 | 7 Jan 2026 92,921.10 | 1 | $938.60 |
| short | 14 Jan 2026 95,414.00 | 15 Jan 2026 96,951.80 | 1 | −$1,537.79 |
| short | 16 Jan 2026 95,604.80 | 18 Jan 2026 94,648.80 | 2 | $956.05 |
| short | 17 Mar 2026 74,884.70 | 18 Mar 2026 73,909.40 | 1 | $975.30 |
| short | 11 Apr 2026 72,962.70 | 12 Apr 2026 72,233.10 | 1 | $729.63 |
| short | 14 Apr 2026 74,418.00 | 15 Apr 2026 73,673.80 | 1 | $744.18 |
| short | 16 Apr 2026 74,810.00 | 17 Apr 2026 75,932.10 | 1 | −$1,122.15 |
| short | 22 Jul 2026 66,556.10 | 23 Jul 2026 65,890.60 | 1 | $665.57 |
Similar scripts
Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| RSI Mean Reversion Strategy this page | — | v5 strategy | +0.8% | 12 | 0.2% |
| RSI Cross [xaurr] by livetrend | rsi ema | v4 strategy | +24.4% | 53 | 35.3% |
| Bitpanda Coinrule Template by joshuajcoop01 | rsi ema | v5 strategy | −2.8% | 4 | 4.6% |
| RSI with Slow and Fast MA Crossing Strategy (by Coinrule) by Coinrule | rsi ema | v5 strategy | −2.8% | 4 | 4.6% |
| RSI of VWAP by mohanee | rsi ema | v4 strategy | No trades on this market | ||
| EMA SCALPEUR + RSi - SHORT by YukalMoon | rsi ema | v5 strategy | No trades on this market | ||
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- 5 settings use the untyped input() (line 6, 7, 8, 11, 12). It still compiles in v6; input.int, input.float or input.source state the type.
- No v6-only features. v6 is not needed to run this script.
Same 12 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 6: input() → input.bool()
- Line 7: input() → input.bool()
- Line 8: input() → input.int()
- Line 11: input() → input.int()
- Line 12: input() → input.int()
⋯//Inputs-long_entry = input(true, title='Long Entry')+long_entry = input.bool(true, title='Long Entry')-short_entry = input(true, title='Short Entry')+short_entry = input.bool(true, title='Short Entry')-emaSettings = input(100, 'EMA Length')+emaSettings = input.int(100, 'EMA Length')ema = ta.ema(close,emaSettings)rsi = ta.rsi(close,14)-rsios = input(40, title= 'RSI Oversold Settings')+rsios = input.int(40, title= 'RSI Oversold Settings')-rsiob = input(60, title= 'RSI Overbought Settings')+rsiob = input.int(60, title= 'RSI Overbought Settings')//Conditions⋯Does it run in Pine Script v6?
No
The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 12 before, 20 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © Sarahann999-//@version=5+//@version=6strategy("RSI Strategy", shorttitle="RSI", overlay= false)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 13 ms | With a workaround 1,000 bars in 33 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
Zeal Terminal's engine currently reads an order side written as direction = strategy.long, direction = strategy.short as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 38, 41) so the engine trades the way the script says.
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © Sarahann999//@version=5strategy("RSI Strategy", shorttitle="RSI", overlay= false)//Inputslong_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')//Conditionsuptrend = close > emadowntrend = close < emaOB = rsi > rsiobOS = rsi < rsiosbuySignal = uptrend and OS and strategy.position_size == 0sellSignal = downtrend and OB and strategy.position_size == 0//Calculate Take Profit PercentagelongProfitPerc = input.float(title="Long Take Profit", group='Take Profit Percentage', minval=0.0, step=0.1, defval=1) / 100shortProfitPerc = input.float(title="Short Take Profit", minval=0.0, step=0.1, defval=1) / 100// Figure out take profit price 1longExitPrice = strategy.position_avg_price * (1 + longProfitPerc)shortExitPrice = strategy.position_avg_price * (1 - shortProfitPerc)// Make inputs that set the stop % 1longStopPerc = input.float(title="Long Stop Loss", group='Stop Percentage', minval=0.0, step=0.1, defval=1.5) / 100shortStopPerc = input.float(title="Short Stop Loss", minval=0.0, step=0.1, defval=1.5) / 100// Figure Out Stop PricelongStopPrice = strategy.position_avg_price * (1 - longStopPerc)shortStopPrice = strategy.position_avg_price * (1 + shortStopPerc)// Submit entry ordersif 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 priceif (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")