Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

The Box Percent Strat

A Pine Script v4 strategy built on SMA (FirstTop, 1). It has 2 order rules and no entry of its own. Exits use a stop loss. Each trade is one contract and pays no commission. 4 settings; defaults include Risk losing this much per trade 100, The System won't trade if price is below this MA 50 and Box size in % 10.

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 The Box Percent Strat repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 1 of 1 trades.
How did The Box Percent Strat do on BTCUSDT 1D with default settings?
+156.56%
From 26 Jan 2024 to 21 Oct 2026, over 16 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can The Box Percent 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 The Box Percent Strat use a stop loss?
Yes
strategy.exit sets a stop on line 58.
Does The Box Percent Strat run in Zeal Terminal?
Yes
It compiles in 12 ms and runs 1,000 bars in 38 ms, in the browser.

BTCUSDT · 1D · default settings

  • Bot
  • Top
  • Mid
  • Trade AboveMAF Filter
  • 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 (4)
50,00075,000100,000125,000AprJulOct2025AprJulOct2026AprJulOct$2,791$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.
Net profit
+156.56%
$1,565.58
Buy and hold
+102.5%
same period
Closed trades
16
7 won · 9 lost
Profitable
43.8%
of closed trades
Profit factor
3.98
gross profit ÷ gross loss
Max drawdown
33.68%
−$336.78

One run with the author's default settings, $1,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.

  • Likely bug

    sma(FirstTop, 1) ignores the settings line 15

    Line 15 hard-codes 1 where line 29 uses “The System won't trade if price is below this MA”. Changing those settings moves one band but not this one.

    sma(FirstTop, 1)sma(FirstTop, TradeAboveMAFilterPer)
  • Likely bug

    sma(FirstBot, 1) ignores the settings line 16

    Line 16 hard-codes 1 where line 29 uses “The System won't trade if price is below this MA”. Changing those settings moves one band but not this one.

    sma(FirstBot, 1)sma(FirstBot, TradeAboveMAFilterPer)
  • 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.

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 sma → ta.sma, abs → math.abs.

v4 → v5

Verified

Same 16 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 1: //@version=4 → //@version=5
  • Line 15: sma → ta.sma
  • Line 16: sma → ta.sma
  • Line 29: sma → ta.sma
  • Line 34: abs → math.abs
-//@version=4+//@version=5// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/⋯var FirstBot            = FirstBar * DnBoxSize-var top                 = sma(FirstTop, 1)+var top                 = ta.sma(FirstTop, 1)-var bot                 = sma(FirstBot, 1)+var bot                 = ta.sma(FirstBot, 1)///     The Box Calcs⋯plot(mid,   "Mid", color.gray)-TradeAboveMAFilter      = sma(close, TradeAboveMAFilterPer)+TradeAboveMAFilter      = ta.sma(close, TradeAboveMAFilterPer)plot(TradeAboveMAFilter, "Trade AboveMAF Filter", color.yellow, 3, style=plot.style_circles)⋯///     Shares-RiskRange                   = close * abs(DnBoxSize - 1) // 0.9 - 1 == 1.10 // 10% abs so you don't get a neg number NB NB+RiskRange                   = close * math.abs(DnBoxSize - 1) // 0.9 - 1 == 1.10 // 10% abs so you don't get a neg number NB NBShares                      = RiskPerTrade / RiskRange ⋯

Clean v5

VerifiedDownload the v5 script

Same 16 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 8: input() → input.int()
  • Line 9: input() → input.int()
  • Line 10: input() → input.int()
  • Line 11: input() → input.int()
⋯///     Limit buy to not overpay-RiskPerTrade            = input(100, "Risk losing this much per trade", tooltip="This calculates how much you will lose based on difference between the entry price and stop loss price")+RiskPerTrade            = input.int(100, "Risk losing this much per trade", tooltip="This calculates how much you will lose based on difference between the entry price and stop loss price")-TradeAboveMAFilterPer   = input(50, "The System won't trade if price is below this MA")+TradeAboveMAFilterPer   = input.int(50, "The System won't trade if price is below this MA")-UpBoxSize               = (input(10, "Box size in %") * 0.01)+1 // 1.1 == 10% up+UpBoxSize               = (input.int(10, "Box size in %") * 0.01)+1 // 1.1 == 10% up-DnBoxSize               = 1-(input(10, "Box size in %") * 0.01) // 0.9 == 10% dn+DnBoxSize               = 1-(input.int(10, "Box size in %") * 0.01) // 0.9 == 10% dnvar FirstBar            = close > 0 ? close : na⋯

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: 16 before, 1 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.

