Strategy · Pine Script v5 · On the chart

Bollinger Band Mean Reversion

by AlnurM

This strategy calculates standard Bollinger Bands and Average True Range to implement a basic mean-reversion system.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code51
Settings4inputs
LicenceMIT
Last update7 months ago22 Mar 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

51 lines of Pine Script v5 from references/pinescript-examples/mean-reversion.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

mean-reversion.pine
//@version=5
strategy("Bollinger Band Mean Reversion", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1)

// === INPUTS ===
bbLength = input.int(20, "Bollinger Band Period", minval=5, maxval=200)
bbMult = input.float(2.0, "Bollinger Band StdDev", minval=0.5, maxval=4.0, step=0.1)
atrLength = input.int(14, "ATR Period for Stop", minval=5, maxval=50)
atrMultiplier = input.float(1.5, "ATR Stop Multiplier", minval=0.5, maxval=5.0, step=0.1)

// === INDICATORS ===
[bbMiddle, bbUpper, bbLower] = ta.bb(close, bbLength, bbMult)
atrValue = ta.atr(atrLength)

// === SIGNALS ===
// Long: price touches or crosses below lower band
longCondition = ta.crossunder(close, bbLower)
// Close long: price reaches middle band
closeLongCondition = ta.crossover(close, bbMiddle)

// Short: price touches or crosses above upper band
shortCondition = ta.crossover(close, bbUpper)
// Close short: price reaches middle band
closeShortCondition = ta.crossunder(close, bbMiddle)

// === EXECUTION ===
if longCondition
    strategy.entry("Long", strategy.long)

if closeLongCondition
    strategy.close("Long")

if shortCondition
    strategy.entry("Short", strategy.short)

if closeShortCondition
    strategy.close("Short")

// ATR-based stop loss
if strategy.position_size > 0
    longStop = strategy.position_avg_price - atrValue * atrMultiplier
    strategy.exit("Long SL", "Long", stop=longStop)

if strategy.position_size < 0
    shortStop = strategy.position_avg_price + atrValue * atrMultiplier
    strategy.exit("Short SL", "Short", stop=shortStop)

// === PLOTTING ===
plot(bbMiddle, "BB Middle", color=color.yellow, linewidth=1)
plot(bbUpper, "BB Upper", color=color.red, linewidth=1)
plot(bbLower, "BB Lower", color=color.green, linewidth=1)
fill(plot(bbUpper, display=display.none), plot(bbLower, display=display.none), color=color.new(color.gray, 90))

Overview

Bollinger Band Mean Reversion opens long positions when price drops below the lower band and short positions when price pierces above the upper band. Open trades are exited when price reverts to the middle band or triggers an ATR-based stop loss offset from the position entry price. Key parameters allow users to adjust the band period, standard deviation, ATR length, and ATR stop multiplier. Indicators and filled band bounds are drawn directly on the main price chart.

Details

Published by

AlnurM on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Mean reversion, Volatility & bands, Moving averages

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Bollinger Band Periodint20bbLength
Bollinger Band StdDevfloat2.0bbMult
ATR Period for Stopint14atrLength
ATR Stop Multiplierfloat1.5atrMultiplier
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

218 stars

The Mean Reversion Strategy Template calculates a baseline mean using SMA, EMA, or VWAP alongside upper and lower deviation bands based on standard deviation, ATR, or percentage offsets. It generates long and short entries when price touches, bounces from, or

Strategy v6 Mean reversionMoving averages
Updated 3 months ago · None stated GitHub repository of Mean Reversion Strategy Template
106 stars

This strategy backtests a long Bollinger Band deal on the price chart for 3Commas-style bots. It plots short and long simple moving averages, offset bands, and the average entry price of the open deal. A long starts when price crosses the lower band inside a c

Strategy v4 Volatility & bandsMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of 3Commas Bollinger Strategy

Bollinger Bands Strategy

by cklose2000
105 stars

This strategy generates buy and sell signals based on Bollinger Band crossovers combined with a simple moving average trend filter. It enters a long position when price crosses above the lower band while trading above the SMA filter line, and enters a short po

Strategy v5 Volatility & bandsMoving averages
Updated 1 year ago · None stated GitHub repository of Bollinger Bands Strategy

Bollinger Bands Strategy

by TWODS-CAPITAL
37 stars

This strategy executes trades based on price interactions with standard Bollinger Bands plotted on the price chart. It calculates a simple moving average basis line along with upper and lower standard deviation bands. Long entries are triggered when price cros

Strategy v5 Volatility & bandsMean reversion
Updated 3 years ago · MIT GitHub repository of Bollinger Bands Strategy
9 stars

This strategy scaffold calculates a standard Volume Weighted Average Price (VWAP) alongside upper and lower volatility bands set 1.5 ATRs away. It generates long entry signals when a confirmed bar closes below the lower band and short entry signals when a conf

Strategy v5 Mean reversionMoving averages
Updated 12 days ago · Custom licence GitHub repository of Nexural VWAP Reversion Scaffold

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers