BollingerBands  |  BollingerOnBollingerBands  |  BBForex  |  EquityTrader  |  BollingerVideo  |  GroupPower  |  MarketTechnician  |  PatternPower  |  BBScript  |  Mobile  

Plotting Bollinger Bands® on the price chart.

Script

// Bollinger Bands in BBScript
// Copyright John Bollinger 2011
// Use the data from the chart
data("x")
// Use the close
myData = close(x)
// Set the length
period = 20
// Set the width
width = 2.0
// The middle band is a simple moving average
middleBB =sma(myData, period)
// The width is driven by standard deviation
volatility = stdev(myData, period)
// This is the upper band
upperBB = middleBB + width * volatility
// This is the lower band
lowerBB = middleBB - width * volatility
// Create the objects to be plotted
// dark red line
plot1 = plot(upperBB, "upperBB", "line", "CC0000")
// blue line
plot2 = plot(middleBB, "middleBB", "line", "0000FF")
// dark green line
plot3 = plot(lowerBB, "lowerBB", "line", "009900")
// draw the bands on the price chart
pchart(plot1, plot2, plot3)
// That's all folks!


Plotting %b Bollinger Band indicator.

Script

// %b in BBScript
// Copyright John Bollinger 2011
// Use the data from the chart
data("x")
// Use the close
myData = close(x)
// Set the length
period = 20
// Set the width
width = 2.0
// The middle band is an average
middleBB =sma(myData, period)
// The width is driven by standard deviation
volatility = stdev(myData, period)
// This is the upper band
upperBB = middleBB + width * volatility
// This is the lower band
lowerBB = middleBB - width * volatility
// %b
pct_b = (myData - lowerBB) / (upperBB - lowerBB)
// Create the objects to be plotted
// Blue indicator line
plot1 = plot(pct_b, "%b", "line", "0000FF")
// Black reference lines with no labels
plot2 = plot(0.0, "", "line", "000000")
plot3 = plot(0.5, "", "line", "000000")
plot4 = plot(1.0, "", "line", "000000")
// Draw the indicator and references
chart(plot1, plot2, plot3, plot4)
// That's all folks!


Plotting BandWidth Bollinger Band indicator.

Script

// BandWidth in BBScript
// Copyright John Bollinger 2011
// Use the data from the chart
data("x")
// Use the close
myData = close(x)
// Set the length
period = 20
// Set the width
width = 2.0
// Since BandWidth is twice the width times the
// coefficeint of variation we can take a short cut
BandWidth = 2 * width * (stdev(myData, period) / sma(myData, period))
// Create the objects to be plotted
// Blue indicator line
plot1 = plot(BandWidth, "BandWidth", "line", "0000FF")
// Black 0 reference lines with no label
plot2 = plot(0.0, "", "line", "000000")
// Draw the indicator and reference
chart(plot1, plot2)
// That's all folks!


Plotting Normalized Volume indicator.

Script

// Normalize Volume in BBScript
// Copyright Bollinger Capital 2011
// Use the data from the chart
data("x")
// get volume array
myVolume = volume(x)
// Set the normalized volume period
period = 50
//normalized volume is volume divided by volume moving average
nv = myVolume/(sma(myVolume,period))*100
// Create the objects to be plotted
// indicator plot, vertical lines (histogram style)
plot1 = plot(nv, "Norm Volume", "histogram")
// Black 100 reference lines with no label
plot2 = plot(100, "", "line", "000000")
chart(plot1,plot2)
// That's all folks!


Plotting Rate of Change indicator.

Script

// Rate of Change in BBScript
// Copyright Bollinger Capital 2011
// Use the data from the chart
data("x")
// get close array
myData= close(x)
// Set the ROC period
period = 12
//ROC is rate of change from close within period of samples
rocArray = (myData - myData[-period]) /myData[-period]*100
// Create the objects to be plotted
// indicator plot in red
plot1 = plot(rocArray, "ROC", "line","ff0000")
// Black 0 reference lines with no label
plot2 = plot(0, "", "line", "000000")
chart(plot1,plot2)
// That's all folks!


