Indicator · Pine Script v5 · On the chart

Bollinger Band Swing

by benso87

This indicator plots Bollinger Bands on the price chart alongside a baseline moving average to identify trend-aligned swing setups.

View on GitHub

GitHub stars24on the repository
Pine versionv5runs as is
Lines of code121
Settings17inputs
LicenceMPL-2.0
Last update4 years ago5 Oct 2022

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 indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

121 lines of Pine Script v5 from BB Swing indicator.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

BB Swing indicator.pine
//@version=5
//
// Author: benso87
// benso87.com
//
//
indicator(title='Bollinger Band Swing', shorttitle='BB Swing', format=format.price, precision=2, timeframe='', timeframe_gaps=true, overlay=true)

noColor = color.new(color.white, transp=100)
///////////////////////////////////////////////////////////////////////////////
// Inputs

// General
src = input.source(close, title='Source', group='General')

// Moving Average
maLength = input.int(200, title='EMA Length', minval=1, group='Moving Average')
maType = input.string( "EMA", title="Type",    options=['SMA','EMA'], group="Moving Average")
maLookback = input.int(1, title='Slope Lookback', minval=1, group='Moving Average')

// Bollinger Bands
bbLength = input.int(21, minval=1, group='Bollinger Bands')
bbMult = input.float(2.0, minval=0.001, maxval=50, title="StdDev", group='Bollinger Bands')
defaultFill = input.color(noColor, title='Fill Color', group='Bollinger Bands')
fillType = input.string('Signal', title='Fill Type', options=['Static', 'Bias', 'Signal'], group='Bollinger Bands')
longFill = input.color(color.new(color.green, transp=80), title='Long Bias', group='Bollinger Bands')
shortFill = input.color(color.new(color.red, transp=80), title='Short Bias', group='Bollinger Bands')

// Stoch RSI
smoothK = input.int(3, title='K', minval=1, group='Stochastic RSI')
smoothD = input.int(3, title='D', minval=1, group='Stochastic RSI')
rsiLength = input.int(21, minval=1, title='RSI Length', group='Stochastic RSI')
stochLength = input.int(14, title='Stoch Length', minval=1, group='Stochastic RSI')
srsiHigh = input.float(90, title='High', minval=0, maxval=100, group='Stochastic RSI')
srsiLow = input.float(10, title='Low', minval=0, maxval=100, group='Stochastic RSI')

// ATR
atrLength = input.int(21, title='Length', minval=1, group='Average True Range')


///////////////////////////////////////////////////////////////////////////////
// Functions

// Moving average type
f_ma(source, length, type) =>
    switch type
        'SMA' => ta.sma(source, length)
        'EMA' => ta.ema(source, length)
        'RMA' => ta.rma(source, length)
        'WMA' => ta.wma(source, length)


///////////////////////////////////////////////////////////////////////////////
// Calculations

// Moving Average
ma = f_ma(src, maLength, maType)
maSlope = ma - ma[maLookback]

// Bollinger Bands
basis = ta.sma(src, bbLength)
dev = bbMult * ta.stdev(src, bbLength)
upper = basis + dev
lower = basis - dev

// Stoch RSI
rsi = ta.rsi(src, rsiLength)
k = ta.sma(ta.stoch(rsi, rsi, rsi, stochLength), smoothK)
d = ta.sma(k, smoothD)

// ATR
atr = ta.atr(atrLength)

// Biases
longBias = basis >= ma
shortBias = basis <= ma

// Stop loss line
slLong = lower - (2 * atr)
slShort = upper + (2 * atr)

// Signals
longSignal = longBias and k <= srsiLow
shortSignal = shortBias and k >= srsiHigh

// Fill color
biasFillLong = fillType == 'Bias' and longBias
biasFillShort = fillType == 'Bias' and shortBias
signalFillLong = fillType == 'Signal' and longSignal
signalFillShort = fillType == 'Signal' and shortSignal
fillColor = biasFillLong or signalFillLong ? longFill : biasFillShort or signalFillShort ? shortFill : defaultFill


///////////////////////////////////////////////////////////////////////////////
// Plots

// Bollinger Bands
plot(basis, title='Basis', color=color.orange, linewidth=1)
p1 = plot(upper, title='Upper', color=color.blue, linewidth=2)
p2 = plot(lower, title='Lower', color=color.blue, linewidth=2)
fill(p1, p2, title='BB Background', color=fillColor)

// Moving Average
plot(ma, title='MA', color=color.yellow, linewidth=2)

