Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (10)
0.0050,000100,000AprJulOct2025AprJulOct2026AprJulOct$2,028,540$2,025,273Equity from $1,000,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots. Lines, boxes and labels are the script's own drawings, the most recent ones its limits keep.
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.

SettingDefaultType and rangeFeeds
rsi length14intx
sourceclosesourcex
RSI Divergence length14int · max 500xo 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
zoom0int · min -27 · max 27hline
buy
take profit25float · step 0.5exit exit long
stop5float · step 0.5exit exit long
trailing stop0.25float · step 0.5exit exit long
sell
take profit25float · step 0.5exit exit short
stop5float · step 0.5exit exit short
trailing stop0.25float · step 0.5exit exit short

What it calculates

  • RSIsource (close), rsi length (14)x · line 7
  • Lowestlow, 200dnm · line 30
  • Barssincecgyo3 · line 68
  • Barssincecgxyo3 · line 69
  • Barssincecryo4 · line 84
  • Barssincecrxyo4 · 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.

closecloseprice datarsilenrsi lengthsetting · 14lowlowprice datalenRSI Divergence lengthsetting · 14zoomzoomsetting · 0xRSI→ xdnmLowest→ dnmyo3Barssince→ yo3xyo3Barssince→ xyo3yo4Barssince→ yo4xyo4Barssince→ xyo4srcsrcvaluexoxovalueyoyovaluexyoxyovaluexxoxxovaluedizi, dizi2, dizi3, dizi4dizi · dizi2 +24 valuesaavaluebbvaluexo2xo2valueyo2yo2valuexyo2xyo2valuexxo2xxo2valuea2a2valueb2b2valuettkttkvaluexo3xo3valuexxo3xxo3valuea3a3valueb3b3valuexo4xo4valuexxo4xxo4valuea4a4valueb4b4valuelongConditionlongConditionconditionshortConditionshortConditionconditioncg, crcg · cr2 conditionsalertalertconditionline, line2Colourline · line2entry:LongOpen Longstrategy.entryexit:exit longexit exit longstrategy.exitentry:ShortOpen Shortstrategy.entryexit:exit shortexit exit shortstrategy.exithline:18hlinehlinehline:19hlinehlinebgcolor:58bgcolorbgcolorplot:60plotplotalertcondition:105alertconditionalertconditionhline:106hlinehlinehline:107hlinehlinehline:108hlinehlinefill:RSI Background FillRSI Background Fillfill

Order rules

OrderFires whenBy defaultLine
Open Long long(longCondition)On112
Exit exit long stop and limit(longCondition)On113
Open Short short(shortCondition)On116
Exit exit short stop and limit(shortCondition)On117

Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.

Alerts

AlertCallMessageLine
(no title)alertconditionno message105

Does it repaint?

No

No repainting patterns in the source, and the live replay matched history.

13/13trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+102.9%+102.5%4166%8.942.0%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+47.2%+5.5%4549%4.693.3%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+5.6%+9.8%4459%11.180.1%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+1.9%+8.4%5060%6.090.1%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+0.3%+16.7%4947%10.910.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.1%+21.5%4951%4.140.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+1.5%+148.3%4560%12.570.0%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+0.5%+17.1%4867%7.600.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 barsTradesWonP&L
Rising market1362%$220,506.00
Sideways market2171%$674,253.00
Falling market757%$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.

