Abraham Trend [Loxx]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © loxx

//@version=5
indicator("Abraham Trend [Loxx]", 
     shorttitle="AT [Loxx]", 
     overlay = true, 
     timeframe="", 
     timeframe_gaps = true)

greencolor = #2DD204
redcolor = #D2042D 

per = input.int(21, "Period", group = "Basic Settings") 
mult = input.int(3, "Multiplier", group = "Basic Settings") 

colorbars = input.bool(false, "Color bars?", group = "UI Options")

hi = ta.highest(close, per)
lo = ta.lowest(close, per)
tr = ta.wma(math.max(high, nz(close[1]))-math.min(low, nz(close[1])), per)

hiLimit = hi - tr * mult
loLimit = lo + tr * mult
 
linein = 0.
lineincl = 0.

linein := nz(linein[1])
lineincl := nz(lineincl[1]) 

linein := close > loLimit and close>hiLimit ?  hiLimit : linein
linein :=close < loLimit and close<hiLimit ? loLimit : linein

lineincl := close > linein ? 1 :lineincl
lineincl := close < linein ? 2 : lineincl

colorout = lineincl == 1 ? greencolor : lineincl == 2 ? redcolor : color.gray

barcolor(colorbars ? colorout : na)
plot(linein, "Abraham Trend", color = colorout, linewidth = 2)
