Appearance
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
| Input | Type | Default |
|---|---|---|
| Length | int | 21 |
| Smoothing | int | 3 |
| Standard errors | float | 2 |
| Source | source | "close" |
| Middle color | color | "#2962FF" |
| Upper color | color | "#F23645" |
| Lower color | color | "#089981" |
| Background | color | "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" }) });