Appearance
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
| Input | Type | Default |
|---|---|---|
| Length | int | 20 |
| Basis MA type | select | "SMA" |
| Source | source | "close" |
| StdDev | float | 2 |
| Basis color | color | "#2962FF" |
| Upper color | color | "#F23645" |
| Lower color | color | "#089981" |
| Background | color | "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" }) });