Plotting Simple Volatility Breakout signals.

Script

// Simple Volatility Breakout logic in BBScript
// Copyright John Bollinger 2011
// Set the length
period = 20
// Set the width
width = 2.0
// Lookback period for the Squeeze
lookback = 125
// Window for the Squeeze
window = 3
// Use the data from the chart
data("x")
// Use the close
last = close(x)
// Bollinger Bands and indicators
middleBB = sma(last, period)
upperBB = middleBB + width * stdev(last, period)
lowerBB = middleBB - width * stdev(last, period)
BandWidth = (upperBB - lowerBB) / middleBB
pctB = (last - lowerBB) / (upperBB - lowerBB)
// Squeeze
Squeeze = within(equal(BandWidth, movmin(BandWidth, lookback)), window)
// Breakouts
BreakUp = greater(pctB, 1.0)
BreakDown = less(pctB, 0.0)
// Volatility Breakout
VolBreak = and(Squeeze, BreakUp) + and(Squeeze, BreakDown) *-1
/* Create plot object with signals anchored to close
The plot color scheme is AARRGGBB
00 = 0%, 40 = 25%, 80 = 50%, C0 is 75% and FF = 100%
AA controls the transparency, RR the amount of red,
GG the amount of green and BB the amount of blue
The values are hex numbers from 00 to FF
"800000FF" is 50% transparent blue
"0000000" is an invisible line */

VBplot = plot(last, "Vol Break", "line", "00000000", VolBreak)
// Plot it on price chart
pchart(VBplot)
// That's all folks!


Plotting Intraday Intensity Oscillator indicator.

Script

// Intraday Intensity % in BBScript
// Copyright Bollinger Capital 2011
// Use the data from the chart
data("x")
// Set the II% period
period=21
//array of closes
lastArray = close(x)
//array of highs
highArray = high(x)
//array of lows
lowArray = low(x)
//array of volumes
volArray = volume(x)
//temp array is twice of close - high and low, divided by difference between high and low multiplied by volume
temp = (2*lastArray - highArray - lowArray) / (highArray - lowArray)*volArray
//intraday intensity oscillator
//simple moving average of temp
//divided by simple moving average of volume
ii=sma(temp,period)/sma(volArray,period)*100
// indicator plot histogram style
plotII = plot(ii,"II%","histogram","000000")
//display indicator plot
chart(plotII)
// That's all folks!


Plotting Accumulation Destribution Line indicator with exponentional moving average.

Script

// Accumulation Destribution Line in BBScript
// Copyright Bollinger Capital 2011
// Use the data from the chart
data("x")
ema_period = 20//ema period
//array of opens
openArray = open(x)
//array of closes
lastArray = close(x)
//array of highs
highArray = high(x)
//array of lows
lowArray = low(x)
//array of volumes
volArray = volume(x)
//initialize adline array to 0
adlineData=array(0)
//calculate clv
clv = (lastArray-openArray)/(highArray-lowArray)*volArray
//calculate the accumulation sum
//bbscript starts from earliest to latest, sets current value to current clv value plus the previous
//and moves to the next value and repeats
adlineData=adlineData[-1]+clv
//normalize the adline (between 1 and -1)by dividing it with the maximum absolute value of the entire array
maxAbsAdline = movmax(abs(adlineData))
adlineData=adlineData/maxAbsAdline
//calculate the exponentional moving average of the AD line
emaAD = ema(adlineData,ema_period)

adlinePlot = plot(adlineData, "AD","line","ff0000")//AD line plot, red line
emaADPlot = plot(emaAD,"EMA","line","000000")//ema line plot, black line
//display AD line and ema line on plot
chart(adlinePlot,emaADPlot)
// That's all folks!


Plotting typical price on price chart.

Script

// Typical Price Line in BBScript
// Copyright Bollinger Capital 2011
// Use the data from the chart
data("x")
//array of closes
lastArray = close(x)
//array of highs
highArray = high(x)
//array of lows
lowArray = low(x)

//calculate typical price (close + high + low)/3
typical_price = (lastArray + highArray + lowArray)/3

