LNL Keltner Candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//
// @version=5
//
// LNL Keltner Candles with Reversals
//
// Mean reversion reversal candles with custom painted candles based on the price touch or close above or below keltner channel limits (upper & lower bands).
//
// Can serve as an additional indicator or as the mean reversion tool.
// 
// Created by L&L Capital
// 

indicator("LNL Keltner Candles", shorttitle = "Keltner Candles", overlay=true) 

// Inputs

length = input(defval=13, title="Length")
factor = input(defval=1.5, title="ATR Factor")
pricetypeUpper = input(high, title="Price Type Upper")
pricetypeLower = input(low, title="Price Type Lower")

// Keltner Channels Calculations

shift = factor * ta.atr(length)
average = ta.ema(close,length)
upperband = average + shift 
lowerband = average - shift
upperlimit = pricetypeUpper > upperband
lowerlimit = pricetypeLower < lowerband 

// Mean Reversion Arrows Calculations

UpBar = close > open
DnBar = close < open
AvgRange = 0.05 * math.avg(high - low, 20)

// Keltner Channels Plots & Colors

[middle, upper, lower] = ta.kc(close, length, factor)
plot(middle, color=color.new(#1848cc,0),title="Avg",display = display.none)
plot(upper, color=color.new(#1848cc,0),title="Upper Band",display = display.none)
plot(lower, color=color.new(#1848cc,0),title="Lower Band",display = display.none)

bar_color = upperlimit ? color.new(#ff0000,0) : lowerlimit ? color.new(#27c22e,0) : color.new(color.white,0)
barcolor(color=bar_color)

// Reversal Arrows Plots & Colors

SignalUp = DnBar[3] and DnBar[2] and DnBar[1] and UpBar[0] and close[3] >= close[2] and low[1] > open[0] and close[2] >= close[1] and low <= lowerband
plotshape(SignalUp,title='Reversal Up',style=shape.triangleup,location=location.belowbar,color=(color.purple),size=size.tiny)    

SignalDown = UpBar[3] and UpBar[2] and UpBar[1] and DnBar[0] and close[3] <= close[2] and low[1] < open[0] and close[2] <= close[1] and high >= upperband
plotshape(SignalDown,title='Reversal Down',style=shape.triangledown,location=location.abovebar,color=(color.purple),size=size.tiny)   