Pine Script v5 · Strategy
Steven Primo's bollinger bands strategy
A Pine Script v5 strategy built on BB (close, 20), Pivothigh (1, 1) and Pivotlow (1, 1). It has 8 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade commits 100% of equity and pays no commission. 7 settings; defaults include Price source close, Bollinger length 20 and Standard deviation 0.382.
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 Steven Primo's bollinger bands strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 4 of 4 trades.
- How did Steven Primo's bollinger bands strategy do on BTCUSDT 1D with default settings?
- −17.74%
- From 26 Jan 2024 to 21 Oct 2026, over 15 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Steven Primo's bollinger bands strategy 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: 15 before, 11 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 Steven Primo's bollinger bands strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 97, 98, 105, 108.
- Does Steven Primo's bollinger bands strategy run in Zeal Terminal?
- Yes
- It compiles in 67 ms and runs 1,000 bars in 87 ms, in the browser.
BTCUSDT · 1D · default settings
- Middle band
- Upper band
- Lower band
- Stop loss
- Target
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −17.74%
- −$177,400.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 15
- 7 won · 8 lost
- Profitable
- 46.7%
- of closed trades
- Profit factor
- 0.69
- gross profit ÷ gross loss
- Max drawdown
- 28.24%
- −$282,418.00
One run with the author's default settings, $1,000,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.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses an argument v6 removed
when = on line 105, 108. The upgraded version below moves each call inside an if block.
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 |
|---|---|---|---|
| Bollinger settings | |||
| Price source | close | source | middle upper lower |
| Bollinger length | 20 | int · min 3 | middle upper lower |
| Standard deviation | 0.382 | float · min 0.0 · step 0.1 | middle upper lower |
| Setup settings | |||
| Order direction | LONG | string · options [LONG, SHORT, ALL] | line 82 |
| Use trailing stop | false | bool | line 103 |
| Consecutive closes for the setup | 5 | int · min 1 | canBuy canSell |
| Extension (%) | 100 | int · min 100 | totalTicks |
What it calculates
- BBPrice source (close), Bollinger length (20), Standard deviation (0.382)
middle · line 21 - BBPrice source (close), Bollinger length (20), Standard deviation (0.382)
upper · line 21 - BBPrice source (close), Bollinger length (20), Standard deviation (0.382)
lower · line 21 - Pivothigh1, 1
pivotHigh · line 48 - Pivotlow1, 1
pivotLow · line 49
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 | canBuy | On | 87 |
| Open short short | canSell | On | 91 |
| cancel long | canSell | On | 93 |
| cancel short | canSell | On | 94 |
| Exit long stop and limit | not inMarket | On | 97 |
| Exit short stop and limit | not inMarket | On | 98 |
| Exit long stop and limit | stopLong > plotStop | On | 105 |
| Exit short stop and limit | stopShort < plotStop | On | 108 |
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 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 | −17.7% | +102.5% | 15 | 47% | 0.69 | 28.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −3.3% | +5.5% | 13 | 38% | 0.79 | 9.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −15.3% | +9.8% | 14 | 43% | 0.69 | 34.7% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −24.2% | +8.4% | 13 | 15% | 0.16 | 24.5% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −8.0% | +16.7% | 13 | 38% | 0.91 | 31.9% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −26.4% | +21.5% | 14 | 29% | 0.27 | 27.2% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −33.8% | +148.3% | 21 | 38% | 0.54 | 42.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +17.2% | +17.1% | 16 | 56% | 2.31 | 8.5% | 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 | 33% | −$166,598.00 |
| Sideways market | 6 | 33% | −$96,191.10 |
| Falling market | 3 | 100% | $85,388.70 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
42.1%
The 95th percentile of the worst drawdown when the same 15 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 23.0% to 42.1%; the tick is what actually happened, 23.8%. Order luck alone moves it this much.
All 15 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 20 Feb 2024 52,816.60 | 23 Feb 2024 50,625.00 | 3 | −$41,495.30 |
| long | 27 May 2024 69,610.00 | 11 Jun 2024 66,312.10 | 15 | −$45,410.30 |
| long | 19 Jul 2024 66,128.60 | 5 Aug 2024 56,542.50 | 17 | −$132,365.00 |
| long | 23 Sep 2024 64,133.30 | 29 Oct 2024 70,773.40 | 36 | $80,832.80 |
| long | 10 Nov 2024 77,200.00 | 11 Nov 2024 87,565.00 | 1 | $115,675.00 |
| long | 4 Dec 2024 98,130.00 | 5 Dec 2024 90,791.10 | 1 | −$73,085.40 |
| long | 19 Jan 2025 105,865.00 | 25 Feb 2025 89,256.70 | 37 | −$141,847.00 |
| long | 21 Apr 2025 85,678.00 | 21 Apr 2025 88,244.40 | 0 | $22,833.80 |
| long | 25 Apr 2025 94,696.10 | 12 May 2025 105,443.00 | 17 | $89,101.20 |
| long | 9 Jul 2025 109,700.00 | 10 Jul 2025 112,155.00 | 1 | $19,565.00 |
| long | 15 Sep 2025 116,666.00 | 25 Sep 2025 109,977.00 | 10 | −$51,243.50 |
| long | 12 Jan 2026 92,082.60 | 13 Jan 2026 94,854.10 | 1 | $25,360.10 |
| long | 16 Mar 2026 73,913.80 | 19 Mar 2026 70,317.00 | 3 | −$42,234.50 |
| long | 13 Apr 2026 73,790.00 | 17 Apr 2026 77,114.00 | 4 | $37,194.80 |
| long | 20 Jul 2026 65,600.00 | 31 Jul 2026 62,537.60 | 11 | −$40,282.70 |
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 |
|---|---|---|---|---|---|
| Steven Primo's bollinger bands strategy this page | — | v5 strategy | −17.7% | 15 | 28.2% |
| Pivot Point Breakout by EduardoMattje | pivotlow pivothigh | v5 strategy | +93.0% | 25 | 32.3% |
| Monthly Returns of a Strategy in a Chart by QuantNomad | pivothigh pivotlow | v5 strategy | +21.0% | 76 | 13.0% |
| ZigZag by Zeal Terminal | pivothigh pivotlow | v5 indicator | indicator | ||
| RSI Centered Pivots by OztheWoz | pivotlow pivothigh | v4 strategy | +98.0% | 4 | 160.2% |
| ABCD Strategy by kerok3g | pivothigh pivotlow | v5 strategy | −2.7% | 4 | 3.0% |
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.
- 2 strategy calls use the when = argument (line 105, 108). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- No v6-only features. v6 is not needed to run this script.
Same 15 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 105: when = stopLong > plotStop → if block
- Line 108: when = stopShort < plotStop → if block
⋯ if strategy.position_entry_name == "long"- strategy.exit("long", "long", stop=stopLong, limit=plotTarget, comment="Exit long", when=stopLong > plotStop)+ if stopLong > plotStop+ strategy.exit("long", "long", stop=stopLong, limit=plotTarget, comment="Exit long") plotStop := stopLong else- strategy.exit("short", "short", stop=stopShort, limit=plotTarget, comment="Exit short", when=stopShort < plotStop) + if stopShort < plotStop+ strategy.exit("short", "short", stop=stopShort, limit=plotTarget, comment="Exit short") plotStop := stopShort⋯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: 15 before, 11 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.
⋯// © EduardoMattje-//@version=5+//@version=6strategy("Steven Primo Bollinger Band", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, process_orders_on_close=true, max_labels_count=500)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 67 ms | Yes 1,000 bars in 87 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/// © EduardoMattje//@version=5strategy("Steven Primo Bollinger Band", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, process_orders_on_close=true, max_labels_count=500)// Constantsvar TRANSP = 5var LONG = strategy.direction.longvar SHORT = strategy.direction.shortvar ALL = strategy.direction.allvar S_BOLLINGER = "Bollinger settings"var S_SETUP = "Setup settings"// Inputssrc = math.log(input.source(close, "Price source", group=S_BOLLINGER))var bollingerLength = input.int(20, "Bollinger length", minval=3, group=S_BOLLINGER, inline=S_BOLLINGER)var mult = input.float(0.382, "Standard deviation", minval=0.0, step=0.1, group=S_BOLLINGER, inline=S_BOLLINGER)var orderDirection = input.string(LONG, "Order direction", options=[LONG, SHORT, ALL], group=S_SETUP)var useTrailingStop = input.bool(false, "Use trailing stop", group=S_SETUP)var consecutiveCloses = input.int(5, "Consecutive closes for the setup", minval=1, group=S_SETUP, inline=S_SETUP)var extension = input.int(100, "Extension (%)", minval=100, group=S_SETUP, inline=S_SETUP) / 100.0// Getting the BB[middle, upper, lower] = ta.bb(src, bollingerLength, mult)middle := math.exp(middle)upper := math.exp(upper)lower := math.exp(lower)// Plotting the BBvar colorAtTheLimits = color.new(color.yellow, TRANSP)var colorAtTheMiddle = color.new(color.blue, TRANSP)plot(middle, "Middle band", colorAtTheMiddle, display=display.none)plot(upper, "Upper band", colorAtTheLimits)plot(lower, "Lower band", colorAtTheLimits)// MA setup// BB setuplongComparison() => close >= uppershortComparison() => close <= lowervar countLong = 0var countShort = 0incCount(count, comparison) => if comparison if count == 0 and comparison[1] 0 else count + 1 else 0countLong := incCount(countLong, longComparison())countShort := incCount(countShort, shortComparison())// Pivot setuppivotHigh = ta.pivothigh(1, 1)pivotLow = ta.pivotlow(1, 1)pivotInRange(pivot, count) => ta.barssince(pivot) < countpvHighInRange = pivotInRange(pivotHigh, countLong)pvLowInRange = pivotInRange(pivotLow, countShort)// Entry priceepLong = fixnan(pivotHigh) + syminfo.mintickepShort = fixnan(pivotLow) - syminfo.mintick// Stop pricegetRange(currentPrice, pivot, cond, tickMod) => if cond currentPrice else fixnan(pivot) + syminfo.mintick * tickModvar stopLong = 0.0var stopShort = 0.0stopLong := epShortstopShort := epLong // Target pricegetTarget(stopPrice, entryPrice) => totalTicks = (entryPrice - stopPrice) * extension entryPrice + totalTicksvar targetLong = 0.0var targetShort = 0.0targetLong := getTarget(stopLong, epLong)targetShort := getTarget(stopShort, epShort)// Entry conditioncanBuy = countLong >= consecutiveCloses and pvHighInRange and high < epLongcanSell = countShort >= consecutiveCloses and pvLowInRange and low > epShort// Entry ordersinMarket = strategy.opentrades != 0var plotTarget = 0.0var plotStop = 0.0strategy.risk.allow_entry_in(orderDirection)if not inMarket if canBuy plotTarget := targetLong plotStop := stopLong strategy.entry("long", strategy.long, stop=epLong, comment="Entry long") else if canSell plotTarget := targetShort plotStop := stopShort strategy.entry("short", strategy.short, stop=epShort, comment="Entry short") else strategy.cancel("long") strategy.cancel("short") // Exit orders strategy.exit("long", "long", stop=stopLong, limit=targetLong, comment="Exit long") strategy.exit("short", "short", stop=stopShort, limit=targetShort, comment="Exit short")else countLong := 0 countShort := 0// Trailing stopif useTrailingStop and inMarket if strategy.position_entry_name == "long" strategy.exit("long", "long", stop=stopLong, limit=plotTarget, comment="Exit long", when=stopLong > plotStop) plotStop := stopLong else strategy.exit("short", "short", stop=stopShort, limit=plotTarget, comment="Exit short", when=stopShort < plotStop) plotStop := stopShort// Plot exitplotCond(price) => inMarket ? price : inMarket[1] ? price[1] : naplot(plotCond(plotStop), "Stop loss", color.red, style=plot.style_linebr)plot(plotCond(plotTarget), "Target", color.teal, style=plot.style_linebr)