typical_pricePlot = plot(typical_price , "TP","line","ff0000")//Typical Price line plot, red line

//display typical line on price chart
pchart(typical_pricePlot)
// That's all folks!


Plotting momentum indicator and its ema.

Script

// Momentum Indicator in BBScript
// Copyright Bollinger Capital 2011
// Use the data from the chart
data("x")//data object

//create momentum indicator and its ema
period1 = 12//mtm period
period2 = 12//ema period

mtmData = close(x) - close(x)[-period1]//mtm formula
ema_mtm = ema(mtmData,period2)//ema of mtm

plot1 = plot(mtmData,"Momentum","histogram","ff0000")//mtm plot
plot2 = plot(ema_mtm, "EMA", "line", "0000ff")//plot of ema

chart(plot1,plot2)//display mtm and ema in indicator chart
// That's all folks!


Plotting Bollinger Envelopes on the price chart.

Script

// Bollinger Envelopes in BBScript
// Copyright John Bollinger 2011
// Set the length
period = 20
// Set the width
width = 1.5
// Use the data from the chart
data("x")
// Use the highs and the lows
highs = high(x)
lows = low(x)
// This is the upper envelope
upperBE = sma(highs, 20) + width * stdev(highs, 20)
// This is the lower envelope
lowerBE = sma(lows, 20) - width * stdev(lows, 20)
// There is no middle band, so we must imply one
middleBE = (upperBE + lowerBE) / 2
// Create the objects to be plotted
// dark red line, 50% solid
plot1 = plot(upperBE, "upperBE", "line", "80C00000")
// blue line, 50% solid
plot2 = plot(middleBE, "middleBE", "line", "800000FF")
// dark green line, 50% solid
plot3 = plot(lowerBE, "lowerBE", "line", "80009000")
// draw the bands on the price chart
pchart(plot1, plot2, plot3)
// That's all folks


Plotting 52 Week Highs and Lows on the price chart.

Script

// 52 Week Highs and Lows in BBScript
// Copyright John Bollinger 2011
//choose from these periods for 1 year, 1/2 year and 3 months highs and lows
one_year = 252//one year
six_months = 126//six months
three_months = 63//3 months

period = one_year//set to 52 week
// Use the data from the chart
data("x")
highs=movmax(high(x),period)//moving 52 week high
lows=movmin(low(x),period)//moving 52 week low

highsPlot=plot(highs,"52wkh","line","ff0000")//moving 52 wk high in red
lowsPlot=plot(lows,"52wkl","line","0000ff")//moving 52 wk low in blue

//display on price chart
pchart(highsPlot,lowsPlot)
// That's all folks


Plotting Tushar Chande's Q-stick Indicator.

Script

// Q-stick Indicator in BBScript
// Copyright John Bollinger 2011
// Tushar Chande Q-stick indicator
data("x")

//close - open
temp = close(x) - open(x)
//period
period = 14
//qstick, ema of close - open
qstick = ema(temp,period)
//you can also use the sma of close - open too
//qstick = sma(temp, period)

//qtick plot, red line
qstickPlot = plot(qstick,"QSTK","line","ff0000")

//draw qstick indicator
chart(qstickPlot)
// That's all folks


Plotting Money Flow Index Indicator.

Script

// Money Flow Index Indicator in BBScript
// Copyright John Bollinger 2011
// Tushar Chande Q-stick indicator
data("x")//get data
period = 14//mfi period

typical_price=(close(x)+high(x)+low(x))/3 //typical price

mf=typical_price*volume(x)//money flow if typical price multiplied by volume

//positive money flow
//current typical price greater than or equal prior, set to mf, otherwise 0
p=if(greaterorequal(typical_price,typical_price[-1]),mf,0)

//negative money flow
//current typical price less than prior, set to mf, otherwise 0
n=if(less(typical_price,typical_price[-1]),mf,0)

pmf=movsum(p,period)//total positive money flow in moving period
nmf=movsum(n,period)//total negative money flow in moving period

