#// This source code is subject to the terms of the Mozilla Public License 2.0 at https:/
#// © loxx
#indicator("ATR-Adaptive, Smoothed Laguerre RSI [Loxx]", shorttitle = "ATRASLRSI [Loxx]"
# Converted by Sam4Cok@Samer800 - 01/2024
Declare lower;
input Source = Close;
input SmoothingPeriod = 5;#, "Source Smoothing Period"
input atrLength = 15;#, "ATR Period"
input UpperBoundary = 85;#, "Upper Boundary"
input LowerBoundary = 15;#, "Lower Boundary"
input colorBars = no;#, "Color bars?"
def na = Double.NaN;
def last = isNaN(close);
Script lagfiltosc {
input src = close;
input per = 15;
def _gamma = 1.0 - 10.0 / (per + 9.0);
def L0 = CompoundValue(1, (1 - _gamma) * src + _gamma * L0[1], 0);
def L1 = CompoundValue(1, -_gamma * L0 + L0[1] + _gamma * L1[1], 0);
def L2 = CompoundValue(1, -_gamma * L1 + L1[1] + _gamma * L2[1], 0);
def L3 = CompoundValue(1, -_gamma * L2 + L2[1] + _gamma * L3[1], 0);
def CU1 = if (L0 >= L1) then L0 - L1 else 0;
def CD1 = if (L0 >= L1) then 0 else L1 - L0;
def CU2 = if (L1 >= L2) then CU1 + L1 - L2 else CU1;
def CD2 = if (L1 >= L2) then CD1 else CD1 + L2 - L1;
def CU = if (L2 >= L3) then CU2 + L2 - L3 else CU2;
def CD = if (L2 >= L3) then CD2 else CD2 + L3 - L2;
def out = if (CU + CD != 0) then CU / (CU + CD) else 0;
plot return = out;
}
def atr = atr(Length = atrLength);
def prevMax = fold j = 2 to atrLength + 1 with p =atr do
if p < atr[j] then atr[j] else p;
def prevMin = fold k = 2 to atrLength + 1 with q =atr do
if q > atr[k] then atr[k] else q;
def srcout = ExpAverage(Source, SmoothingPeriod);
def _max = if prevMax > atr then prevMax else atr;
def _min = if prevMin < atr then prevMin else atr;
def _coeff = if (_min != _max) then 1 - (atr - _min) / (_max - _min) else 0.5;
def val = lagfiltosc(srcout, atrLength * (_coeff + 0.75));
def valc = if (val > UpperBoundary/100) then 1 else
if (val < LowerBoundary/100) then 2 else 0;
plot plval = val * 100;
plval.SetLineWeight(2);
plval.AssignValueColor(if valc == 1 then Color.GREEN else
if valc == 2 then Color.RED else color.GRAY);
plot plup = if last then na else UpperBoundary;
plot pldn = if last then na else LowerBoundary;
plup.SetStyle(Curve.SHORT_DASH);
pldn.SetStyle(Curve.SHORT_DASH);
plup.SetDefaultColor(Color.GRAY);
pldn.SetDefaultColor(Color.GRAY);
#-- cloud & bar color
AddCloud(if valc == 1 then plval else na, plup, Color.GREEN);
AddCloud(if valc == 2 then pldn else na, plval, Color.RED);
AssignPriceColor(if !colorbars then Color.CURRENT else
if valc == 1 then Color.GREEN else
if valc == 2 then Color.RED else Color.GRAY);
#-- END of CODE