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
- 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
VerifiedSame 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 ⋯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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Risk losing this much per tradeThis calculates how much you will lose based on difference between the entry price and stop loss price | 100 | int | Shares |
| The System won't trade if price is below this MA | 50 | int | TradeAboveMAFilter |
| Box size in % | 10 | int | FirstTop top bot |
| Box size in % | 10 | int | FirstBot top bot RiskRange |
What it calculates
- SMA (
sma,ta.smain v5)FirstTop, 1top · line 15 - SMA (
sma,ta.smain v5)FirstBot, 1bot · line 16 - SMA (
sma,ta.smain 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| order En long | Enter | On | 50 |
| Exit Ex stop | strategy.position_size > 0 | On | 58 |
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 | +156.6% | +102.5% | 16 | 44% | 3.98 | 33.7% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | 0.0% | +5.5% | 0 | 0% | — | 3.9% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −11.2% | +9.8% | 21 | 24% | 0.91 | 54.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +13.1% | +8.4% | 4 | 50% | 1.66 | 14.3% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −56.1% | +16.7% | 27 | 19% | 0.57 | 105.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +156.5% | +21.5% | 7 | 71% | 15.94 | 8.9% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +67.9% | +148.3% | 18 | 39% | 1.90 | 38.9% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −19.8% | +17.1% | 2 | 0% | 0.00 | 21.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 7 | 29% | $156.38 |
| Sideways market | 8 | 63% | $1,495.28 |
| Falling market | 1 | 0% | −$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.
All 16 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 9 Apr 2024 71,620.00 | 13 Apr 2024 65,356.60 | 4 | −$87.45 |
| long | 21 May 2024 71,173.30 | 21 Jun 2024 64,056.00 | 31 | −$99.62 |
| long | 18 Jul 2024 63,415.40 | 3 Aug 2024 61,498.30 | 16 | −$29.47 |
| long | 30 Jul 2024 66,784.70 | 3 Aug 2024 61,498.30 | 4 | −$79.16 |
| long | 1 Oct 2024 61,531.90 | 27 Jan 2025 98,106.80 | 118 | $581.03 |
| long | 16 Oct 2024 67,074.10 | 27 Jan 2025 98,106.80 | 103 | $462.66 |
| long | 7 Nov 2024 74,453.60 | 27 Jan 2025 98,106.80 | 81 | $312.99 |
| long | 14 Nov 2024 90,088.90 | 27 Jan 2025 98,106.80 | 74 | $88.72 |
| long | 23 Nov 2024 98,892.00 | 27 Jan 2025 98,106.80 | 65 | −$7.94 |
| long | 21 Jan 2025 102,260.00 | 27 Jan 2025 98,106.80 | 6 | −$40.61 |
| long | 14 Apr 2025 83,760.00 | 10 Oct 2025 112,891.00 | 179 | $347.80 |
| long | 24 Apr 2025 93,691.10 | 10 Oct 2025 112,891.00 | 169 | $204.93 |
| long | 9 May 2025 103,262.00 | 10 Oct 2025 112,891.00 | 154 | $93.26 |
| long | 1 Aug 2025 114,032.00 | 10 Oct 2025 112,891.00 | 70 | −$9.70 |
| long | 6 Oct 2025 123,482.00 | 10 Oct 2025 112,891.00 | 4 | −$85.77 |
| long | 18 Apr 2026 77,072.00 | 2 Jun 2026 70,437.90 | 45 | −$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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| The Box Percent Strat this page | — | v4 strategy | +156.6% | 16 | 33.7% |
| Follow the Crypto Shorts by xtradernet | sma | v4 strategy | No trades on this market | ||
| buy down and sell high trend 200 sma and ma cross by willi130503 | sma | v4 strategy | No trades on this market | ||
| Mayer Multiple Strategy by xtradernet | sma | v4 strategy | No trades on this market | ||
| Moving Average Cross Strategy early Closing by Geduldtrader | sma | v4 strategy | +73.8% | 20 | 151.5% |
| MFI Simple Strategy by OztheWoz | sma | v5 strategy | −0.2% | 28 | 3.2% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 12 ms | Yes 1,000 bars in 38 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.
//@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)