Skip to content

Bollinger Bands ​

SMA basis with standard-deviation bands.

Kind indicator · Drawn on the price pane

Try it ​

On the chart, press / and choose Bollinger Bands under Bands & channels. To change it, copy the source below into the AlphaScript editor.

Inputs ​

InputTypeDefault
Lengthint20
Basis MA typeselect"SMA"
Sourcesource"close"
StdDevfloat2
Basis colorcolor"#2962FF"
Upper colorcolor"#F23645"
Lower colorcolor"#089981"
Backgroundcolor"rgba(33,150,243,0.05)"

Source ​

From packages/alphascript/indicators/bbands.as.js.

js
indicator({ name: "Bollinger Bands", shortTitle: "BB", overlay: true });
const length = input.int("Length", 20, { min: 1 });
const maType = input.select("Basis MA type", "SMA", ["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"]);
const src = input.source("Source", "close");
const mult = input.float("StdDev", 2, { min: 0.001, max: 50, step: 0.1 });
const basis = maType === "EMA" ? ta.ema(src, length)
  : maType === "SMMA (RMA)" ? ta.rma(src, length)
  : maType === "WMA" ? ta.wma(src, length)
  : maType === "VWMA" ? ta.vwma(src, length)
  : ta.sma(src, length);
const dev = ta.stdev(src, length);
const upper = ta.calc([basis, dev], (b, d) => b + mult * d), lower = ta.calc([basis, dev], (b, d) => b - mult * d);
plot(basis, { title: "Basis", color: input.color("Basis color", "#2962FF", { group: "Style" }) });
plot(upper, { title: "Upper", color: input.color("Upper color", "#F23645", { group: "Style" }) });
plot(lower, { title: "Lower", color: input.color("Lower color", "#089981", { group: "Style" }) });
fill(upper, lower, { title: "Background", color: input.color("Background", "rgba(33,150,243,0.05)", { group: "Style" }) });

AlphaScript language v0.1 · runtime 0.2.0