Pine Script v5 · Strategy
Bollinger Band Reversion Strategy
A Pine Script v5 strategy built on BB (close, 20), ATR (14) and Crossover (close, lower). It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits use a stop loss and a profit target. Each trade commits 10% of equity and pays 0.05% commission. 4 settings; defaults include Length 20, Standard deviations 2.0 and ATR length 14.
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 Bollinger Band Reversion Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 5 of 5 trades.
- How did Bollinger Band Reversion Strategy do on BTCUSDT 1D with default settings?
- +1.43%
- From 26 Jan 2024 to 21 Oct 2026, over 22 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- What alerts does Bollinger Band Reversion Strategy have?
- 1
- Close back inside the lower band.
- Does Bollinger Band Reversion Strategy run in Pine Script v6?
- Yes
- Same 22 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. The v6 script can be downloaded below.
- Does Bollinger Band Reversion Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 25.
BTCUSDT · 1D · default settings
- Basis
- Upper
- Lower
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +1.43%
- $143.33
- Buy and hold
- +102.5%
- same period
- Closed trades
- 22
- 16 won · 6 lost
- Profitable
- 72.7%
- of closed trades
- Profit factor
- 1.31
- gross profit ÷ gross loss
- Max drawdown
- 2.68%
- −$275.38
One run with the author's default settings, $10,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.
Nothing to point out.
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 |
|---|---|---|---|
| Length | 20 | int · min 2 | basis upper lower |
| Standard deviations | 2.0 | float · min 0.1 · step 0.1 | basis upper lower |
| ATR length | 14 | int · min 1 | atr |
| Stop (ATRs) | 2.0 | float · min 0.1 · step 0.1 | stopLevel |
What it calculates
- BBclose, Length (20), Standard deviations (2.0)
basis · line 13 - BBclose, Length (20), Standard deviations (2.0)
upper · line 13 - BBclose, Length (20), Standard deviations (2.0)
lower · line 13 - ATRATR length (14)
atr · line 14 - Crossoverclose, lower
reEntry · line 16
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 Long long | reEntry and strategy.position_size == 0 | On | 19 |
| Exit Exit stop and limit | strategy.position_size > 0 | On | 25 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Alerts
| Alert | Call | Message | Line |
|---|---|---|---|
| Close back inside the lower band | alertcondition | Close crossed back above the lower Bollinger Band | 32 |
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 | +1.4% | +102.5% | 22 | 73% | 1.31 | 2.7% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.6% | +5.5% | 30 | 77% | 1.31 | 0.9% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −4.6% | +9.8% | 22 | 55% | 0.51 | 6.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.8% | +8.4% | 27 | 63% | 0.76 | 2.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −5.1% | +16.7% | 22 | 55% | 0.61 | 7.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −1.2% | +21.5% | 27 | 63% | 0.67 | 2.1% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +2.3% | +148.3% | 21 | 67% | 1.42 | 1.9% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.2% | +17.1% | 24 | 71% | 0.89 | 1.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 | 6 | 83% | $28.25 |
| Sideways market | 9 | 67% | $84.41 |
| Falling market | 7 | 71% | $30.67 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
3.4%
The 95th percentile of the worst drawdown when the same 22 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 1.3% to 3.4%; the tick is what actually happened, 2.3%. Order luck alone moves it this much.
All 22 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 15 Apr 2024 65,661.90 | 22 Apr 2024 66,487.90 | 7 | $11.57 |
| long | 4 May 2024 62,882.00 | 5 May 2024 63,892.00 | 1 | $15.06 |
| long | 16 Jun 2024 66,228.30 | 24 Jun 2024 62,716.60 | 8 | −$54.11 |
| long | 26 Jun 2024 61,806.00 | 4 Jul 2024 56,815.90 | 8 | −$81.43 |
| long | 7 Jul 2024 58,230.10 | 13 Jul 2024 59,639.90 | 6 | $22.93 |
| long | 8 Aug 2024 55,133.80 | 18 Aug 2024 60,258.60 | 10 | $91.07 |
| long | 8 Sep 2024 54,160.90 | 13 Sep 2024 58,416.60 | 5 | $77.54 |
| long | 7 Feb 2025 96,554.40 | 20 Feb 2025 97,470.50 | 13 | $8.55 |
| long | 2 Mar 2025 86,064.60 | 3 Mar 2025 94,270.00 | 1 | $95.11 |
| long | 10 Apr 2025 82,615.20 | 11 Apr 2025 83,271.10 | 1 | $7.06 |
| long | 7 Jun 2025 104,288.00 | 9 Jun 2025 106,563.00 | 2 | $21.19 |
| long | 4 Aug 2025 114,209.00 | 7 Aug 2025 116,813.00 | 3 | $22.25 |
| long | 27 Aug 2025 111,763.00 | 5 Sep 2025 112,529.00 | 9 | $5.99 |
| long | 27 Sep 2025 109,643.00 | 29 Sep 2025 113,940.00 | 2 | $39.08 |
| long | 6 Nov 2025 103,885.00 | 16 Nov 2025 93,188.70 | 10 | −$106.79 |
| long | 23 Nov 2025 84,739.80 | 2 Dec 2025 91,084.60 | 9 | $75.09 |
| long | 17 Dec 2025 87,863.40 | 22 Dec 2025 89,674.10 | 5 | $20.08 |
| long | 3 Feb 2026 78,738.60 | 5 Feb 2026 68,801.10 | 2 | −$130.52 |
| long | 7 Feb 2026 70,580.30 | 25 Feb 2026 67,758.40 | 18 | −$41.51 |
| long | 24 May 2026 76,752.00 | 28 May 2026 72,996.80 | 4 | −$50.37 |
| long | 8 Jun 2026 63,332.00 | 15 Jun 2026 67,011.30 | 7 | $57.31 |
| long | 27 Jun 2026 60,097.30 | 3 Jul 2026 62,431.40 | 6 | $38.20 |
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 |
|---|---|---|---|---|---|
| Bollinger Band Reversion Strategy this page | — | v5 strategy | +1.4% | 22 | 2.7% |
| TrendMaAlignmentStrategy - Long term trades by Trendoscope | atr bb | v4 strategy | −22.0% | 8 | 46.3% |
| BuyTheDip by Trendoscope | atr bb | v4 strategy | +4.6% | 2 | 8.3% |
| Breakout Trend Trading Strategy - V2 by Trendoscope | atr bb | v4 strategy | −1.2% | 1 | 9.8% |
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?
Yes
Same 22 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
⋯// © Zeal Terminal-//@version=5+//@version=6strategy("Bollinger Band Reversion Strategy", shorttitle = "BB Reversion", overlay = true, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 10, commission_type = strategy.commission.percent, commission_value = 0.05, slippage = 1)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 12 ms | Yes 1,000 bars in 29 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
Written by Zeal Terminal and published under the Mozilla Public License 2.0.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © Zeal Terminal//@version=5strategy("Bollinger Band Reversion Strategy", shorttitle = "BB Reversion", overlay = true, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 10, commission_type = strategy.commission.percent, commission_value = 0.05, slippage = 1) // Not investment advice. Buys a close back inside the lower band and takes// profit at the basis, with a stop a fixed number of ATRs below entry.length = input.int(20, "Length", minval = 2)mult = input.float(2.0, "Standard deviations", minval = 0.1, step = 0.1)atrLen = input.int(14, "ATR length", minval = 1)stopAtrs = input.float(2.0, "Stop (ATRs)", minval = 0.1, step = 0.1) [basis, upper, lower] = ta.bb(close, length, mult)atr = ta.atr(atrLen) reEntry = ta.crossover(close, lower) if reEntry and strategy.position_size == 0 strategy.entry("Long", strategy.long) var float stopLevel = naif strategy.position_size > 0 and strategy.position_size[1] == 0 stopLevel := close - stopAtrs * atrif strategy.position_size > 0 strategy.exit("Exit", "Long", stop = stopLevel, limit = basis) plot(basis, "Basis", color = #ffb74d)u = plot(upper, "Upper", color = color.new(#42a5f5, 40))l = plot(lower, "Lower", color = color.new(#42a5f5, 40))fill(u, l, color = color.new(#42a5f5, 94), title = "Band") alertcondition(reEntry, "Close back inside the lower band", "Close crossed back above the lower Bollinger Band")