-//@version=5+//@version=6// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/⋯

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
Risk losing this much per tradeThis calculates how much you will lose based on difference between the entry price and stop loss price100intShares
The System won't trade if price is below this MA50intTradeAboveMAFilter
Box size in %10intFirstTop top bot
Box size in %10intFirstBot top bot RiskRange

What it calculates

  • SMA (sma, ta.sma in v5)FirstTop, 1top · line 15
  • SMA (sma, ta.sma in v5)FirstBot, 1bot · line 16
  • SMA (sma, ta.sma in v5)close, The System won't trade if price is below this MA (50)TradeAboveMAFilter · line 29

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.

UpBoxSizeBox size in %setting · 10DnBoxSizeBox size in %setting · 10closecloseprice dataTradeAboveMAFilterPerThe System won't trade …setting · 50RiskPerTradeRisk losing this much p…setting · 100highhighprice datatopSMA→ topbotSMA→ botTradeAboveMAFilterSMA→ TradeAboveMAFilterFirstBarFirstBarvalueFirstTopFirstTopvalueFirstBotFirstBotvaluemidmidvalueRiskRangeRiskRangevalueSharesSharesvalueEnterEnterconditionorder:Enorder Enstrategy.orderexit:Exexit Exstrategy.exitplot:25Botplotplot:26Topplotplot:28Midplotplot:30Trade AboveMAF Filterplot

Order rules

OrderFires whenBy defaultLine
order En longEnterOn50
Exit Ex stopstrategy.position_size > 0On58

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.

1/1trades 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+156.6%+102.5%1644%3.9833.7%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h0.0%+5.5%00%—3.9%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−11.2%+9.8%2124%0.9154.3%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+13.1%+8.4%450%1.6614.3%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−56.1%+16.7%2719%0.57105.2%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+156.5%+21.5%771%15.948.9%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+67.9%+148.3%1839%1.9038.9%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−19.8%+17.1%20%0.0021.4%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 market729%$156.38
Sideways market863%$1,495.28
Falling market10%−$86.08

Rising and falling mean the market moved more than 15% over the 90 bars before entry.

How bad could the drawdown be?

28.5%

The 95th percentile of the worst drawdown when the same 16 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 12.0% to 28.5%; the tick is what actually happened, 28.8%. Order luck alone moves it this much.

0%33%
All 16 trades
SideEntryExitBarsP&L
long9 Apr 2024 71,620.0013 Apr 2024 65,356.604−$87.45
long21 May 2024 71,173.3021 Jun 2024 64,056.0031−$99.62
long18 Jul 2024 63,415.403 Aug 2024 61,498.3016−$29.47
long30 Jul 2024 66,784.703 Aug 2024 61,498.304−$79.16
long1 Oct 2024 61,531.9027 Jan 2025 98,106.80118$581.03
long16 Oct 2024 67,074.1027 Jan 2025 98,106.80103$462.66
long7 Nov 2024 74,453.6027 Jan 2025 98,106.8081$312.99
long14 Nov 2024 90,088.9027 Jan 2025 98,106.8074$88.72
long23 Nov 2024 98,892.0027 Jan 2025 98,106.8065−$7.94
long21 Jan 2025 102,260.0027 Jan 2025 98,106.806−$40.61
long14 Apr 2025 83,760.0010 Oct 2025 112,891.00179$347.80
long24 Apr 2025 93,691.1010 Oct 2025 112,891.00169$204.93
long9 May 2025 103,262.0010 Oct 2025 112,891.00154$93.26
long1 Aug 2025 114,032.0010 Oct 2025 112,891.0070−$9.70
long6 Oct 2025 123,482.0010 Oct 2025 112,891.004−$85.77
long18 Apr 2026 77,072.002 Jun 2026 70,437.9045−$86.08

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
The Box Percent Strat this page—v4 strategy+156.6%1633.7%
Follow the Crypto Shorts by xtradernetsmav4 strategyNo trades on this market
buy down and sell high trend 200 sma and ma cross by willi130503smav4 strategyNo trades on this market
Mayer Multiple Strategy by xtradernetsmav4 strategyNo trades on this market
Moving Average Cross Strategy early Closing by Geduldtradersmav4 strategy+73.8%20151.5%
MFI Simple Strategy by OztheWozsmav5 strategy−0.2%283.2%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 12 msYes 1,000 bars in 38 msNot measured
PineTSNo 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.

