[JL] n Bars Average True Range
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Jesse.Lau

//@version=5
indicator(title="[JL] n Bars Average True Range", shorttitle="nATR", overlay=false, timeframe="", timeframe_gaps=true)
length = input.int(title="nATR Length", defval=120, minval=1)
n = input.int(title="nBars", defval=12, 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)
		=> ta.wma(source, length)

n_true_range() =>
    math.max(ta.highest(high,n) - ta.lowest(low,n), math.max(math.abs(ta.highest(high,n) - close[n+1]), math.abs(ta.lowest(low,n) - close[n+1])))
plot(ma_function(n_true_range(), length), title = "nATR", color=color.new(#B71C1C, 0))