Pine Script v5 · Strategy
RSI Divergence Strategy
A Pine Script v5 strategy built on RSI (close, 14), Lowest (low, 200) and Barssince (cg). 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. 10 settings; defaults include rsi length 14, source close and RSI Divergence length 14.
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 Divergence Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 13 of 13 trades.
- How did RSI Divergence Strategy do on BTCUSDT 1D with default settings?
- +102.86%
- From 26 Jan 2024 to 21 Oct 2026, over 41 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- What alerts does RSI Divergence Strategy have?
- 1
- alertcondition.
- Does RSI Divergence 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: 41 before, 0 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 Divergence Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 113, 117.
BTCUSDT · 1D · default settings
- Plot
- HLine
- HLine
- HLine
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +102.86%
- $1,028,600.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 41
- 27 won · 14 lost
- Profitable
- 65.9%
- of closed trades
- Profit factor
- 8.94
- gross profit ÷ gross loss
- Max drawdown
- 1.99%
- −$28,558.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 |
|---|---|---|---|
| rsi length | 14 | int | x |
| source | close | source | x |
| RSI Divergence length | 14 | int · max 500 | xo yo xyo xxo dizi xo2 yo2 xyo2 xxo2 dizi2 xo3 yo3 xyo3 xxo3 dizi3 xo4 yo4 xyo4 xxo4 dizi4 line line2 alert longCondition shortCondition line 25 line 35 line 43 line 52 bgcolor line 66 line 75 line 82 line 91 |
| zoom | 0 | int · min -27 · max 27 | hline |
| buy | |||
| take profit | 25 | float · step 0.5 | exit exit long |
| stop | 5 | float · step 0.5 | exit exit long |
| trailing stop | 0.25 | float · step 0.5 | exit exit long |
| sell | |||
| take profit | 25 | float · step 0.5 | exit exit short |
| stop | 5 | float · step 0.5 | exit exit short |
| trailing stop | 0.25 | float · step 0.5 | exit exit short |
What it calculates
- RSIsource (close), rsi length (14)
x · line 7 - Lowestlow, 200
dnm · line 30 - Barssincecg
yo3 · line 68 - Barssincecg
xyo3 · line 69 - Barssincecr
yo4 · line 84 - Barssincecr
xyo4 · line 85
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 | (longCondition) | On | 112 |
| Exit exit long stop and limit | (longCondition) | On | 113 |
| Open Short short | (shortCondition) | On | 116 |
| Exit exit short stop and limit | (shortCondition) | On | 117 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Alerts
| Alert | Call | Message | Line |
|---|---|---|---|
| (no title) | alertcondition | no message | 105 |
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 | +102.9% | +102.5% | 41 | 66% | 8.94 | 2.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +47.2% | +5.5% | 45 | 49% | 4.69 | 3.3% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +5.6% | +9.8% | 44 | 59% | 11.18 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +1.9% | +8.4% | 50 | 60% | 6.09 | 0.1% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.3% | +16.7% | 49 | 47% | 10.91 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.1% | +21.5% | 49 | 51% | 4.14 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +1.5% | +148.3% | 45 | 60% | 12.57 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.5% | +17.1% | 48 | 67% | 7.60 | 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 | 13 | 62% | $220,506.00 |
| Sideways market | 21 | 71% | $674,253.00 |
| Falling market | 7 | 57% | $133,838.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
3.0%
The 95th percentile of the worst drawdown when the same 41 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 1.3% to 3.0%; the tick is what actually happened, 1.8%. Order luck alone moves it this much.
All 41 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 23 Feb 2024 51,288.40 | 23 Feb 2024 51,416.60 | 0 | −$3,461.94 |
| short | 9 Mar 2024 68,124.20 | 9 Mar 2024 68,031.40 | 0 | $2,319.75 |
| short | 29 Mar 2024 70,780.60 | 29 Mar 2024 69,185.90 | 0 | $63,786.00 |
| long | 25 Apr 2024 64,289.60 | 25 Apr 2024 65,137.20 | 0 | $38,991.40 |
| long | 8 May 2024 62,312.10 | 8 May 2024 62,864.40 | 0 | $25,409.00 |
| short | 29 May 2024 68,398.40 | 29 May 2024 67,295.60 | 0 | $48,521.00 |
| long | 2 Jul 2024 62,900.00 | 2 Jul 2024 63,131.60 | 0 | $10,189.50 |
| short | 30 Jul 2024 66,784.70 | 30 Jul 2024 66,951.60 | 0 | −$7,012.32 |
| long | 14 Aug 2024 60,587.20 | 14 Aug 2024 61,648.50 | 0 | $50,945.80 |
| long | 13 Sep 2024 58,132.30 | 14 Sep 2024 60,479.70 | 1 | $115,021.00 |
| short | 2 Oct 2024 60,804.90 | 2 Oct 2024 60,152.00 | 0 | $35,257.10 |
| short | 26 Oct 2024 66,698.30 | 26 Oct 2024 66,606.60 | 0 | $4,033.48 |
| short | 24 Nov 2024 97,672.40 | 24 Nov 2024 97,916.60 | 0 | −$5,371.96 |
| long | 4 Jan 2025 98,174.20 | 4 Jan 2025 98,533.00 | 0 | $19,376.30 |
| short | 28 Jan 2025 102,083.00 | 28 Jan 2025 102,338.00 | 0 | −$11,739.70 |
| long | 16 Feb 2025 97,569.70 | 16 Feb 2025 97,460.60 | 0 | −$5,128.64 |
| long | 7 Mar 2025 89,931.90 | 7 Mar 2025 91,058.20 | 0 | $51,810.30 |
| long | 12 Apr 2025 83,423.80 | 12 Apr 2025 85,696.40 | 0 | $113,631.00 |
| short | 6 May 2025 94,733.70 | 6 May 2025 93,613.80 | 0 | $43,674.10 |
| short | 20 May 2025 105,574.00 | 20 May 2025 104,449.00 | 0 | $34,877.50 |
| short | 18 Jun 2025 104,551.00 | 18 Jun 2025 104,813.00 | 0 | −$13,853.10 |
| long | 27 Jun 2025 106,947.00 | 27 Jun 2025 106,680.00 | 0 | −$14,705.30 |
| short | 22 Jul 2025 117,380.00 | 22 Jul 2025 116,421.00 | 0 | $35,479.70 |
| long | 6 Sep 2025 110,660.00 | 6 Sep 2025 111,031.00 | 0 | $16,326.20 |
| short | 23 Sep 2025 112,651.00 | 23 Sep 2025 112,933.00 | 0 | −$15,771.30 |
| short | 12 Oct 2025 110,644.00 | 12 Oct 2025 109,842.00 | 0 | $48,966.50 |
| long | 25 Oct 2025 111,005.00 | 25 Oct 2025 110,727.00 | 0 | −$13,043.00 |
| long | 28 Nov 2025 91,333.90 | 28 Nov 2025 92,863.70 | 0 | $62,718.90 |
| long | 26 Dec 2025 87,225.30 | 26 Dec 2025 87,007.20 | 0 | −$9,158.52 |
| short | 21 Jan 2026 88,427.70 | 21 Jan 2026 87,484.60 | 0 | $56,583.60 |
| long | 12 Feb 2026 67,082.50 | 12 Feb 2026 68,242.80 | 0 | $34,808.70 |
| long | 7 Apr 2026 68,853.70 | 7 Apr 2026 68,681.50 | 0 | −$8,606.50 |
| short | 29 Apr 2026 76,342.80 | 29 Apr 2026 75,128.40 | 0 | $53,433.60 |
| short | 15 May 2026 81,090.00 | 15 May 2026 78,861.70 | 0 | $89,131.20 |
| long | 14 Jun 2026 64,458.00 | 14 Jun 2026 64,296.90 | 0 | −$5,962.55 |
| long | 24 Jun 2026 62,734.60 | 24 Jun 2026 63,082.20 | 0 | $12,863.10 |
| long | 4 Jul 2026 62,583.30 | 4 Jul 2026 62,426.80 | 0 | −$7,510.08 |
| short | 28 Jul 2026 63,755.90 | 28 Jul 2026 63,915.30 | 0 | −$8,288.28 |
| long | 8 Aug 2026 64,923.20 | 8 Aug 2026 65,030.20 | 0 | $5,779.62 |
| short | 1 Sep 2026 78,581.30 | 1 Sep 2026 76,616.40 | 0 | $56,980.70 |
| short | 2 Oct 2026 84,880.10 | 2 Oct 2026 84,100.20 | 0 | $27,294.80 |
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 Divergence Strategy this page | — | v5 strategy | +102.9% | 41 | 2.0% |
| PlanB Quant Investing 101 v2 by fillippone | rsi highest lowest | v4 strategy | No trades on this market | ||
| STRATEGY R18-F-BTC by SenatorVonShaft | highest lowest rsi | v4 strategy | −8.6% | 11 | 14.4% |
| Dynamic Stop Loss Demo by Tindtily | highest lowest rsi | v5 strategy | does not run in Zeal Terminal | ||
| Stochastic RSI by Zeal Terminal | rsi highest lowest | v5 indicator | indicator | ||
| Alex Days HiLo Rajib by 14rajib | highest lowest | v4 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.
- 1 setting uses the untyped input() (line 6). 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 41 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 6: input() → input.source()
⋯rsilen=input.int(14, title="rsi length")-rsisrc=input(close, title="source")+rsisrc=input.source(close, title="source")x=ta.rsi(rsisrc,rsilen)⋯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: 41 before, 0 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.
⋯// © faytterro-//@version=5+//@version=6strategy("RSI Divergence Strategy", overlay=true, scale = scale.none)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 64 ms | Yes 1,000 bars in 501 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
“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/// © faytterro//@version=5strategy("RSI Divergence Strategy", overlay=true, scale = scale.none)rsilen=input.int(14, title="rsi length")rsisrc=input(close, title="source")x=ta.rsi(rsisrc,rsilen)len=input.int(14, title="RSI Divergence length", maxval=500)tpb = input.float(25, title="take profit", group = "buy", step = 0.5)sb = input.float(5, title="stop", group = "buy", step = 0.5)tsb = input.float(0.25, title="trailing stop", group = "buy", step = 0.5)tps = input.float(25, title="take profit", group = "sell", step = 0.5)ss =input.float(5, title="stop", group = "sell", step = 0.5)tss = input.float(0.25, title="trailing stop", group = "sell", step = 0.5)src=closeextrapolation=0zoom=input.int(0, title="zoom", maxval=27, minval=-27)hline(300-zoom*10, color=color.rgb(54, 58, 69, 100))hline(10, color=color.rgb(54, 58, 69, 100))// for ax+bxo=0.0yo=0.0xyo=0.0xxo=0.0for i=0 to len-1 xo:= xo + i/(len) yo:= yo + x[len-1-i]/(len) xyo:= xyo + i*x[len-1-i]/(len) xxo:= xxo + i*i/(len)dnm=ta.lowest(low,200)dizi=array.new_float(len*2+1+extrapolation)linedizi=array.new_line()a=(xo*yo-xyo)/(xo*xo-xxo)b=yo-a*xofor i=0 to len-1+extrapolation array.set(dizi,i,a*i+b)//// for src// for ax+bxo2=0.0yo2=0.0xyo2=0.0xxo2=0.0for i=0 to len-1 xo2:= xo2 + i/(len) yo2:= yo2 + src[len-1-i]/(len) xyo2:= xyo2 + i*src[len-1-i]/(len) xxo2:= xxo2 + i*i/(len)dizi2=array.new_float(len*2+1+extrapolation)linedizi2=array.new_line()a2=(xo2*yo2-xyo2)/(xo2*xo2-xxo2)b2=yo2-a*xo2for i=0 to len-1+extrapolation array.set(dizi2,i,a2*i+b2)ttk=((array.get(dizi,0)<array.get(dizi,1)) and (array.get(dizi2,0)>array.get(dizi2,1)))? 1 : ((array.get(dizi,0)>array.get(dizi,1)) and (array.get(dizi2,0)<array.get(dizi2,1)))? -1 : 0cg=((array.get(dizi,0)<array.get(dizi,1)) and (array.get(dizi2,0)>array.get(dizi2,1)))// and ta.highest(ttk[1],len/2)<1)cr=((array.get(dizi,0)>array.get(dizi,1)) and (array.get(dizi2,0)<array.get(dizi2,1)))// and ta.lowest(ttk[1],len/2)>-1)bgcolor(color=(cg and ta.highest(ttk[1],len/2)<1)? color.rgb(76, 175, 79, 50) : (cr and ta.lowest(ttk[1],len/2)>-1)? color.rgb(255, 82, 82, 50) : na, offset=0, display=display.none)plot(x)// for ax+bxo3=0.0yo3=0.0xyo3=0.0xxo3=0.0for i=0 to len-1 xo3:= xo3 + i/(len) yo3:= yo3 + x[len-1-i+(ta.barssince(cg))]/(len) xyo3:= xyo3 + i*x[len-1-i+(ta.barssince(cg))]/(len) xxo3:= xxo3 + i*i/(len)dizi3=array.new_float(len*2+1+extrapolation)linedizi3=array.new_line()a3=(xo3*yo3-xyo3)/(xo3*xo3-xxo3)b3=yo3-a3*xo3for i=0 to len-1+extrapolation array.set(dizi3,i,a3*i+b3)// for ax+bxo4=0.0yo4=0.0xyo4=0.0xxo4=0.0for i=0 to len-1 xo4:= xo4 + i/(len) yo4:= yo4 + x[len-1-i+(ta.barssince(cr))]/(len) xyo4:= xyo4 + i*x[len-1-i+(ta.barssince(cr))]/(len) xxo4:= xxo4 + i*i/(len)dizi4=array.new_float(len*2+1+extrapolation)linedizi4=array.new_line()a4=(xo4*yo4-xyo4)/(xo4*xo4-xxo4)b4=yo4-a4*xo4for i=0 to len-1+extrapolation array.set(dizi4,i,a4*i+b4)line=line.new((last_bar_index-ta.barssince(cg)-len), array.get(dizi3,0), last_bar_index-ta.barssince(cg), array.get(dizi3,len-1), color=color.rgb(0,255,0), width=2)line2=line.new((last_bar_index-ta.barssince(cr)-len), array.get(dizi4,0), last_bar_index-ta.barssince(cr), array.get(dizi4,len-1), color=color.rgb(255, 0, 0, 0), width=2)line.delete(line[1])line.delete(line2[1])alert=((array.get(dizi,0)<array.get(dizi,1)) and (array.get(dizi2,0)>array.get(dizi2,1)) and ta.highest(ttk[1],len/2)<1) or ((array.get(dizi,0)>array.get(dizi,1)) and (array.get(dizi2,0)<array.get(dizi2,1)) and ta.lowest(ttk[1],len/2)>-1)alertcondition(alert)hline(50)rs=hline(30)rss=hline(70)fill(rs, rss, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")longCondition = cg and ta.highest(ttk[1],len/2)<1 if (longCondition) strategy.entry("Long", strategy.long, qty = math.round(x)) strategy.exit("exit long", "Long", limit = close*(100+tpb)/100 , stop =close*(100-sb)/100 , trail_price = close , trail_offset = close*tsb)shortCondition = cr and ta.lowest(ttk[1],len/2)>-1 if (shortCondition) strategy.entry("Short", strategy.short, qty = math.round(100-x)) strategy.exit("exit short", "Short", limit = close*(100-tps)/100, stop = close*(100+ss)/100, trail_price = close , trail_offset = close*tss)