# Standard Error of the Estimate -Composite Bands-

Standard Error of the Estimate - Code and adaptation by @glaz & @XeL_arjona
Ver. 2.00.a

Original implementation idea of bands by:
Traders issue: Stocks & Commodities V. 14:9 (375-379):
Standard Error Bands by Jon Andersen

This code is a former update to previous "Standard Error Bands" that was wrongly applied given that previous version in reality use the Standard Error OF THE MEAN, not THE ESTIMATE as it should be used by Jon Andersen original idea and corrected in this version.

As always I am very Thankfully with the support at the Pine Script Editor chat room, with special mention to user @glaz in order to help me adequate the alpha-beta (y-y') algorithm, as well to give him full credit to implement the "wide" version of the former bands.

For a quick and publicly open explanation of this truly statistical (regression analysis) indicator, you can refer at Here!

Extract from the former URL:
Standard Error Bands are quite different than Bollinger's. First, they are bands constructed around a linear regression curve. Second, the bands are based on two standard errors above and below this regression line. The error bands measure the standard error of the estimate around the linear regression line. Therefore, as a price series follows the course of the regression line the bands will narrow, showing little error in the estimate. As the market gets noisy and random, the error will be greater resulting in wider bands.

```//@version=2
study("Standard Error of the Estimate -Composite Bands-", shorttitle="SEE", overlay=true)
p = input(title="Rolling Lookback Window:", defval=21)
sdeg = input(title="Smoothing Factor:", defval=3)

// Standard Error of the Estimate Algorithm's
beta(array,per) =>
val1 = sum(n*array,per)-(per*sma(n,per)*sma(array,per))
val2 = sum(pow(n,2),per)-(per*pow(sma(n,per),2))
calcB = val1/val2
alpha(array,per) =>
calcA = sma(array,per)-(beta(array,per)*sma(n,per))
see(array,per,mult,dir,type) =>
lr = linreg(array,per,0)
val1 = (sum(pow(array,2),per))-((alpha(array,per)*sum(array,per)))-((beta(array,per)*sum(n*array,per)))
val2 = per - 2
narrow = sqrt(val1/val2)
est = sum(pow(lr-array,2),per) / (per - 2 )
wide = sqrt(est)
d = dir ? 1 : -1
band = type ? narrow : wide
seb = lr + d * mult * band

// Plotting
UWB = plot(sma(see(close,p,2,true,false),sdeg),color=red,transp=90)
UNB = plot(sma(see(close,p,2,true,true),sdeg),color=red,transp=90)
middle = plot(sma(linreg(close,p,0),sdeg),color=red,style=line,transp=0)
BNB = plot(sma(see(close,p,2,false,true),sdeg),color=red,transp=90)
BWB = plot(sma(see(close,p,2,false,false),sdeg),color=red,transp=90)
fill(UWB,BWB,transp=95,title="WSEE",color=red)
fill(UNB,BNB,transp=90,title="NSEE",color=red)```