Strategy · Pine Script v5 · On the chart

Bollinger Bands Strategy

by suyons· code credited to syoungs

This strategy plots a customizable Bollinger Band basis using selectable moving average types such as HMA, SMA, EMA, or others.

View on GitHub

GitHub stars7on the repository
Pine versionv5runs as is
Lines of code70
Settings10inputs
LicenceNone statedfile: MPL-2.0
Last update4 months ago21 Jun 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

70 lines of Pine Script v5 from bollinger-bands/02-bollinger-bands-strategy.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

02-bollinger-bands-strategy.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © syoungs

//@version=5
strategy("Bollinger Bands Strategy", "Bollinger Bands Strategy", overlay=true)

// Input settings
length_basis = input.int(120, "Basis Len", minval=1, inline="1", group="Band")
length_stdev = input.int(120, "Stdev Len", minval=1, inline="1", group="Band")
src = input(close, title="Source", inline="2", group="Band")
type_ma = input.string(title="MA Type", defval="HMA", inline="2", options = ["SMA", "EMA", "WMA", "HMA", "RMA", "VWMA", "ALMA"], group="Band")

// Basis calculation
basis = switch type_ma
    "SMA" => ta.sma(src, length_basis)
    "EMA" => ta.ema(src, length_basis)
    "WMA" => ta.wma(src, length_basis)
    "HMA" => ta.hma(src, length_basis)
    "RMA" => ta.rma(src, length_basis)
    "VWMA" => ta.vwma(src, length_basis)
    "ALMA" => ta.alma(src, length_basis, 1, 0)

basis_color = ta.roc(basis, 1) >= 0 ? color.new(#26a69a, 20) : color.new(#ff5555, 20)
bb_stdev = ta.stdev(close, length_stdev, false)

// Levels
L1 = input.float(2.0, "L1", inline="3", group="Band")
L2 = input.float(1.0, "L2", inline="3", group="Band")
L3 = input.float(0.5, "L3", inline="3", group="Band")
L4 = input.float(-0.5, "L4", inline="4", group="Band")
L5 = input.float(-1.0, "L5", inline="4", group="Band")
L6 = input.float(-2.0, "L6", inline="4", group="Band")

// Level calculations
pL1 = basis + L1 * bb_stdev
pL2 = basis + L2 * bb_stdev
pL3 = basis + L3 * bb_stdev
pL4 = basis + L4 * bb_stdev
pL5 = basis + L5 * bb_stdev
pL6 = basis + L6 * bb_stdev

// Plotting
plot(basis, "MA", color=basis_color, linewidth=2, display=display.none)
p_pL1 = plot(pL1, "L1", color=color.new(#b2b5be, 50), style=plot.style_stepline, display=display.none)
plot(pL2, "L2", color=color.new(#ff9800, 50), display=display.none)
plot(pL3, "L3", color=color.new(#b2b5be, 50), display=display.none)
plot(pL4, "L4", color=color.new(#b2b5be, 50), display=display.none)
plot(pL5, "L5", color=color.new(#ff9800, 50), display=display.none)
p_pL6 = plot(pL6, "L6", color=color.new(#b2b5be, 50), style=plot.style_stepline, display=display.none)
fill(p_pL1, p_pL6, title="Fill", color=color.new(#b2b5be, 85), display=display.all)

alertcondition(ta.crossover(close, pL1) or ta.crossunder(close, pL6), "BB 2.0", "{{interval}} / BB 2.0")

perB_width = bb_stdev
perB = close > basis ? (high - basis) / bb_stdev : (low - basis) / bb_stdev

// Buy and sell signals
buySignal = ta.crossunder(low, pL6)
sellSignal = ta.crossover(high, pL1)

// Plot buy and sell signals
plotshape(buySignal, title="Buy Signal", style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 20), size=size.small)
plotshape(sellSignal, title="Sell Signal", style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 20), size=size.small)

// Strategy entries and exits
if (buySignal)
    strategy.entry("Buy", strategy.long)

if (sellSignal)
    strategy.close("Buy")

Overview

Bollinger Bands Strategy draws the basis line, multiple standard deviation levels, and fills the region between L1 and L6 on the price chart. Buy signals trigger on low price crossing below L6 and sell signals on high price crossing above L1. The strategy enters long positions on buy signals and closes positions on sell signals with no stop loss. Main settings adjust basis length, standard deviation length, moving average type, and the six L multipliers.

Details

Published by

suyons on GitHub

Code credit

syoungs, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Volatility & bands, Moving averages, Support & resistance

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

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

SettingTypeDefaultVariable
Basis Lenint120length_basis
Stdev Lenint120length_stdev
Sourcesourceclosesrc
MA TypestringHMAtype_ma
L1float2.0L1
L2float1.0L2
L3float0.5L3
L4float-0.5L4
L5float-1.0L5
L6float-2.0L6
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

257 stars

This overlay strategy is built for ES futures on the one-minute chart. It plots a volume-weighted moving average of price opens and standard-deviation bands at Fibonacci ratios, plus a second longer volume-weighted average from a higher-timeframe open. Call an

Strategy v5 Volatility & bandsMoving averages
Updated 6 weeks ago · None stated GitHub repository of ES Futures 1M No Repaint

KC15

by benso87
24 stars

KC15 is a price-chart strategy that configures two moving averages, a Bollinger Band Width Percentile filter, and PMARP entry and exit thresholds for long and short trades. Users can switch each average between SMA and EMA, set lengths and colors, and toggle l

Strategy v5 Moving averagesVolatility & bands
Updated 4 years ago · MPL-2.0 GitHub repository of KC15
8 stars

This strategy combines moving averages, ATR volatility bands and ADX trend strength on the price chart. It calculates ADX from price data and adjusts the ATR multiplier dynamically based on whether ADX is rising or falling. Long and short entries are controlle

Strategy v4 TrendMoving averages
Updated 4 years ago · None stated GitHub repository of MA+ATR+ADX+PP Strategy
106 stars

This strategy plots a volume-weighted average price, Bollinger Bands built on that VWAP, a 200-period simple moving average, and Fibonacci pivot levels in a separate pane. It opens a long position when VWAP crosses above the band basis and the close is at or a

Strategy v4 Volatility & bandsSupport & resistance
Updated 2 years ago · GPL-3.0 GitHub repository of BBofVWAP with entry at Pivot Point

This overlay strategy offers two selectable entry modes: a Bollinger squeeze on typical price and a simple moving-average cross. It filters trades by weekday, session, date range, and an optional news window, and can restrict direction or invert the side. Risk

Strategy v5 Volatility & bandsMoving averages
Updated today · None stated GitHub repository of Boilerplate Configurable Strategy [Yosiet]

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