Skip to main content

Bollinger Band Bounce — Pine Script v5 strategy

REVERSALBollinger BandsPine Script v5

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

ParameterValueRole
length20Period of the moving average basis and of the standard deviation window used by ta.bb.
mult2.0Multiplier applied to the standard deviation to place the upper and lower bands around the basis.
sourceclosePrice series handed to ta.bb; the code passes the bar close, not an input.
overlaytrueDraws the strategy on the price chart instead of a separate pane.
strategy titleBB BounceName given to the strategy in the strategy() declaration.
entry order idLongIdentifier used by strategy.entry for the long order.
exit order idLongIdentifier reused by strategy.close to flatten the long position.
Pine version5Declared by the //@version=5 annotation at the top of the script.

Known limits of this 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.

Get the free Lab →

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

RSI Mean ReversionEMA Crossover 9/21EMA Ribbon 8/21/55MACD MomentumSuperTrendStochastic RSIATR BreakoutVWAP BounceRSI + EMA Combo
TradingView Starter
Choose the account path. Prove the strategy separately.
Your first chart and price alert do not require the Lab. Strategy Arena proof remains required before trusting or exporting a strategy.
Partner attribution is for first-time users and web purchases only. Strategy Arena may earn a commission on eligible purchases; starting with a free account needs no payment.
I have never had an account · Start free I already have an account Check a strategy
More TradingView paths
Evidence-gated Pine review

Educational code on past data. No investment advice; no result is promised.