//mfi formula
mfiData=if(equal(pmf+nmf,0),0,100*pmf/(pmf+nmf))//if dividing by zero, set to 0, otherwise use mfi formula

//plot mfi line in red
mfiPlot = plot(mfiData,"MFI","line","ff0000")

//display mfi indicator chart
chart(mfiPlot)
// That's all folks


Plotting John Bollinger's Stochastic Display Indicator.

Script

// BBScript example
// John Bollinger's Stochastic Display
// Copyright John Bollinger 2011
lookback = 10 // lookback period
data("x") // use the data from the chart

// use the close, high, low
myClose = close(x)
myHigh = high(x)
myLow = low(x)

// stochastic components
highest = movmax(myHigh, lookback)
lowest = movmin(myLow, lookback)
numerator = myClose - lowest
denominator = highest - lowest

// raw stochastic and smoothings
stoch = numerator / denominator
stoch1 = ema(stoch, 3)
stoch2 = ema(stoch1, 3)

// plot objects
stochPlot = plot(stoch, "stoch", "line","3300FF")
stochPlot1 = plot(stoch1, "stoch1", "line", "CC0000")
stochPlot2 = plot(stoch2, "stoch2", "line", "339900")

// reference lines
myRef0 = plot(0.0, "0.0")
myRef1 = plot(1.0, "1.0")

// draw the charts using the plot objects
chart(stochPlot, stochPlot1, stochPlot2, myRef0, myRef1)


Plotting John Bollinger's BBAccumulation Indicator using BBScript1.1 built-in indicator functions.

Script

// BBScript example
// John Bollinger's BBAccumulation (tm)
// Copyright 2012 by John Bollinger
// Combines three popular measures of supply and demand
// in a normalized Bollinger Band framework.
// Use the data from the chart
data("x")
// Vary the next two lines to suit your needs
len = 20 // Length
width = 2.0 // Width
// Accumulation Distribution section
AD = adline(x)
pct_bAD = (AD - sma(AD, len)) / (width * stdev(AD, len))
// Intraday Intensity section
II = iiline(x)
pct_bII = (II - sma(II, len)) / (width * stdev(II, len))
// On Balance Volume section
OBV = obv(x)
pct_bOBV = (OBV - sma(OBV, len)) / (width * stdev(OBV, len))
// BBAccumulation
BBAccum = (pct_bAD + pct_bII + pct_bOBV) / 3
// create the plot object
BBAccumulation = plot(BBAccum, "BBAccumulation", "histogram")
// Un-comment the next two lines if you want reference levels
// top = plot(1.0, "Top ref.", "line")
// bot = plot(-1.0, "Bottom ref.", "line")
// plot the results
// Comment out the next line and un-comment the line after for reference levels
chart(BBAccumulation)
// chart(BBAccumulation, top, bot)


Plotting Bollinger Bands® on RSI using BBScript1.1 built-in indicator functions.

Script

// BBScript example
// Bollinger Bands on RSI
// Copyright 2012 by John Bollinger
// Use the data from the chart
data("x")
// Vary the next three lines to suit your needs
RSI_len = 14 // RSI Length
BB_len = 50 // BB Length
BB_width = 2.1 // BB Width
rs = rsi(x, RSI_len) // RSI
// Bollinger Bands on RSI
upperBB = sma(rs, BB_len) + BB_width * stdev(rs, BB_len)
middleBB = sma(rs, BB_len)
lowerBB = sma(rs, BB_len) - BB_width * stdev(rs, BB_len)
// create the plot objects
rsi_plot = plot(rs, "RSI", "line", "000000")
upperBB_plot = plot(upperBB, "upper BB", "line", "ff0000")
middleBB_plot = plot(middleBB, "middle BB", "line", "0000ff")
lowerBB_plot = plot(lowerBB, "lower BB", "line", "00ff00")
// plot the results
chart(rsi_plot, upperBB_plot, middleBB_plot, lowerBB_plot)


Plotting MFI normalized with Bollinger Bands® using BBScript1.1 built-in indicator functions.

Script