0%3%
All 41 trades
SideEntryExitBarsP&L
short23 Feb 2024 51,288.4023 Feb 2024 51,416.600−$3,461.94
short9 Mar 2024 68,124.209 Mar 2024 68,031.400$2,319.75
short29 Mar 2024 70,780.6029 Mar 2024 69,185.900$63,786.00
long25 Apr 2024 64,289.6025 Apr 2024 65,137.200$38,991.40
long8 May 2024 62,312.108 May 2024 62,864.400$25,409.00
short29 May 2024 68,398.4029 May 2024 67,295.600$48,521.00
long2 Jul 2024 62,900.002 Jul 2024 63,131.600$10,189.50
short30 Jul 2024 66,784.7030 Jul 2024 66,951.600−$7,012.32
long14 Aug 2024 60,587.2014 Aug 2024 61,648.500$50,945.80
long13 Sep 2024 58,132.3014 Sep 2024 60,479.701$115,021.00
short2 Oct 2024 60,804.902 Oct 2024 60,152.000$35,257.10
short26 Oct 2024 66,698.3026 Oct 2024 66,606.600$4,033.48
short24 Nov 2024 97,672.4024 Nov 2024 97,916.600−$5,371.96
long4 Jan 2025 98,174.204 Jan 2025 98,533.000$19,376.30
short28 Jan 2025 102,083.0028 Jan 2025 102,338.000−$11,739.70
long16 Feb 2025 97,569.7016 Feb 2025 97,460.600−$5,128.64
long7 Mar 2025 89,931.907 Mar 2025 91,058.200$51,810.30
long12 Apr 2025 83,423.8012 Apr 2025 85,696.400$113,631.00
short6 May 2025 94,733.706 May 2025 93,613.800$43,674.10
short20 May 2025 105,574.0020 May 2025 104,449.000$34,877.50
short18 Jun 2025 104,551.0018 Jun 2025 104,813.000−$13,853.10
long27 Jun 2025 106,947.0027 Jun 2025 106,680.000−$14,705.30
short22 Jul 2025 117,380.0022 Jul 2025 116,421.000$35,479.70
long6 Sep 2025 110,660.006 Sep 2025 111,031.000$16,326.20
short23 Sep 2025 112,651.0023 Sep 2025 112,933.000−$15,771.30
short12 Oct 2025 110,644.0012 Oct 2025 109,842.000$48,966.50
long25 Oct 2025 111,005.0025 Oct 2025 110,727.000−$13,043.00
long28 Nov 2025 91,333.9028 Nov 2025 92,863.700$62,718.90
long26 Dec 2025 87,225.3026 Dec 2025 87,007.200−$9,158.52
short21 Jan 2026 88,427.7021 Jan 2026 87,484.600$56,583.60
long12 Feb 2026 67,082.5012 Feb 2026 68,242.800$34,808.70
long7 Apr 2026 68,853.707 Apr 2026 68,681.500−$8,606.50
short29 Apr 2026 76,342.8029 Apr 2026 75,128.400$53,433.60
short15 May 2026 81,090.0015 May 2026 78,861.700$89,131.20
long14 Jun 2026 64,458.0014 Jun 2026 64,296.900−$5,962.55
long24 Jun 2026 62,734.6024 Jun 2026 63,082.200$12,863.10
long4 Jul 2026 62,583.304 Jul 2026 62,426.800−$7,510.08
short28 Jul 2026 63,755.9028 Jul 2026 63,915.300−$8,288.28
long8 Aug 2026 64,923.208 Aug 2026 65,030.200$5,779.62
short1 Sep 2026 78,581.301 Sep 2026 76,616.400$56,980.70
short2 Oct 2026 84,880.102 Oct 2026 84,100.200$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.

ScriptSharesTypeNetTradesMax drawdown
RSI Divergence Strategy this page—v5 strategy+102.9%412.0%
PlanB Quant Investing 101 v2 by fillipponersi highest lowestv4 strategyNo trades on this market
STRATEGY R18-F-BTC by SenatorVonShafthighest lowest rsiv4 strategy−8.6%1114.4%
Dynamic Stop Loss Demo by Tindtilyhighest lowest rsiv5 strategydoes not run in Zeal Terminal
Stochastic RSI by Zeal Terminalrsi highest lowestv5 indicatorindicator
Alex Days HiLo Rajib by 14rajibhighest lowestv4 strategyNo 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.

Clean v5

VerifiedDownload the v5 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

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 64 msYes 1,000 bars in 501 msNot measured
PineTSNot testedNot testedNot 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)