Length = Param("Length", 14, 1, 100, 1);
Src = Param("Source", 1, 1, 5, 1);
Mult = Param("Mult", 2.0, 0.001, 50, 0.001);
MaLength = Param("MaLength", 7, 1, 100, 1);
Basis = MA(Src, Length);
Dev = Mult * ATR(Length);
Upper = Basis + Dev;
Lower = Basis - Dev;
BBR = (Src - Lower)/(Upper - Lower);
BBE = EMA(BBR, MaLength);
Up = IIf(Ref(BBE,-1)>BBE AND Ref(BBE,-2)<Ref(BBE,-1) , BBE , 0);
Bt = IIf(Ref(BBE,-1)<BBE AND Ref(BBE,-2)>Ref(BBE,-1) , BBE , 0);
TopH = HHV(H, 3);
BottomL = LLV(L, 3);
Tf = IIf(Up != 0 , TopH, Ref(TopH,-1));
Bf = IIf(Bt != 0 , BottomL , Ref(BottomL,-1));
Plot(Tf, "", colorRed, styleLine | styleThick);
Plot(Bf, "", colorGreen, styleLine | styleThick);
Buy = Cross(Close, Tf);
Sell = Cross(Bf, Close);
Link for original code: ATR_RSRL — Indicator by tongue1 — TradingView