// BBScript example
// MFI normalized with Bollinger Bands
// From "Bollinger on Bollinger Bands"
// Chapter 21
// Copyright 2012 by John Bollinger
// Use the data from the chart
data("x")
// Vary the next three lines to suit your needs
MFI_len = 10 // MFI Length
BB_len = 40 // BB Length
BB_width = 2.0 // BB Width
// MFI
mf = mfi(x, MFI_len)
// Bollinger Bands on MFI
upperBB = sma(mf, BB_len) + BB_width * stdev(mf, BB_len)
middleBB = sma(mf, BB_len)
lowerBB = sma(mf, BB_len) - BB_width * stdev(mf, BB_len)
// %b on MFI
pct_b_mfi = (mf - lowerBB) / (upperBB - lowerBB)
// create the plot object
mfi_plot = plot(pct_b_mfi, "BB Normalized MFI", "line", "0000ff")
// Reference levels
one = plot(1, "one")
zero = plot(0, "zero")
// Plot the results
chart(mfi_plot, one, zero)


Plotting two independent sets of Bollinger Bands®.

Script

// BBScript example
// Bollinger Bands in BBScript
// Two independent sets of Bollinger Bands
// Copyright John Bollinger 2012
// Use the data from the chart
data("x")
// Use the close
myData = close(x)
// Set the length
period1 = 20
period2 = 50
// Set the widths
width1 = 2.0
width2 = 2.0
// The middle bands are averages
middleBB1 = sma(myData, period1)
middleBB2 = sma(myData, period2)
// The widths are driven by standard deviation
volatility1 = stdev(myData, period1)
volatility2 = stdev(myData, period2)
// The upper bands
upperBB1 = middleBB1 + width1 * volatility1
upperBB2 = middleBB2 + width2 * volatility2
// The lower bands
lowerBB1 = middleBB1 - width1 * volatility1
lowerBB2 = middleBB2 - width2 * volatility2
// Create the objects to be plotted
// dark red lines
plotUpper1 = plot(upperBB1, "upperBB 1", "line", "CC0000")
plotMid1 = plot(middleBB1, "middleBB 1", "line", "CC0000")
plotLower1 = plot(lowerBB1, "lowerBB 1", "line", "CC0000")
// dark green lines
plotUpper2 = plot(upperBB2, "upperBB 2", "line", "009900")
plotMid2 = plot(middleBB2, "middleBB 2", "line", "009900")
plotLower2 = plot(lowerBB2, "lowerBB 2", "line", "009900")
// draw the bands on the price chart
pchart(plotUpper1, plotMid1, plotLower1, plotUpper2, plotMid2, plotLower2)


Plotting two sets of Bollinger Bands® built on the same middle band.

Script

// BBScript example
// Bollinger Bands® in BBScript™
// Two sets of Bollinger Bands
// Built on the same middle band
// Copyright John Bollinger 2012
// Use the data from the chart
data("x")
// Use the close
myData = close(x)
// Set the length
period = 20
// Set the widths
width1 = 1.5
width2 = 3.0
// The middle band is an average
middleBB =sma(myData, period)
// The width is driven by standard deviation
volatility = stdev(myData, period)
// The upper bands
upperBB2 = middleBB + width2 * volatility
upperBB1 = middleBB + width1 * volatility
// The lower bands
lowerBB1 = middleBB - width1 * volatility
lowerBB2 = middleBB - width2 * volatility
// Create the objects to be plotted
// dark red lines
plotUpper2 = plot(upperBB2, "upperBB 2", "line", "CC0000")
plotUpper1 = plot(upperBB1, "upperBB 1", "line", "CC0000")
// blue line
plotMid = plot(middleBB, "middleBB", "line", "0000FF")
// dark green lines
plotLower1 = plot(lowerBB1, "lowerBB 1", "line", "009900")
plotLower2 = plot(lowerBB2, "lowerBB 2", "line", "009900")
// draw the bands on the price chart
pchart(plotUpper2, plotUpper1, plotMid, plotLower1, plotLower2)


Plotting %K and %R.

Script

