Bollinger Band Bounce — Pine Script v5 strategy
This script buys when the close drops under the lower Bollinger Band and closes the position when the close rises above the upper band. It is a mean reversion rule with no stop loss, no short side and no trend filter.
Full code (free to copy)
//@version=5
strategy("BB Bounce", overlay=true)
length = input.int(20)
mult = input.float(2.0)
[middle, upper, lower] = ta.bb(close, length, mult)
if (close < lower)
strategy.entry("Long", strategy.long)
if (close > upper)
strategy.close("Long")How the script works
The opening lines set the Pine Script v5 version and declare the strategy as BB Bounce with overlay=true, so orders and levels are drawn on the price chart rather than in a separate pane. No other strategy argument is passed: initial capital, commission, slippage and order sizing stay at the platform defaults. Nothing else runs before the band calculation.
length and mult are the only inputs, an integer fixed at 20 and a float fixed at 2.0. The ta.bb call applies both to close, so each bar's closing price is the source, and returns middle, the moving average basis, plus upper and lower, each placed mult standard deviations away from that basis. The bands are recomputed on every new bar.
Entry rests on one condition: if close is below lower, strategy.entry sends a long order with the identifier Long. Exit is the mirror condition: if close is above upper, strategy.close closes that same Long position. The script defines no stop loss, no target order and no short entry, so a position stays open until the upper band condition is met.
Parameters in the code
| Parameter | Value | Role |
|---|---|---|
length | 20 | Period of the moving average basis and of the standard deviation window used by ta.bb. |
mult | 2.0 | Multiplier applied to the standard deviation to place the upper and lower bands around the basis. |
source | close | Price series handed to ta.bb; the code passes the bar close, not an input. |
overlay | true | Draws the strategy on the price chart instead of a separate pane. |
strategy title | BB Bounce | Name given to the strategy in the strategy() declaration. |
entry order id | Long | Identifier used by strategy.entry for the long order. |
exit order id | Long | Identifier reused by strategy.close to flatten the long position. |
Pine version | 5 | Declared by the //@version=5 annotation at the top of the script. |
Known limits of this rule
- A close beyond a band is the only filter, so a brief spike can open a position that reverses on the following bars.
- In a sustained decline the close can stay under the lower band for a long stretch, and with no trend filter the long is simply held through it until the upper band is reached.
- Commission, slippage and position size are never set in the code and there is no stop loss, so real fills and costs differ from the bare rule.
Before you trust it: test it on data it never saw
A backtest in TradingView's Strategy Tester shows one past path with the settings you picked. Strategy Arena Lab (free, Windows and Linux) replays this Pine on your PC, compares its trades one by one with TradingView's for supported Pine, counts the variants you try, then judges the rule on a period kept sealed during the search: holds, mirage or inconclusive.
FAQ
Does it work on crypto?
The code only needs the close and the Bollinger Bands, so it can be applied to crypto pairs like any other symbol. Behaviour varies by market, so replay it on a period you have not seen before in Strategy Arena Lab, which is free.
How do I add a stop loss?
Add a strategy.exit call with a stop parameter, or a second close condition, after the entry block. The current code has no protective order, so the long only closes when the close is above the upper band.
Other free Pine Script v5 strategies
More TradingView paths
Educational code on past data. No investment advice; no result is promised.