4C ATR w/ Reference Line
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © FourC
//Simple script that adds a horizontal renference line for minimum ATR criteria.
//Updated version of 4C ATR that includes decimal increments in the ATR Reference Line input, and ATR slope color change.
//Apadted from original Tradingview ATR indicator by 4C.
//@version=5

indicator(title="4C ATR w/ Reference Line", shorttitle="4C ATR", overlay=false, timeframe="", timeframe_gaps=true)
source = input.source(title="Source", defval=close)
length = input.int(title="Length", defval=14, minval=1)
showrefline = input.bool(true, title = "Show Reference Line")
atrline = input.float(title="ATR Reference Line", defval=2, step=0.1)
atrslopesmoothing = input.int(title="ATR Slope Smoothing", defval=8, minval=1)
smoothing = input.string(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])

ma_function(source, length) =>
	switch smoothing
		"RMA" => ta.rma(source, length)
		"SMA" => ta.sma(source, length)
		"EMA" => ta.ema(source, length)
		"WMA" => ta.wma(source, length)

atravg = ta.sma(ma_function(ta.tr(true), length), atrslopesmoothing)

x = if atravg[0] > atravg[1]
    color.rgb(0, 255, 255)
else
    color.rgb(255, 0, 0)

plot(ma_function(ta.tr(true), length), title = "ATR", color=x, linewidth=2)

hline(showrefline ? atrline : na, 'ATR Ref', color.white, linestyle=hline.style_solid, linewidth=1)