// %K and %R
// Copyright John Bollinger 2012
// Use the data from the chart
data("x")
// data to use
myClose = close(x)
myHigh = high(x)
myLow = low(x)
// Lookback period
len = 10
// %K
K = (myClose - movmin(myClose, len)) / (movmax(myClose, len) - movmin(myClose, len))
K1 = (myClose - movmin(myLow, len)) / (movmax(myHigh, len) - movmin(myLow, len))
// %R
R = (movmax(myClose, len) - myClose) / (movmax(myClose, len) - movmin(myClose, len))
R1 = (movmax(myHigh, len) - myClose) / (movmax(myHigh, len) - movmin(myLow, len))
// K and R
plot1 = plot(K, "%K -- single series", "line", "0000FF")
plot2 = plot(R, "%R -- single series", "line", "FF0000")
// K1 and R1
plot3 = plot(K1, "%K1 -- high and low", "line", "0000FF")
plot4 = plot(R1, "%R1 -- high and low", "line", "FF0000")
// Black reference lines with no labels
ref1 = plot(0.0, "")
ref2 = plot(1.0, "")
// Draw the indicators and references
chart(ref1, ref2, plot1, plot2)
chart(ref1, ref2, plot3, plot4)
// That's all folks!


Simple Bollinger Band System, discrete trades with stops and no pyramiding backtester and equity curve plot.

Script

// Written by John Bollinger April 2014
// use the data from the chart
data("x")
// Bollinger Bands using built-in functions
middleBB = bbands(x, 20, 2, "middle")
lowerBB = bbands(x, 20, 2, "lower")
// back in the lower BBands = buy
entry = xover(close(x), lowerBB)
// tag the middle BBand = sell
exit = -xover(close(x), middleBB)
// group buy and sell signals in one array
signals = entry + exit
// back test type 4 = discrete trades, use stops, no pyramiding
back_type = 4
// stop type = Chandelier
stop_type = 0
// run the back test
bt = backtest(x, signals, back_type, stop_type)
// prepare price chart with signals
plot1 = plot(close(x), "signals", "line", "00000000", bt)
// show chart with signals
pchart(plot1)
// calculate equity curve without compounding
equity_curve_calc = 0
// get equity-curve array using the back-tester object
eqCurve = equitycurve(bt, equity_curve_calc)
// create equity-curve plot
plot2 = plot(eqCurve, "EQ Curve", "line", "0000ff")
// display equity-curve chart
chart(plot2)


Simple Bollinger Band System, discrete trades with stops and no pyramiding backtester and equity curve plot. Custom start date for backtester report and equity curve.

Script

// Written by John Bollinger April 2014
// use the data from the chart
data("x")
// Bollinger Bands using built-in functions
middleBB = bbands(x, 20, 2, "middle")
lowerBB = bbands(x, 20, 2, "lower")
// back in the lower BBands = buy
entry = xover(close(x), lowerBB)
// tag the middle BBand = sell
exit = -xover(close(x), middleBB)
// group buy and sell signals in one array
signals = entry + exit
//ignore all dates older than 2013-06-01
d = greater(date(x),"2013-06-01")
//uncomment the line below to run backtester for dates between 2013-06-01 and 2014-01-01
//d = greater(date(x),"2013-06-01")* less(date(x),"2014-01-01")
//reset signals older than 2013-06-01
signals=if(d,signals,0)
// back test type 4 = discrete trades, use stops, no pyramiding
back_type = 4
// stop type = Chandelier
stop_type = 0
// run the back test
bt = backtest(x, signals, back_type, stop_type)
// prepare price chart with signals
plot1 = plot(close(x), "signals", "line", "00000000", bt)
// show chart with signals
pchart(plot1)
// calculate equity curve without compounding
equity_curve_calc = 0
// get equity-curve array using the back-tester object
eqCurve = equitycurve(bt, equity_curve_calc)
// create equity-curve plot
plot2 = plot(eqCurve, "EQ Curve", "line", "0000ff")
// display equity-curve chart
chart(plot2)


Ice Breaker signals system, discrete trades with Chandelier stops and pyramiding backtester and equity curve plot.

Script