// Stop Loss
sl1 = plot(slLong, title='Long SL', color=color.new(color.red, transp=50), linewidth=1)
sl2 = plot(slShort, title='Short SL', color=color.new(color.red, transp=50), linewidth=1)

///////////////////////////////////////////////////////////////////////////////
// Alerts

alertcondition(longSignal or shortSignal, title='Any Signal', message='BB Swing Signal')
alertcondition(longSignal, title='Long Signal', message='BB Swing Long Signal')
alertcondition(shortSignal, title='Short Signal', message='BB Swing Short Signal')
alertcondition(k <= srsiLow, title='Stoch RSI High', message='Stoch RSI Low')
alertcondition(k >= srsiHigh, title='Stoch RSI High', message='Stoch RSI High')
alertcondition(src >= upper, title='Price Above BB', message='Price Above BB')
alertcondition(src <= lower, title='Price Below BB', message='Price Below BB')
alertcondition(src >= slShort, title='Price Above Long SL', message='Price Above Short SL')
alertcondition(src <= slLong, title='Price Below Short SL', message='Price Below Short SL')

Overview

Bollinger Band Swing calculates trend bias by comparing the Bollinger Bands basis to a customizable moving average, such as a 200 EMA. Trade signals trigger when the bias aligns with overbought or oversold Stochastic RSI thresholds. The script also calculates ATR-based stop loss levels and shades the band background by signal or bias state. As an indicator, it provides visual reference lines and color fills without automated order execution or trade exits.

Details

Published by

benso87 on GitHub

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Volatility & bands, Oscillators & momentum, Moving averages, Trend

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
Sourcesourceclosesrc
EMA Lengthint200maLength
TypestringEMAmaType
Slope Lookbackint1maLookback
bbLengthint21bbLength
StdDevfloat2.0bbMult
Fill ColorcolornoColordefaultFill
Fill TypestringSignalfillType
Long Biascolorcolor.new(color.green, transp=80)longFill
Short Biascolorcolor.new(color.red, transp=80)shortFill
Kint3smoothK
Dint3smoothD
Show all 17 settings
SettingTypeDefaultVariable
RSI Lengthint21rsiLength
Stoch Lengthint14stochLength
Highfloat90srsiHigh
Lowfloat10srsiLow
Lengthint21atrLength
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

114 stars

ADX Strategy (original) is an overlay indicator that identifies trend-following trade signals using the Average Directional Index (ADX) combined with a Simple Moving Average filter. A long signal is generated when the ADX rises above a user-defined trend level

Indicator v4 TrendOscillators & momentum
Updated 1 year ago · None stated GitHub repository of ADX Strategy (original)
56 stars

Double Screened Decision is a multi-timeframe trend and momentum indicator overlaid directly on the price chart. It dynamically selects a higher timeframe based on the active chart resolution to evaluate higher-timeframe MACD, Bollinger Bands, RSI, and Stochas

Indicator v5 TrendMoving averages Repaints
Updated 4 months ago · None stated GitHub repository of Double Screened Decision

Index Intraday Analyst

by chris-c-thomas
14 stars

Index Intraday Analyst overlays CBOE cash indices such as SPX, NDX, RUT, and DJX. It plots a multi-EMA ribbon, a session-anchored TWAP with standard-deviation bands, and session levels such as prior-day high, low, and close, the opening range, and the high and

Indicator v6 TrendOscillators & momentum
Updated 6 months ago · MIT GitHub repository of Index Intraday Analyst

RePaNoCHa V4

by wocclyl
9 stars

RePaNoCHa V4 is an overlay alert indicator that plots a Tillson T3 moving average on the price chart and colors it by whether the line is rising or falling. It combines that slope with a range filter, ADX, parabolic SAR, RSI, and MACD to mark long and short si

Indicator v4 TrendOscillators & momentum
Updated 7 years ago · MIT GitHub repository of RePaNoCHa V4

Kijun-Sen + SSL + WAE

by krosenfeld7
5 stars

Combines the Kijun-Sen baseline, SSL channel crossovers, and Waddah Attar Explosion momentum calculations to generate directional trade signals. The indicator plots the Kijun-Sen and SSL lines directly on the price chart. When price aligns relative to the Kiju

Indicator v4 TrendOscillators & momentum
Updated 3 years ago · Apache-2.0 GitHub repository of Kijun-Sen + SSL + WAE

More indicators

Trade Your Charts With a Regulated Broker

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

Compare brokers