periode = Param("Periode",30,30,50000,1);
bar = BarIndex();
islastbar = bar == BarCount - 1;
isfirstbar = bar == BarCount - periode;
firstbar = LastValue( ValueWhen( isfirstbar, bar ) );
lastbar = LastValue( ValueWhen( islastbar, bar ) );
al = LastValue( ValueWhen( islastbar, LinRegSlope( C, lastbar - firstbar + 1) ) );
bl = LastValue( ValueWhen( islastbar, LinRegIntercept( C, lastbar - firstbar + 1 ) ) );
Lr = al * ( BarIndex() - firstbar ) + bl;
Lr = IIf( bar >= firstbar AND bar <= lastbar , Lr, Null );
se = StdErr(C,lastbar - firstbar);
se = LastValue( ValueWhen( islastbar, StdErr( C, lastbar - firstbar + 1 ) ));
upper = Lr + se;
Lower = Lr - se;
Plot(Lr, "lr", colorYellow, styleThick );
Plot(upper, "upper", colorGreen, styleThick );
Plot(Lower, "lower", colorRed, styleThick );
SetBarFillColor( IIf( C > O, colorBrightGreen, colorRed ) );
Plot(C, "C", colorWhite, styleCandle );