//@version=4// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © HermanBrummer 4 April 2021strategy ("The Box Percent Strat", shorttitle="The Box", overlay = true, initial_capital = 1000)///     Designed for LONG only on Daily, 2D or 3D Charts///     Uses fixed investment risk amount, meaning you're willing to lose that amount per trade///     Limit buy to not overpayRiskPerTrade            = input(100, "Risk losing this much per trade", tooltip="This calculates how much you will lose based on difference between the entry price and stop loss price")TradeAboveMAFilterPer   = input(50, "The System won't trade if price is below this MA")UpBoxSize               = (input(10, "Box size in %") * 0.01)+1 // 1.1 == 10% upDnBoxSize               = 1-(input(10, "Box size in %") * 0.01) // 0.9 == 10% dnvar FirstBar            = close > 0 ? close : navar FirstTop            = FirstBar * UpBoxSizevar FirstBot            = FirstBar * DnBoxSizevar top                 = sma(FirstTop, 1)var bot                 = sma(FirstBot, 1)///     The Box Calcsif  high[2] > top    top                 := top * UpBoxSize    bot                 := bot * UpBoxSizeif  low[1]  < bot    top                 := top * DnBoxSize    bot                 := bot * DnBoxSize plot(bot,   "Bot",      #ff0000) // Greenplot(top,   "Top",      #00ff00) // Redmid                     = ((top-bot)/2)+bot plot(mid,   "Mid", color.gray)TradeAboveMAFilter      = sma(close, TradeAboveMAFilterPer)plot(TradeAboveMAFilter, "Trade AboveMAF Filter", color.yellow, 3, style=plot.style_circles)// col = high[1] < top and high >= top ? color.white : na// bgcolor(col)///     SharesRiskRange                   = close * abs(DnBoxSize - 1) // 0.9 - 1 == 1.10 // 10% abs so you don't get a neg number NB NBShares                      = RiskPerTrade / RiskRange //plot(close-RiskRange, "RiskRange", color.fuchsia)Enter   =   high >= top             and close[1] > TradeAboveMAFilter             and strategy.opentrades[0] == strategy.opentrades[1]             and strategy.opentrades[1] == strategy.opentrades[2]              and strategy.opentrades[2] == strategy.opentrades[3]             and strategy.opentrades[3] == strategy.opentrades[4]              and strategy.opentrades[4] == strategy.opentrades[5]             and strategy.opentrades[5] == strategy.opentrades[6]             // won't enter if new positon was taken in the last 6 bars             // need better code for this.///     Buy & Sell//  (new highs)    and  (Close above moving average filter) and (No new trades were taken receently)if  Enter //(high >= top)  and  (close[1] > TradeAboveMAFilter) and strategy.opentrades[0] == strategy.opentrades[1]     strategy.order("En", strategy.long, qty=Shares, limit=top)//, stop=top)    //barcolor(strategy.position_size != 0 ? #00ff00 : color.gray)// ///     If ONE Position THEN this Stop Because: // if  strategy.position_size == 1//     strategy.exit("Ex", "En", stop=bot)///     If it has more than one trad OPENif  strategy.position_size > 0    strategy.exit("Ex", "En", stop=bot[2] )   // puts stop on old bot//plot(strategy.position_avg_price, "Avg Price", color.yellow)