// BBScript back test example using Ice Breaker signals.
// use the data from the chart
data("x")
// load data for signals
data("sig_data", "SPY")
// create Ice Breaker signals
// trade charted security with signals from another
ib = icebreaker(x, sig_data)
// back test discrete trades, multiple entries OK with stops
bt_mode = 5
// stop type for back test = Chandelier
bt_stop = 0
// create the back test signals/stops
bt = backtest(x, ib, bt_mode, bt_stop)
// create a backtest signal/stop plot with labels
plot1 = plot(close(x), "signals", "line", "00000000", bt)
// display signals and their labels in price chart
pchart(plot1)
// calculate equity curve, no compounding
equity_curve_calc = 0
// get equity curve array using backtester object created
eq = equitycurve(bt, equity_curve_calc)
// create equity curve plot
plot2 = plot(eq, "Equity Curve", "line", "0000ff")
// display equity curve chart
chart(plot2)
// end


Plotting Bollinger Bands® and Keltner channel on the price chart.

Script

// Copyright John Bollinger 2014
// Use the data from the chart
data("x")
// The typical price
typ = (high(x) + low(x) + close(x)) / 3
// Set the Bollinger Bands length and width
BBlen = 20
BBwidth = 2.0
// Set Keltner channel length and width
KClen = 15
KCwidth = 1.5
// Bollinger Bands
upperBB = bbands(x, BBlen, BBwidth, "upper")
lowerBB = bbands(x, BBlen, BBwidth, "lower")
// Keltner Channels
upperKC = sma(typ, KClen) + KCwidth * atr(x, KClen)
lowerKC = sma(typ, KClen) - KCwidth * atr(x, KClen)
// Create the objects to be plotted
// BBs with dark red lines
BBplot1 = plot(upperBB, "upper BB", "line", "CC0000")
BBplot2 = plot(lowerBB, "lower BB", "line", "CC0000")
// KCs with dark green lines
KCplot1 = plot(upperKC, "upper Keltner", "line", "009900")
KCplot2 = plot(lowerKC, "lower Keltner", "line", "009900")
// draw the bands and channels on the price chart
pchart(BBplot1, BBplot2, KCplot1, KCplot2)
// That's all folks!


Plotting simple Up-Down Oscillator.

Script

// Simple Up-Down Oscillator in BBScript
// Copyright John Bollinger 2014
// Use the data from the chart
data("x")
// Oscillator period
period = 21
// Direction of changes
sign = signum(close(x) - close(x)[-1])
// The oscillator
UDosc = movsum(sign, period) / period * 100
// Create the object to be plotted as a histogram
UDplot = plot(UDosc, "Up-Down Oscillator", "histogram")
// Plot the Up-Down Oscillator
chart(UDplot)
// That's all folks!


Stochastic RSI is the result of a marriage of two indicators, Stochastics and the Relative Strength Index. Interpretation is simpler and clearer than for RSI alone. The general rules are the same as for RSI, Stochastics or any other over-bought / over-sold index. Divergence analysis is particularity useful. Mathematically Stochastic RSI is an n-period Stochastic of an m-period RSI. The defaults for n and m are usually 14. Please see Normalized RSI for our version of this approach in which RSI is normalized with Bollinger Bands. Stochastic RSI was written by Tushar Chande.

Script

data("x")
rsiPer = 14
stochPer = 14
rawRSI = rsi(x, rsiPer)
k = (rawRSI - movmin(rawRSI, stochPer)) / (movmax(rawRSI, stochPer) - movmin(rawRSI, stochPer))
d = ema(k, 3)
kPlot = plot(k, "stochRSI %k", "line")
dPlot = plot(d, "stochRSI %d", "line", "0000FF")
highRef = plot(0.8, "overbought", "line", "FF0000")
lowRef = plot(0.2, "oversold", "line", "00FF00")
chart(kPlot, dPlot, highRef, lowRef)


Plotting Bollinger Bands® on chart using BBScript iterations.

Script

//manual bollinger bands
data("x") //get data object
period=20 //Bollinger Band period
width = 2 //Bollinger Band width
a=close(x) //a is the array of closing prices
middle=sma(a,period) //middle is the array of simple moving averages using period
std=array(0) //initialize the array of standard deviation, used to store the standard deviation values

