Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (0)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$1,007$2,025Equity from $1,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
+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.

SettingDefaultType and rangeFeeds
Color growth (no label in the script)DEFAULT_COLOR_GROWTHcolorcolor_border_growth colorBD
BodyDEFAULT_COLOR_FALLcolorcolor_border_fall colorBD

What it calculates

  • SMAclose, 14a · line 7
  • SMAclose, 20b · line 8
  • Crossovera, blongCondition · line 11
  • Crossundera, bcloseCondition · line 14
  • Highesthigh, len_Lhigh_session_L · line 49
  • Lowestlow, len_Llow_session_L · line 50
  • Highesthigh, len_Shigh_session_S · line 52
  • Lowestlow, len_Slow_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.

closecloseprice datahighhighprice datalowlowprice dataaSMA→ abSMA→ blongConditionCrossover→ longConditioncloseConditionCrossunder→ closeConditionhigh_session_LHighest→ high_session_Llow_session_LLowest→ low_session_Lhigh_session_SHighest→ high_session_Slow_session_SLowest→ low_session_Sleft_L, left_S, right_L, right_S, past_profit, now_profitleft_L · left_S +46 valuesentryentryvalueexitexitvaluetop_body, bottom_bodytop_body · bottom_body2 valueslen_Llen_Lvaluelen_Slen_SvaluecolorBLcolorBLvaluelast_box1_Llast_box1_Lvaluelast_box2_Llast_box2_Lvaluelast_box1_Slast_box1_Svaluelast_box2_Slast_box2_Svalueis_trading, is_trading_L, is_trading_S, tradingstart, tradingstart_L, tradingstart_S, tradingend, tradingend_L, tradingend_Sis_trading · is_trading…9 conditionsDEFAULT_COLOR_GROWTH, DEFAULT_COLOR_FALL, color_border_growth, color_border_fall, colorBDColourDEFAULT_COLOR_GROWTH · DEFAUL…entry:LOpen Lstrategy.entryclose:LClose Lstrategy.closeplot:9plotplotplot:10plotplot

Order rules

OrderFires whenBy defaultLine
Open L long(longCondition)On13
Close L(closeCondition)On16

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.

8/8trades 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. On 0 of them the position changed while the bar was forming before it settled. Anyone watching live sees those changes; once the bar closes it matches history.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+0.7%+102.5%2737%1.073.9%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+0.7%+5.5%3247%1.212.0%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+5.2%+9.8%3238%1.446.3%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+1.5%+8.4%3135%1.371.4%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+12.1%+16.7%2850%1.925.4%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+1.6%+21.5%3234%1.272.3%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+1.2%+148.3%2846%1.134.1%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+0.3%+17.1%2941%1.141.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 barsTradesWonP&L
Rising market863%$11.95
Sideways market138%−$49.14
Falling market667%$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.

0%8%
All 27 trades
SideEntryExitBarsP&L
long2 Apr 2024 65,464.0012 Apr 2024 67,116.5010$2.44
long15 May 2024 66,206.508 Jun 2024 69,310.4024$4.62
long9 Jun 2024 69,648.2018 Jun 2024 65,175.309−$6.54
long19 Jul 2024 66,660.004 Aug 2024 58,161.0016−$12.83
long21 Aug 2024 61,156.106 Sep 2024 53,962.9016−$11.69
long19 Sep 2024 62,948.008 Oct 2024 62,160.5019−$1.30
long17 Oct 2024 67,421.8027 Dec 2024 94,299.0071$38.75
long13 Jan 2025 94,536.1020 Jan 2025 102,260.007$8.19
long21 Jan 2025 106,144.004 Feb 2025 97,763.1014−$8.14
long23 Feb 2025 96,258.0024 Feb 2025 91,552.901−$5.03
long25 Mar 2025 87,392.906 Apr 2025 78,430.0012−$10.42
long20 Apr 2025 85,179.305 Jun 2025 101,509.0046$19.04
long16 Jun 2025 106,795.0023 Jun 2025 105,334.007−$1.47
long2 Jul 2025 108,850.0030 Jul 2025 117,840.0028$8.30
long16 Aug 2025 117,381.0025 Aug 2025 110,112.009−$6.42
long12 Sep 2025 116,029.0027 Sep 2025 109,636.0015−$5.68
long6 Oct 2025 124,659.0017 Oct 2025 106,432.0011−$14.86
long1 Nov 2025 110,098.007 Nov 2025 103,339.006−$6.20
long6 Dec 2025 89,236.8017 Dec 2025 86,243.2011−$3.40
long1 Jan 2026 88,839.1027 Jan 2026 89,250.0026$0.38
long8 Mar 2026 65,971.2029 Mar 2026 66,010.9021−$0.02
long11 Apr 2026 73,043.2020 May 2026 77,552.2039$6.01
long21 Jun 2026 63,312.0028 Jun 2026 59,577.007−$5.94
long10 Jul 2026 64,161.702 Aug 2026 63,570.0023−$0.99
long15 Aug 2026 63,086.0011 Sep 2026 77,225.7027$22.02
long16 Sep 2026 76,206.0017 Sep 2026 76,417.001$0.20
long23 Sep 2026 84,397.609 Oct 2026 82,635.5016−$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.

ScriptSharesTypeNetTradesMax drawdown
Trading range display with Box this page—v5 strategy+0.7%273.9%
ChBrkOutStrategySMA by anshuanshu333sma highest lowestv4 strategy−4.5%4220.9%
KDJ Strategy @ionvolution by ionvolutionsma highest lowestv5 strategyNo trades on this market
Open Drive by Marcns_highest lowest smav5 strategyNo trades on this market
Trend Bounce [racer8] by racer8highest lowest smav4 strategy+2.4%2232.2%
Lawyers Trend Pro Strategy by ErdemDemirsma lowest highestv4 strategy+12.1%10241.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

Verified

Nothing 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

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 41 msYes 1,000 bars in 58 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/// © 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)