Pine Script v5 · Strategy
Trading range display with Box
A Pine Script v5 strategy built on SMA (close, 14), Crossover (a, b) and Crossunder (a, b). It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 10% of equity and pays 0.04% commission. 2 settings.
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 Trading range display with Box repaint?
- Yes
- At least one pattern in the source can repaint: intrabar recalculation.
- How did Trading range display with Box do on BTCUSDT 1D with default settings?
- +0.68%
- From 26 Jan 2024 to 21 Oct 2026, over 27 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Trading range display with Box 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: 27 before, 27 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 Trading range display with Box use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Trading range display with Box run in Zeal Terminal?
- Yes
- It compiles in 41 ms and runs 1,000 bars in 58 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +0.68%
- $6.84
- Buy and hold
- +102.5%
- same period
- Closed trades
- 27
- 10 won · 17 lost
- Profitable
- 37.0%
- of closed trades
- Profit factor
- 1.07
- gross profit ÷ gross loss
- Max drawdown
- 3.88%
- −$39.76
One run with the author's default settings, $1,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 stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
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 |
|---|---|---|---|
| Color growth (no label in the script) | DEFAULT_COLOR_GROWTH | color | color_border_growth colorBD |
| Body | DEFAULT_COLOR_FALL | color | color_border_fall colorBD |
What it calculates
- SMAclose, 14
a · line 7 - SMAclose, 20
b · line 8 - Crossovera, b
longCondition · line 11 - Crossundera, b
closeCondition · line 14 - Highesthigh, len_L
high_session_L · line 49 - Lowestlow, len_L
low_session_L · line 50 - Highesthigh, len_S
high_session_S · line 52 - Lowestlow, len_S
low_session_S · line 53
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 L long | (longCondition) | On | 13 |
| Close L | (closeCondition) | On | 16 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
Yes
At least one pattern in the source can repaint; it is marked Check below.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- CheckIntrabar recalculationcalc_on_every_tick = true; reads realtime-only state. Live values can differ from history.
- ClearOrder timingprocess_orders_on_close = true: orders fill at the signal bar's close.
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.7% | +102.5% | 27 | 37% | 1.07 | 3.9% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.7% | +5.5% | 32 | 47% | 1.21 | 2.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +5.2% | +9.8% | 32 | 38% | 1.44 | 6.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +1.5% | +8.4% | 31 | 35% | 1.37 | 1.4% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +12.1% | +16.7% | 28 | 50% | 1.92 | 5.4% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +1.6% | +21.5% | 32 | 34% | 1.27 | 2.3% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +1.2% | +148.3% | 28 | 46% | 1.13 | 4.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.3% | +17.1% | 29 | 41% | 1.14 | 1.9% | 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 | 8 | 63% | $11.95 |
| Sideways market | 13 | 8% | −$49.14 |
| Falling market | 6 | 67% | $44.03 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
7.1%
The 95th percentile of the worst drawdown when the same 27 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 3.1% to 7.1%; the tick is what actually happened, 3.6%. Order luck alone moves it this much.
All 27 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 2 Apr 2024 65,464.00 | 12 Apr 2024 67,116.50 | 10 | $2.44 |
| long | 15 May 2024 66,206.50 | 8 Jun 2024 69,310.40 | 24 | $4.62 |
| long | 9 Jun 2024 69,648.20 | 18 Jun 2024 65,175.30 | 9 | −$6.54 |
| long | 19 Jul 2024 66,660.00 | 4 Aug 2024 58,161.00 | 16 | −$12.83 |
| long | 21 Aug 2024 61,156.10 | 6 Sep 2024 53,962.90 | 16 | −$11.69 |
| long | 19 Sep 2024 62,948.00 | 8 Oct 2024 62,160.50 | 19 | −$1.30 |
| long | 17 Oct 2024 67,421.80 | 27 Dec 2024 94,299.00 | 71 | $38.75 |
| long | 13 Jan 2025 94,536.10 | 20 Jan 2025 102,260.00 | 7 | $8.19 |
| long | 21 Jan 2025 106,144.00 | 4 Feb 2025 97,763.10 | 14 | −$8.14 |
| long | 23 Feb 2025 96,258.00 | 24 Feb 2025 91,552.90 | 1 | −$5.03 |
| long | 25 Mar 2025 87,392.90 | 6 Apr 2025 78,430.00 | 12 | −$10.42 |
| long | 20 Apr 2025 85,179.30 | 5 Jun 2025 101,509.00 | 46 | $19.04 |
| long | 16 Jun 2025 106,795.00 | 23 Jun 2025 105,334.00 | 7 | −$1.47 |
| long | 2 Jul 2025 108,850.00 | 30 Jul 2025 117,840.00 | 28 | $8.30 |
| long | 16 Aug 2025 117,381.00 | 25 Aug 2025 110,112.00 | 9 | −$6.42 |
| long | 12 Sep 2025 116,029.00 | 27 Sep 2025 109,636.00 | 15 | −$5.68 |
| long | 6 Oct 2025 124,659.00 | 17 Oct 2025 106,432.00 | 11 | −$14.86 |
| long | 1 Nov 2025 110,098.00 | 7 Nov 2025 103,339.00 | 6 | −$6.20 |
| long | 6 Dec 2025 89,236.80 | 17 Dec 2025 86,243.20 | 11 | −$3.40 |
| long | 1 Jan 2026 88,839.10 | 27 Jan 2026 89,250.00 | 26 | $0.38 |
| long | 8 Mar 2026 65,971.20 | 29 Mar 2026 66,010.90 | 21 | −$0.02 |
| long | 11 Apr 2026 73,043.20 | 20 May 2026 77,552.20 | 39 | $6.01 |
| long | 21 Jun 2026 63,312.00 | 28 Jun 2026 59,577.00 | 7 | −$5.94 |
| long | 10 Jul 2026 64,161.70 | 2 Aug 2026 63,570.00 | 23 | −$0.99 |
| long | 15 Aug 2026 63,086.00 | 11 Sep 2026 77,225.70 | 27 | $22.02 |
| long | 16 Sep 2026 76,206.00 | 17 Sep 2026 76,417.00 | 1 | $0.20 |
| long | 23 Sep 2026 84,397.60 | 9 Oct 2026 82,635.50 | 16 | −$2.19 |
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 |
|---|---|---|---|---|---|
| Trading range display with Box this page | — | v5 strategy | +0.7% | 27 | 3.9% |
| ChBrkOutStrategySMA by anshuanshu333 | sma highest lowest | v4 strategy | −4.5% | 42 | 20.9% |
| KDJ Strategy @ionvolution by ionvolution | sma highest lowest | v5 strategy | No trades on this market | ||
| Open Drive by Marcns_ | highest lowest sma | v5 strategy | No trades on this market | ||
| Trend Bounce [racer8] by racer8 | highest lowest sma | v4 strategy | +2.4% | 22 | 32.2% |
| Lawyers Trend Pro Strategy by ErdemDemir | sma lowest highest | v4 strategy | +12.1% | 102 | 41.7% |
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.
- No v6-only features. v6 is not needed to run this script.
Clean v5
VerifiedNothing to change.
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: 27 before, 27 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.
⋯// © potatoshop-//@version=5+//@version=6strategy("Trading range display with Box","Box", overlay = true,process_orders_on_close=true,calc_on_every_tick=true, max_boxes_count=500, initial_capital=1000, default_qty_type=strategy.percent_of_equity,⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 41 ms | Yes 1,000 bars in 58 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/// © potatoshop//@version=5strategy("Trading range display with Box","Box", overlay = true,process_orders_on_close=true,calc_on_every_tick=true, max_boxes_count=500, initial_capital=1000, default_qty_type=strategy.percent_of_equity, commission_type=strategy.commission.percent,default_qty_value=10,commission_value=0.04,slippage=2)// Just simple strategy for test. Put your strategy here. a= ta.sma(close, 14)b= ta.sma(close, 20)plot(a,color=color.white)plot(b,color=color.blue)longCondition = ta.crossover(a,b)if (longCondition) strategy.entry("L", strategy.long)closeCondition = ta.crossunder(a,b)if (closeCondition) strategy.close("L",comment = "CL")// From here on, just Copy and Paste // 1.Making the trading signal to get the (left,right) for Box. left, right 를 구하기 위한 시그널 만들기. is_trading = strategy.position_size != 0 // 거래 포지션이 0이 아니면 거래중으로 판단하며, true를 리턴.is_trading_L = strategy.position_size > 0is_trading_S = strategy.position_size < 0tradingstart = (is_trading == true and is_trading[1] == false) tradingstart_L = (is_trading_L == true and is_trading_L[1] == false) tradingstart_S = (is_trading_S == true and is_trading_S[1] == false) tradingend = (is_trading == false and is_trading[1] == true) tradingend_L = (is_trading_L == false and is_trading_L[1] == true) tradingend_S = (is_trading_S == false and is_trading_S[1] == true) // plotchar(tradingstart,text="start",textcolor = color.white) // check the signal 시그널 확인용.// plotshape(tradingend,text="End",textcolor = color.white) // 2.Get the left, right. left, right 에 좌표 기준 만들어 주기. var left_L = bar_index , var left_S = bar_index var right_L = bar_index , var right_S = bar_index if tradingstart_L left_L := bar_index -1 // 거래 시작이 확정된 봉이 진짜 시점보다 1봉 이후에 나오므로 일부러 -1을 했다.if tradingend_L or (barstate.islast and is_trading_L) // 과거데이타를 처리할때는 거래 종료 시점만 필요하지만, 현재 거래중일때는 지금의 정보를 계속 업데이트 시켜준다. right_L := bar_index -1if tradingstart_S left_S := bar_index -1 if tradingend_S or (barstate.islast and is_trading_S) right_S := bar_index -1//3.We need two box to make body and wick. body 와 wick 느낌으로 만들기 위해서 top, bottom을 각각 나누어 만들었다. entry = barstate.islast and is_trading? strategy.opentrades.entry_price(strategy.opentrades-1) : strategy.closedtrades.entry_price(strategy.closedtrades-1) // 진입한 가격을 두 상황에 나누어 정의해 준다.exit = barstate.islast ? close : strategy.closedtrades.exit_price(strategy.closedtrades-1) // 현재 거래중일때는 현재종가가 기준이다.// bodytop_body = math.max(entry,exit) bottom_body = math.min(entry,exit)// wicklen_L = right_L and left_L ? right_L - left_L +1 : 1 high_session_L = ta.highest(high,len_L)low_session_L = ta.lowest(low,len_L)len_S = right_S and left_S ? right_S - left_S +1 : 1 high_session_S = ta.highest(high,len_S)low_session_S = ta.lowest(low,len_S)// Box color part.DEFAULT_COLOR_GROWTH = color.rgb(0, 150, 136, 50)DEFAULT_COLOR_FALL = color.rgb(244, 67, 54, 50)color_growth = input.color(DEFAULT_COLOR_GROWTH, title='', inline='Body')color_fall = input.color(DEFAULT_COLOR_FALL, title='Body', inline='Body')color_border_growth = color.rgb(color.r(color_growth), color.g(color_growth), color.b(color_growth), 50)color_border_fall = color.rgb(color.r(color_fall), color.g(color_fall), color.b(color_fall), 50)past_profit = strategy.closedtrades.profit(strategy.closedtrades-1)now_profit = strategy.opentrades.profit(strategy.opentrades-1)colorBD = barstate.islast and is_trading? (now_profit > 0 ? color_growth : color_fall) : (past_profit > 0 ? color_growth : color_fall) // Body colorcolorBL = barstate.islast and is_trading? (now_profit > 0 ? color_border_growth : color_border_fall) : (past_profit > 0 ? color_border_growth : color_border_fall)// : (exit - entry > 0 ? color_border_fall : color_border_growth) // wick color// 4. Box partvar box last_box1_L = navar box last_box2_L = na// Long Position Boxif tradingend_L and not barstate.isrealtime // 1. 과거 데이타로 확인된 봉에서만 실행. box.delete(last_box1_L) , box.delete(last_box2_L) box.new(left_L,high_session_L,right_L,low_session_L, border_color = colorBL, bgcolor = colorBL) box.new(left_L,top_body,right_L,bottom_body, border_color = colorBD, bgcolor = colorBD) left_L := na right_L := naif barstate.islast // 2. 현재봉에서 거래중인지 확인후 실행. if tradingend_L last_box1_L := na last_box2_L := na else if is_trading_L right_L := bar_index -1 box.delete(last_box1_L) , box.delete(last_box2_L) last_box1_L := box.new(left_L,high_session_L,right_L,low_session_L, border_color = colorBL, bgcolor = colorBL) last_box2_L := box.new(left_L,top_body,right_L,bottom_body, border_color = colorBD, bgcolor = colorBD)// Short Position Boxvar box last_box1_S = navar box last_box2_S = naif tradingend_S and not barstate.isrealtime box.delete(last_box1_S) , box.delete(last_box2_S) box.new(left_S,high_session_S,right_S,low_session_S, border_color = colorBL, bgcolor = colorBL) box.new(left_S,top_body,right_S,bottom_body, border_color = colorBD, bgcolor = colorBD) left_S := na right_S := naif barstate.islast if tradingend last_box1_S := na last_box2_S := na else if is_trading_S right_S := bar_index -1 box.delete(last_box1_S) , box.delete(last_box2_S) last_box1_S := box.new(left_S,high_session_S,right_S,low_session_S, border_color = colorBL, bgcolor = colorBL) last_box2_S := box.new(left_S,top_body,right_S,bottom_body, border_color = colorBD, bgcolor = colorBD)