i=0 //i is the iterator index
//populate the standard deviation array
iterate(length(a)-period+1) //repeat the block as many times as there are elements in the array minus the (period - 1)
sum = 0 //temporary sum variable initialized to zero to be used for the standard deviation function
j=i //j is iterator index for the nested loop, for current step, initialize to current value of i
iterate(period) //repeat nested loop period number of times, used to calculate the standard deviation
sum = sum +pow(middle[i+period-1]-a[j],2) //moving standard deviation formula
j=j+1 //increment the nested loop iterator index
end() //nested loop block ends here
std[i+period-1] = sqrt(sum/period) //update the current standard deviation value with the square root of the final sum of the current index divided by the period
i=i+1 //increment the main loop iterator index
end() //main loop block ends here

upper = middle+width*std //using the standard deviation and the middle band, calculate the upper band
lower = middle -width*std //using the standard deviation and the middle band, calculate the lower band

plotUpper = plot(upper,"upper","line","ff0000") //upper band plot line in red
plotLower = plot(lower,"lower","line","00ff00") //lower band plot line in green
plotMiddle = plot(middle,"middle","line","0000ff") //middle band plot line in blue
pchart(plotUpper, plotMiddle, plotLower) //display the calculated bands on the price chart


Plotting On Balance Volume using BBScript iterations.

Script

data("x") //get data object
c = close(x) //c is the array of closing prices
v = volume(x) //v is the array of volume values
len = length(c) //len is the number of elements in the arrays above
o = v //initialize the on balance volume to the same values as the volume array

i = 1 //i is the iterator index, it is initialize to 1 since for any point calculation, the previous value has to be used
iterate(len-1) //repeat the following block of statments (len - 1) times
//conditional block
startif( greater(c[i], c[i-1])) //if the current closing price is greater than the previous closing price
o[i] = o[i-1] + v[i] //set the current obv value to the previous value plus the current volume value
elseif(less(c[i], c[i-1])) //else if the current closing price is less than the previous closing price
o[i] = o[i-1] - v[i] //set the current obv value to the previous value minus the current volume value
else() //else if the current and previous closing prices are the same
o[i] = o[i-1] //set the current obv value to the previous value
endif() //end the conditional block
i = i+1 //increment the main loop iterator index
end() //main loop block ends here

o = o/movmax(o) //normalize the obv array by dividing all the elements in the array by the maximum value in the array

plotOBV = plot(o, "obv", "line","000000") //plot the on balance volume line in black
chart(plotOBV) //display the on balance volume line plot in an indicator chart


Plotting Klinger Volume Oscillator using BBScript iterations.

Script

# Klinger Volume Oscillator
# From Technical Analysis of Stocks and Commodities
# December 1997
# Coded by John Bollinger, January 2015

# get the data from the chart
data ("x")
cl = close(x)
hi = high(x)
lo = low(x)
vol = volume(x)
# create an array for the intermediate results
volForce = array(0)
# get the length of our data
len = length(cl)
# calculate the typical price
typ = (hi + lo + cl) / 3
# calculate the raw values for the oscillator
i = 1
iterate(len - 1)
# if typ is up volume is positive
startif( greater(typ[i], typ[i-1]) )
volForce[i] = vol[i]
# if typ is down volume is negative
elseif( less(typ[i], typ[i-1]) )
volForce[i] = -vol[i]
# if typ is unchanged volume doesn't count
else()
volForce[i] = volForce[i-1]
endif()
i = i+1
end()
# the oscillator is the difference of two exponential averages
KVO = ema(volForce, 34) - ema(volForce, 55)
# the signal line is an ema of the oscillator
KVO_Sig = ema(KVO, 13)
# create our plot objects
plot1 = plot(KVO, "Klinger Vol Osc", "histogram", "000000")
plot2 = plot(KVO_Sig, "Klinger Signal", "line", "0000ff")
# draw the oscillator in its own clip
chart(plot1, plot2)