Skip to content

Standard Error Bands ​

Regression line with standard-error bands, smoothed.

Kind indicator · Drawn on the price pane

Try it ​

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

Inputs ​

InputTypeDefault
Lengthint21
Smoothingint3
Standard errorsfloat2
Sourcesource"close"
Middle colorcolor"#2962FF"
Upper colorcolor"#F23645"
Lower colorcolor"#089981"
Backgroundcolor"rgba(33,150,243,0.05)"

Source ​

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

js
indicator({ name: "Standard Error Bands", shortTitle: "SEB", overlay: true });
const length = input.int("Length", 21, { min: 3 }), smoothing = input.int("Smoothing", 3, { min: 1 }), mult = input.float("Standard errors", 2, { min: 0, step: 0.1 });
const src = input.source("Source", "close");
const se = ta.calc([src], () => {
  let sx = 0, sy = 0, sxx = 0, sxy = 0;
  for (let k = 0; k < length; k++) { const x = length - 1 - k, y = src.at(k); if (y !== y) return NaN; sx += x; sy += y; sxx += x * x; sxy += x * y; }
  const slope = (length * sxy - sx * sy) / (length * sxx - sx * sx), icpt = (sy - slope * sx) / length;
  let ss = 0;
  for (let k = 0; k < length; k++) { const r = src.at(k) - (icpt + slope * (length - 1 - k)); ss += r * r; }
  return math.sqrt(ss / (length - 2));
});
const lr = ta.linreg(src, length, 0);
const middle = ta.sma(lr, smoothing), upper = ta.sma(ta.calc([lr, se], (l, e) => l + mult * e), smoothing), lower = ta.sma(ta.calc([lr, se], (l, e) => l - mult * e), smoothing);
plot(middle, { title: "Middle", color: input.color("Middle 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