{ USER }
Pd1:=Input("Regression Periods",3,100,3);
Pd2:=Input("Standard Deviation Periods",3,100,7);
Pd3:=Input("Standard Deviations",1,5,2);
{ DEFINE }
AP:=(H+L+O+2*C)/5;                                { Price }
LR:=LinearReg(AP,Pd1);                            { Moving Regression line }
SD:=Stdev(LR,Pd2);                                { Standard Deviation }
{ LATCH }
x:=If(PREV=0,                                     { initial or reset }
LR,                                               { store }
If(LR>PREV+Pd3*ValueWhen(1,PREV=0,SD)             { over }
OR LR<PREV-Pd3*ValueWhen(1,PREV=0,SD),            { under }
0,                                                { reset }
PREV));                                           { keep }
{ BANDS }
Middle:=ValueWhen(1,Ref(x,-1)=0,x);               { Get LR value from point in time }
SD:=ValueWhen(1,Ref(x,-1)=0,SD);                  { Get SD value from point in time }
{ PLOT }
Middle+Pd3*SD;                                    { Top - Green }
Middle-Pd3*SD;                                    { Bottom - Red }
Middle;                                           { Blue }
LR;                                               { White }