Pine Script v4 · Strategy
SuperTREX strategy
A Pine Script v4 strategy built on RSI (p, 14), Crossover (vrsi, 35) and Crossunder (vrsi, 70). It has 6 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 25% of equity and pays 0.075% commission. 15 settings; defaults include Strategy Direction long, Length 14 and Over Sold 35.
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 SuperTREX strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 3 of 3 trades.
- How did SuperTREX strategy do on BTCUSDT 1D with default settings?
- −3.82%
- From 26 Jan 2024 to 21 Oct 2026, over 3 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can SuperTREX strategy be converted to Pine Script v5?
- No
- The automatic step from v4 to v5 changes the results, so no v5 download is offered; the edits are shown below.
- Does SuperTREX strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 71, 72, 73, 74.
- Does SuperTREX strategy run in Zeal Terminal?
- Yes
- It compiles in 19 ms and runs 1,000 bars in 62 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- buy
- sell
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −3.82%
- −$381.78
- Buy and hold
- +102.5%
- same period
- Closed trades
- 3
- 2 won · 1 lost
- Profitable
- 66.7%
- of closed trades
- Profit factor
- 0.31
- gross profit ÷ gross loss
- Max drawdown
- 6.11%
- −$610.52
One run with the author's default settings, $10,000 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.
-
Note
4 settings have no label
In the settings dialog length (line 8), overSold (line 9), overBought (line 10), HTF (line 11) show an empty name. The labels on this page come from the variable names.
-
Note
Uses transp =, which v5 removed line 50
transp = on line 50, 51. v5 folds the transparency into the colour with color.new(c, transp).
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 change → ta.change, rsi → ta.rsi, crossover → ta.crossover, crossunder → ta.crossunder, atr → ta.atr, max → math.max, min → math.min, round → math.round.
v4 → v5
DiffersTrades differ: 3 before, 10 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 11: input(type = input.resolution) → input.timeframe()
- Line 12: change → ta.change
- Line 14: rsi → ta.rsi
- Line 18: crossover → ta.crossover
- Line 19: crossunder → ta.crossunder
9 more edits
- Line 31: atr → ta.atr
- Line 32: atr → ta.atr
- Line 34: max → math.max
- Line 36: min → math.min
- Line 43: crossover → ta.crossover
- Line 44: crossunder → ta.crossunder
- Line 50: transp = 0 → color.new(color.green, 0)
- Line 51: transp = 0 → color.new(color.red, 0)
- Line 61: round → math.round
⋯// © RafaelZioni-//@version=4+//@version=5strategy(title = "SuperTREX strategy", overlay = true, pyramiding=1,initial_capital = 10000, default_qty_type= strategy.percent_of_equity, default_qty_value = 25, calc_on_order_fills=false, slippage=0,commission_type=strategy.commission.percent,commission_value=0.075)⋯overBought = input( 70 )-HTF = input("W", type=input.resolution)+HTF = input.timeframe("W")-ti = change( time(HTF) ) != 0+ti = ta.change( time(HTF) ) != 0p = fixnan( ti ? close : na )-vrsi = rsi(p, length)+vrsi = ta.rsi(p, length)price = close⋯var bool short = na-long :=crossover(vrsi,overSold) +long :=ta.crossover(vrsi,overSold) -short := crossunder(vrsi,overBought)+short := ta.crossunder(vrsi,overBought)var float last_open_long = na⋯// CALCULATIONS //-up_lev =xy - (st_mult * atr(st_period))+up_lev =xy - (st_mult * ta.atr(st_period))-dn_lev =xy + (st_mult * atr(st_period))+dn_lev =xy + (st_mult * ta.atr(st_period))up_trend = 0.0-up_trend := entry_value[1] > up_trend[1] ? max(up_lev, up_trend[1]) : up_lev+up_trend := entry_value[1] > up_trend[1] ? math.max(up_lev, up_trend[1]) : up_levdown_trend = 0.0-down_trend := entry_value1[1] < down_trend[1] ? min(dn_lev, down_trend[1]) : dn_lev+down_trend := entry_value1[1] < down_trend[1] ? math.min(dn_lev, down_trend[1]) : dn_lev// Calculate trend var⋯plot(xy,color = trend == 1 ? color.green : color.red)-buy=crossover( close, st_line) +buy=ta.crossover( close, st_line) -sell1=crossunder(close, st_line) +sell1=ta.crossunder(close, st_line) ⋯// STRATEGY-plotshape(buy , title="buy", text="Buy", color=color.green, style=shape.labelup, location=location.belowbar, size=size.small, textcolor=color.white, transp=0) //plot for buy icon+plotshape(buy , title="buy", text="Buy", color=color.new(color.green, 0), style=shape.labelup, location=location.belowbar, size=size.small, textcolor=color.white) //plot for buy icon-plotshape(sell, title="sell", text="Sell", color=color.red, style=shape.labeldown, location=location.abovebar, size=size.small, textcolor=color.white, transp=0) //plot for sell icon+plotshape(sell, title="sell", text="Sell", color=color.new(color.red, 0), style=shape.labeldown, location=location.abovebar, size=size.small, textcolor=color.white) //plot for sell icon// Take profit⋯per(pcnt) =>- strategy.position_size != 0 ? round(pcnt / 100 * strategy.position_avg_price / syminfo.mintick) : float(na)+ strategy.position_size != 0 ? math.round(pcnt / 100 * strategy.position_avg_price / syminfo.mintick) : float(na)stoploss=input(title=" stop loss", defval=25, minval=0.01)⋯Clean v5
VerifiedSame 10 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 5: input() → input.string()
- Line 8: input() → input.int()
- Line 9: input() → input.int()
- Line 10: input() → input.int()
- Line 28: input() → input.int()
- Line 29: input() → input.int()
8 more edits
- Line 62: input() → input.int()
- Line 64: input() → input.int()
- Line 65: input() → input.int()
- Line 66: input() → input.int()
- Line 67: input() → input.int()
- Line 68: input() → input.int()
- Line 69: input() → input.int()
- Line 70: input() → input.int()
⋯strategy(title = "SuperTREX strategy", overlay = true, pyramiding=1,initial_capital = 10000, default_qty_type= strategy.percent_of_equity, default_qty_value = 25, calc_on_order_fills=false, slippage=0,commission_type=strategy.commission.percent,commission_value=0.075)-strat_dir_input = input(title="Strategy Direction", defval="long", options=["long", "short", "all"])+strat_dir_input = input.string(title="Strategy Direction", defval="long", options=["long", "short", "all"])strat_dir_value = strat_dir_input == "long" ? strategy.direction.long : strat_dir_input == "short" ? strategy.direction.short : strategy.direction.allstrategy.risk.allow_entry_in(strat_dir_value)-length = input( 14 )+length = input.int( 14 )-overSold = input( 35 )+overSold = input.int( 35 )-overBought = input( 70 )+overBought = input.int( 70 )HTF = input.timeframe("W")⋯// INPUTS //-st_mult = input(4, title = 'SuperTrend Multiplier', minval = 0, maxval = 100, step = 0.01)+st_mult = input.int(4, title = 'SuperTrend Multiplier', minval = 0, maxval = 100, step = 0.01)-st_period = input(10, title = 'SuperTrend Period', minval = 1)+st_period = input.int(10, title = 'SuperTrend Period', minval = 1)// CALCULATIONS //⋯ strategy.position_size != 0 ? math.round(pcnt / 100 * strategy.position_avg_price / syminfo.mintick) : float(na)-stoploss=input(title=" stop loss", defval=25, minval=0.01)+stoploss=input.int(title=" stop loss", defval=25, minval=0.01)los = per(stoploss)-q1=input(title=" qty_percent1", defval=25, minval=1)+q1=input.int(title=" qty_percent1", defval=25, minval=1)-q2=input(title=" qty_percent2", defval=25, minval=1)+q2=input.int(title=" qty_percent2", defval=25, minval=1)-q3=input(title=" qty_percent3", defval=25, minval=1)+q3=input.int(title=" qty_percent3", defval=25, minval=1)-tp1=input(title=" Take profit1", defval=10, minval=0.01)+tp1=input.int(title=" Take profit1", defval=10, minval=0.01)-tp2=input(title=" Take profit2", defval=20, minval=0.01)+tp2=input.int(title=" Take profit2", defval=20, minval=0.01)-tp3=input(title=" Take profit3", defval=50, minval=0.01)+tp3=input.int(title=" Take profit3", defval=50, minval=0.01)-tp4=input(title=" Take profit4", defval=100, minval=0.01)+tp4=input.int(title=" Take profit4", defval=100, minval=0.01)strategy.exit("x1", qty_percent = q1, profit = per(tp1), loss = los)⋯Does it run in Pine Script v6?
No
The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L16:C0: a 'bool' cannot be 'na' in Pine Script v6 — declare it as true or false. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © RafaelZioni-//@version=5+//@version=6strategy(title = "SuperTREX strategy", overlay = true, pyramiding=1,initial_capital = 10000, default_qty_type= strategy.percent_of_equity, default_qty_value = 25, calc_on_order_fills=false, slippage=0,commission_type=strategy.commission.percent,commission_value=0.075)⋯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 | long | string · options ["long", "short", "all"] | strat_dir_value |
| Length (no label in the script) | 14 | int | vrsi |
| Over Sold (no label in the script) | 35 | int | long |
| Over Bought (no label in the script) | 70 | int | short |
| HTF (no label in the script) | W | timeframe (written input.resolution) | ti |
| SuperTrend Multiplier | 4 | int · min 0 · max 100 · step 0.01 | up_lev dn_lev |
| SuperTrend Period | 10 | int · min 1 | up_lev dn_lev |
| stop loss | 25 | int · min 0.01 | los |
| qty_percent1 | 25 | int · min 1 | exit x1 |
| qty_percent2 | 25 | int · min 1 | exit x2 |
| qty_percent3 | 25 | int · min 1 | exit x3 |
| Take profit1 | 10 | int · min 0.01 | exit x1 |
| Take profit2 | 20 | int · min 0.01 | exit x2 |
| Take profit3 | 50 | int · min 0.01 | exit x3 |
| Take profit4 | 100 | int · min 0.01 | exit x4 |
What it calculates
- RSI (
rsi,ta.rsiin v5)p, Length (14)vrsi · line 14 - Crossover (
crossover,ta.crossoverin v5)vrsi, Over Sold (35)long · line 18 - Crossunder (
crossunder,ta.crossunderin v5)vrsi, Over Bought (70)short · line 19 - ATR (
atr,ta.atrin v5)SuperTrend Period (10)up_lev · line 31 - ATR (
atr,ta.atrin v5)SuperTrend Period (10)dn_lev · line 32 - Crossover (
crossover,ta.crossoverin v5)close, st_linebuy · line 43 - Crossunder (
crossunder,ta.crossunderin v5)close, st_linesell1 · line 44
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 buy long | l | On | 57 |
| Open sell short | s1 | On | 59 |
| Exit x1 stop and limit | every bar | On | 71 |
| Exit x2 stop and limit | every bar | On | 72 |
| Exit x3 stop and limit | every bar | On | 73 |
| Exit x4 stop and limit | every bar | On | 74 |
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.8% | +102.5% | 3 | 67% | 0.31 | 6.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.1% | +5.5% | 3 | 67% | 1.09 | 2.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +4.8% | +9.8% | 14 | 57% | 1.48 | 7.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +4.6% | +8.4% | 5 | 80% | 2.89 | 3.1% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +10.1% | +16.7% | 7 | 86% | 103.81 | 1.7% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +8.5% | +21.5% | 6 | 100% | — | 2.6% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.4% | +148.3% | 4 | 75% | 0.86 | 3.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +2.8% | +17.1% | 4 | 75% | 2.20 | 3.2% | 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 | 1 | 0% | −$555.94 |
| Falling market | 2 | 100% | $175.04 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
5.6%
The 95th percentile of the worst drawdown when the same 3 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 5.6% to 5.6%; the tick is what actually happened, 5.6%. Order luck alone moves it this much.
All 3 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 23 Apr 2026 78,178.20 | 7 Jun 2026 60,884.60 | 45 | −$555.94 |
| long | 18 Aug 2026 64,532.10 | 20 Aug 2026 70,985.30 | 2 | $58.05 |
| long | 18 Aug 2026 64,532.10 | 21 Aug 2026 77,438.50 | 3 | $116.99 |
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 |
|---|---|---|---|---|---|
| SuperTREX strategy this page | — | v4 strategy | −3.8% | 3 | 6.1% |
| [KL] RSI 14 + 10 Strategy by DojiEmoji | atr rsi | v4 strategy | +4.2% | 27 | 39.7% |
| MA cross strategy by Glenn234 | rsi atr | v5 strategy | −606.2% | 35 | 656.0% |
| VWMA_withATRstops_strategy by ediks123 | rsi atr | v4 strategy | No trades on this market | ||
| Mathias & Christer Timeframe RSI by mattehalen | rsi atr | v4 strategy | −0.7% | 18 | 0.7% |
| Estrategia Larry Connors [JoseMetal] by JoseMetal | rsi atr | v5 strategy | +2.7% | 24 | 2.4% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 19 ms | Yes 1,000 bars in 62 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/// © RafaelZioni//@version=4strategy(title = "SuperTREX strategy", overlay = true, pyramiding=1,initial_capital = 10000, default_qty_type= strategy.percent_of_equity, default_qty_value = 25, calc_on_order_fills=false, slippage=0,commission_type=strategy.commission.percent,commission_value=0.075)strat_dir_input = input(title="Strategy Direction", defval="long", options=["long", "short", "all"])strat_dir_value = strat_dir_input == "long" ? strategy.direction.long : strat_dir_input == "short" ? strategy.direction.short : strategy.direction.allstrategy.risk.allow_entry_in(strat_dir_value)length = input( 14 )overSold = input( 35 )overBought = input( 70 )HTF = input("W", type=input.resolution)ti = change( time(HTF) ) != 0p = fixnan( ti ? close : na )vrsi = rsi(p, length)price = closevar bool long = navar bool short = nalong :=crossover(vrsi,overSold) short := crossunder(vrsi,overBought)var float last_open_long = navar float last_open_short = nalast_open_long := long ? close : nz(last_open_long[1])last_open_short := short ? close : nz(last_open_short[1])entry_value =last_open_longentry_value1=last_open_shortxy=(entry_value+entry_value)/2// INPUTS //st_mult = input(4, title = 'SuperTrend Multiplier', minval = 0, maxval = 100, step = 0.01)st_period = input(10, title = 'SuperTrend Period', minval = 1)// CALCULATIONS //up_lev =xy - (st_mult * atr(st_period))dn_lev =xy + (st_mult * atr(st_period))up_trend = 0.0up_trend := entry_value[1] > up_trend[1] ? max(up_lev, up_trend[1]) : up_levdown_trend = 0.0down_trend := entry_value1[1] < down_trend[1] ? min(dn_lev, down_trend[1]) : dn_lev// Calculate trend vartrend = 0trend := close > down_trend[1] ? 1: close < up_trend[1] ? -1 : nz(trend[1], 1)// Calculate SuperTrend Linest_line = trend ==1 ? up_trend : down_trendplot(xy,color = trend == 1 ? color.green : color.red)buy=crossover( close, st_line) sell1=crossunder(close, st_line) buy1=buy//sell=sell1// STRATEGYplotshape(buy , title="buy", text="Buy", color=color.green, style=shape.labelup, location=location.belowbar, size=size.small, textcolor=color.white, transp=0) //plot for buy iconplotshape(sell, title="sell", text="Sell", color=color.red, style=shape.labeldown, location=location.abovebar, size=size.small, textcolor=color.white, transp=0) //plot for sell icon// Take profit//l = buy s1=sell if l strategy.entry("buy", strategy.long)if s1 strategy.entry("sell", strategy.short)per(pcnt) => strategy.position_size != 0 ? round(pcnt / 100 * strategy.position_avg_price / syminfo.mintick) : float(na)stoploss=input(title=" stop loss", defval=25, minval=0.01)los = per(stoploss)q1=input(title=" qty_percent1", defval=25, minval=1)q2=input(title=" qty_percent2", defval=25, minval=1)q3=input(title=" qty_percent3", defval=25, minval=1)tp1=input(title=" Take profit1", defval=10, minval=0.01)tp2=input(title=" Take profit2", defval=20, minval=0.01)tp3=input(title=" Take profit3", defval=50, minval=0.01)tp4=input(title=" Take profit4", defval=100, minval=0.01)strategy.exit("x1", qty_percent = q1, profit = per(tp1), loss = los)strategy.exit("x2", qty_percent = q2, profit = per(tp2), loss = los)strategy.exit("x3", qty_percent = q3, profit = per(tp3), loss = los)strategy.exit("x4", profit = per(tp4), loss = los)