Pine Script v4 · Strategy
ADX + RSI Strat
A Pine Script v4 strategy built on Lowest (20), Highest (20) and Valuewhen or ATR (bought, SwingLow). It has 5 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 commits 100% of equity and pays 0.04 commission. 12 settings; defaults include Strategy Direction 0, Swing Lo/Hi Lookback 20 and SL Expander 0.
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 ADX + RSI Strat repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 2 of 2 trades.
- How did ADX + RSI Strat do on BTCUSDT 1D with default settings?
- −3.49%
- From 26 Jan 2024 to 21 Oct 2026, over 6 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can ADX + RSI Strat be converted to Pine Script v5?
- Yes
- The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
- Does ADX + RSI Strat use a stop loss?
- Yes
- strategy.exit sets a stop on line 60, 61.
- Does ADX + RSI Strat run in Zeal Terminal?
- Yes
- It compiles in 25 ms and runs 1,000 bars in 56 ms, in the browser.
BTCUSDT · 1D · default settings
- Bullish Setup
- Bearish Setup
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −3.49%
- −$3.49
- Buy and hold
- +102.5%
- same period
- Closed trades
- 6
- 2 won · 4 lost
- Profitable
- 33.3%
- of closed trades
- Profit factor
- 0.92
- gross profit ÷ gross loss
- Max drawdown
- 36.30%
- −$46.60
One run with the author's default settings, $100 starting capital, commission as the script sets it. A long position was still open at the last bar. 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
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
3 settings have no label
In the settings dialog OS (line 32), OB (line 33), adxlevel (line 51) show an empty name. The labels on this page come from the variable names.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses an argument v6 removed
when = on line 56, 57, 63. The upgraded version below moves each call inside an if block.
Upgrade and export
Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.
- Upgrading to v5 renames lowest → ta.lowest, highest → ta.highest, valuewhen → ta.valuewhen, atr → ta.atr, rsi → ta.rsi, change → ta.change, rma → ta.rma, abs → math.abs.
- 3 strategy calls use the when = argument (line 56, 57, 63). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 6 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 7: //@version=4 → //@version=5
- Line 9: input(type = input.integer) → input.int()
- Line 19: lowest → ta.lowest
- Line 20: highest → ta.highest
- Line 22: valuewhen → ta.valuewhen
- Line 22: valuewhen → ta.valuewhen
19 more edits
- Line 22: atr → ta.atr
- Line 23: valuewhen → ta.valuewhen
- Line 23: valuewhen → ta.valuewhen
- Line 23: atr → ta.atr
- Line 24: valuewhen → ta.valuewhen
- Line 24: valuewhen → ta.valuewhen
- Line 24: atr → ta.atr
- Line 25: valuewhen → ta.valuewhen
- Line 25: valuewhen → ta.valuewhen
- Line 25: atr → ta.atr
- Line 31: rsi → ta.rsi
- Line 38: change → ta.change
- Line 39: change → ta.change
- Line 42: rma → ta.rma
- Line 42: tr → ta.tr
- Line 43: rma → ta.rma
- Line 44: rma → ta.rma
- Line 49: rma → ta.rma
- Line 49: abs → math.abs
⋯// and selling the OS) wihout stop loss.-//@version=4+//@version=5strategy("ADX + RSI Strat", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=100, commission_value=0.04, calc_on_every_tick=false)-direction = input(0, title = "Strategy Direction", type=input.integer, minval=-1, maxval=1)+direction = input.int(0, title = "Strategy Direction", minval=-1, maxval=1)strategy.risk.allow_entry_in(direction == 0 ? strategy.direction.all : (direction < 0 ? strategy.direction.short : strategy.direction.long))⋯//SL & TP Calculations-SwingLow=lowest(i_SwingLookback)+SwingLow=ta.lowest(i_SwingLookback)-SwingHigh=highest(i_SwingLookback)+SwingHigh=ta.highest(i_SwingLookback)bought=strategy.position_size != strategy.position_size[1]-LSL=valuewhen(bought, SwingLow, 0)-((valuewhen(bought, atr(14), 0))*i_SLExpander)+LSL=ta.valuewhen(bought, SwingLow, 0)-((ta.valuewhen(bought, ta.atr(14), 0))*i_SLExpander)-SSL=valuewhen(bought, SwingHigh, 0)+((valuewhen(bought, atr(14), 0))*i_SLExpander)+SSL=ta.valuewhen(bought, SwingHigh, 0)+((ta.valuewhen(bought, ta.atr(14), 0))*i_SLExpander)-lTP=strategy.position_avg_price + (strategy.position_avg_price-(valuewhen(bought, SwingLow, 0))+((valuewhen(bought, atr(14), 0))*i_TPExpander))+lTP=strategy.position_avg_price + (strategy.position_avg_price-(ta.valuewhen(bought, SwingLow, 0))+((ta.valuewhen(bought, ta.atr(14), 0))*i_TPExpander))-sTP=strategy.position_avg_price - (valuewhen(bought, SwingHigh, 0)-strategy.position_avg_price)-((valuewhen(bought, atr(14), 0))*i_TPExpander)+sTP=strategy.position_avg_price - (ta.valuewhen(bought, SwingHigh, 0)-strategy.position_avg_price)-((ta.valuewhen(bought, ta.atr(14), 0))*i_TPExpander)islong=strategy.position_size > 0⋯//RSI Calculations-RSI=rsi(close, 7)+RSI=ta.rsi(close, 7)OS=input(20, step=5)⋯dirmov(len) =>- up = change(high)+ up = ta.change(high)- down = -change(low)+ down = -ta.change(low) plusDM = na(up) ? na : (up > down and up > 0 ? up : 0) minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)- truerange = rma(tr, len)+ truerange = ta.rma(ta.tr, len)- plus = fixnan(100 * rma(plusDM, len) / truerange)+ plus = fixnan(100 * ta.rma(plusDM, len) / truerange)- minus = fixnan(100 * rma(minusDM, len) / truerange)+ minus = fixnan(100 * ta.rma(minusDM, len) / truerange) [plus, minus]⋯ sum = plus + minus- adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)+ adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)sig = adx(dilen, adxlen)⋯Same 6 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 12: input() → input.bool()
- Line 13: input() → input.int()
- Line 14: input() → input.int()
- Line 15: input() → input.int()
- Line 16: input() → input.bool()
- Line 17: input() → input.bool()
8 more edits
- Line 32: input() → input.int()
- Line 33: input() → input.int()
- Line 35: input() → input.int()
- Line 36: input() → input.int()
- Line 51: input() → input.int()
- Line 56: when = i_reverse?SELL:BUY → if block
- Line 57: when = not i_reverse?SELL:BUY → if block
- Line 63: when = sig > adxlevel → if block
⋯//SL & TP Inputs-i_SL=input(false, title="Use Swing Lo/Hi Stop Loss & Take Profit")+i_SL=input.bool(false, title="Use Swing Lo/Hi Stop Loss & Take Profit")-i_SwingLookback=input(20, title="Swing Lo/Hi Lookback")+i_SwingLookback=input.int(20, title="Swing Lo/Hi Lookback")-i_SLExpander=input(defval=0, step=.2, title="SL Expander")+i_SLExpander=input.int(defval=0, step=.2, title="SL Expander")-i_TPExpander=input(defval=0, step=.2, title="TP Expander")+i_TPExpander=input.int(defval=0, step=.2, title="TP Expander")-i_reverse=input(true, title="Reverse Trades")+i_reverse=input.bool(true, title="Reverse Trades")-i_ADXClose=input(true, title="ADX Close")+i_ADXClose=input.bool(true, title="ADX Close")//SL & TP Calculations⋯RSI=ta.rsi(close, 7)-OS=input(20, step=5)+OS=input.int(20, step=5)-OB=input(80, step=5)+OB=input.int(80, step=5)//ADX Calculations-adxlen = input(14, title="ADX Smoothing")+adxlen = input.int(14, title="ADX Smoothing")-dilen = input(200, title="DI Length")+dilen = input.int(200, title="DI Length")dirmov(len) =>⋯sig = adx(dilen, adxlen)-adxlevel=input(20, step=5)+adxlevel=input.int(20, step=5)//Entry Logic⋯//Entries-strategy.entry("long", strategy.long, when=i_reverse?SELL:BUY)+if i_reverse?SELL:BUY+ strategy.entry("long", strategy.long)-strategy.entry("short", strategy.short, when=not i_reverse?SELL:BUY)+if not i_reverse?SELL:BUY+ strategy.entry("short", strategy.short)//Exits⋯if i_ADXClose- strategy.close_all(when=sig > adxlevel)+ if sig > adxlevel+ strategy.close_all()//Plotss⋯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: 6 before, 19 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.
⋯// and selling the OS) wihout stop loss.-//@version=5+//@version=6strategy("ADX + RSI Strat", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=100, commission_value=0.04, calc_on_every_tick=false)⋯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 |
|---|---|---|---|
| Strategy Direction | 0 | int (written input.integer) · min -1 · max 1 | line 10 |
| Use Swing Lo/Hi Stop Loss & Take Profit | false | bool | exit longexit exit shortexit line 59 SL TP |
| Swing Lo/Hi Lookback | 20 | int | SwingLow SwingHigh |
| SL Expander | 0 | int · step .2 | LSL SSL |
| TP Expander | 0 | int · step .2 | lTP sTP |
| Reverse Trades | true | bool | entry long entry short |
| ADX Close | true | bool | close_all line 62 |
| OS (no label in the script) | 20 | int · step 5 | BUY |
| OB (no label in the script) | 80 | int · step 5 | SELL |
| ADX Smoothing | 14 | int | adx sig |
| DI Length | 200 | int | plus minus sig |
| Adxlevel (no label in the script) | 20 | int · step 5 | BUY SELL close_all |
What it calculates
- Lowest (
lowest,ta.lowestin v5)Swing Lo/Hi Lookback (20)SwingLow · line 19 - Highest (
highest,ta.highestin v5)Swing Lo/Hi Lookback (20)SwingHigh · line 20 - Valuewhen / ATR (
valuewhen,ta.valuewhenin v5)bought, SwingLow, 0LSL · line 22 - Valuewhen / ATR (
valuewhen,ta.valuewhenin v5)bought, SwingHigh, 0SSL · line 23 - Valuewhen / ATR (
valuewhen,ta.valuewhenin v5)bought, SwingLow, 0lTP · line 24 - Valuewhen / ATR (
valuewhen,ta.valuewhenin v5)bought, SwingHigh, 0sTP · line 25 - RSI (
rsi,ta.rsiin v5)close, 7RSI · line 31 - Change (
change,ta.changein v5)highup · line 38 - Change (
change,ta.changein v5)lowdown · line 39 - RMA (
rma,ta.rmain v5)tr, lentruerange · line 42 - RMA (
rma,ta.rmain v5)abs(plus - minus) / (sum == 0 ? 1 : sum), ADX Smoothing (14)adx · line 49
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 | i_reverse?SELL:BUY | On | 56 |
| Open short short | not i_reverse?SELL:BUY | On | 57 |
| Exit longexit stop and limit | i_SL | Off “Use Swing Lo/Hi Stop Loss & Take Profit” | 60 |
| Exit shortexit stop and limit | i_SL | Off “Use Swing Lo/Hi Stop Loss & Take Profit” | 61 |
| Close | sig > adxlevel | On “ADX Close” | 63 |
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 | −3.5% | +102.5% | 6 | 33% | 0.92 | 36.3% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +14.7% | +5.5% | 4 | 50% | 2.78 | 6.3% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −77.3% | +9.8% | 10 | 40% | 0.15 | 78.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +33.9% | +8.4% | 4 | 75% | 3.88 | 14.6% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −78.8% | +16.7% | 12 | 42% | 0.18 | 79.7% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −9.9% | +21.5% | 8 | 25% | 0.77 | 39.6% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −17.5% | +148.3% | 8 | 38% | 0.65 | 38.8% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +11.7% | +17.1% | 8 | 50% | 1.64 | 13.5% | 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 | 0 | — | $0.00 |
| Sideways market | 3 | 33% | $12.43 |
| Falling market | 3 | 33% | −$15.88 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
32.8%
The 95th percentile of the worst drawdown when the same 6 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 16.2% to 32.8%; the tick is what actually happened, 32.8%. Order luck alone moves it this much.
All 6 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 21 Oct 2024 69,032.00 | 26 Feb 2025 88,680.40 | 128 | $28.36 |
| short | 26 Feb 2025 88,680.40 | 23 Apr 2025 93,443.00 | 56 | −$7.00 |
| long | 23 Apr 2025 93,443.00 | 21 Nov 2025 86,637.20 | 212 | −$8.93 |
| short | 21 Nov 2025 86,637.20 | 6 Jan 2026 93,859.70 | 46 | −$9.47 |
| long | 6 Jan 2026 93,859.70 | 1 Feb 2026 78,741.10 | 26 | −$16.66 |
| short | 1 Feb 2026 78,741.10 | 20 Aug 2026 69,334.80 | 200 | $10.24 |
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 |
|---|---|---|---|---|---|
| ADX + RSI Strat this page | — | v4 strategy | −3.5% | 6 | 36.3% |
| Delayed RSI Strategy by tweakerID | lowest highest valuewhen atr rsi | v4 strategy | −40.4% | 7 | 42.2% |
| R19 STRATEGY by SenatorVonShaft | atr rma highest lowest rsi | v4 strategy | No trades on this market | ||
| Double Bollinger Strategy by tweakerID | lowest highest valuewhen atr rsi | v4 strategy | −29.6% | 61 | 50.1% |
| HiLo Extension by Saravanan_Ragavan | valuewhen lowest highest rma | v4 strategy | No trades on this market | ||
| Positive Bars - Strategy by tweakerID | lowest highest valuewhen atr | v4 strategy | −40.3% | 7 | 54.7% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 25 ms | Yes 1,000 bars in 56 ms | Not measured |
| PineTS | No v5 and v6 only | — | 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. PineTS runs Pine Script v5 and v6 only.
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/// © tweakerID// This is a strategy that uses the 7 Period RSI to buy when the indicator is shown as oversold (OS) and sells when // the index marks overbought (OB). It also uses the ADX to determine whether the trend is ranging or trending// and filters out the trending trades. Seems to work better for automated trading when the logic is inversed (buying OB // and selling the OS) wihout stop loss.//@version=4strategy("ADX + RSI Strat", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=100, commission_value=0.04, calc_on_every_tick=false)direction = input(0, title = "Strategy Direction", type=input.integer, minval=-1, maxval=1)strategy.risk.allow_entry_in(direction == 0 ? strategy.direction.all : (direction < 0 ? strategy.direction.short : strategy.direction.long))//SL & TP Inputsi_SL=input(false, title="Use Swing Lo/Hi Stop Loss & Take Profit")i_SwingLookback=input(20, title="Swing Lo/Hi Lookback")i_SLExpander=input(defval=0, step=.2, title="SL Expander")i_TPExpander=input(defval=0, step=.2, title="TP Expander")i_reverse=input(true, title="Reverse Trades")i_ADXClose=input(true, title="ADX Close")//SL & TP CalculationsSwingLow=lowest(i_SwingLookback)SwingHigh=highest(i_SwingLookback)bought=strategy.position_size != strategy.position_size[1]LSL=valuewhen(bought, SwingLow, 0)-((valuewhen(bought, atr(14), 0))*i_SLExpander)SSL=valuewhen(bought, SwingHigh, 0)+((valuewhen(bought, atr(14), 0))*i_SLExpander)lTP=strategy.position_avg_price + (strategy.position_avg_price-(valuewhen(bought, SwingLow, 0))+((valuewhen(bought, atr(14), 0))*i_TPExpander))sTP=strategy.position_avg_price - (valuewhen(bought, SwingHigh, 0)-strategy.position_avg_price)-((valuewhen(bought, atr(14), 0))*i_TPExpander)islong=strategy.position_size > 0isshort=strategy.position_size < 0SL= islong ? LSL : isshort ? SSL : naTP= islong ? lTP : isshort ? sTP : na//RSI CalculationsRSI=rsi(close, 7)OS=input(20, step=5)OB=input(80, step=5)//ADX Calculationsadxlen = input(14, title="ADX Smoothing")dilen = input(200, title="DI Length")dirmov(len) => up = change(high) down = -change(low) plusDM = na(up) ? na : (up > down and up > 0 ? up : 0) minusDM = na(down) ? na : (down > up and down > 0 ? down : 0) truerange = rma(tr, len) plus = fixnan(100 * rma(plusDM, len) / truerange) minus = fixnan(100 * rma(minusDM, len) / truerange) [plus, minus]adx(dilen, adxlen) => [plus, minus] = dirmov(dilen) sum = plus + minus adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)sig = adx(dilen, adxlen)adxlevel=input(20, step=5)//Entry LogicBUY = sig < adxlevel and (RSI < OS) SELL = sig < adxlevel and (RSI > OB) //Entriesstrategy.entry("long", strategy.long, when=i_reverse?SELL:BUY)strategy.entry("short", strategy.short, when=not i_reverse?SELL:BUY)//Exitsif i_SL strategy.exit("longexit", "long", stop=SL, limit=TP) strategy.exit("shortexit", "short", stop=SL, limit=TP)if i_ADXClose strategy.close_all(when=sig > adxlevel)//Plotssplot(i_SL ? SL : na, color=color.red, style=plot.style_cross, title="SL")plot(i_SL ? TP : na, color=color.green, style=plot.style_cross, title="TP")plotshape(BUY ? 1 : na, style=shape.triangleup, location=location.belowbar, color=color.green, title="Bullish Setup")plotshape(SELL ? 1 : na, style=shape.triangledown, location=location.abovebar, color=color.red, title="Bearish Setup")