Average True Range Normalized
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Based on ATR build-in indicator 

//@version=5
indicator(title='Average True Range Normalized', shorttitle='ATR Norm', overlay=false)

// ———————————————————————————————————————————————————————————————————
// >>>>>>>>>>>>>>>>>>>>>>>>>>   Input    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// ———————————————————————————————————————————————————————————————————

length      = input.int     (title='ATR Period ',                        defval=14, minval=1, group="Core", inline="ATR")
smoothing   = input.string  (title='Smoothing True Range',              defval='Classic ATR', group="Core", inline="ATR", options=['Classic ATR', 'SMA', 'EMA', 'WMA'], tooltip="Type of moving average to smooth out the True Range: \n"+"Classic ATR: in based on rma")


perNorma    = input.int     (title='Normalization Period      ',              defval=20, minval=1,  tooltip="Number of candles on which ATR normalization is based", group="Core")

perMA       = input.int     (title='MA Period',                         defval=50, minval=1, group="Core", inline="MA")
typeMA      = input.string  (title='  Type',                           defval='SMA', group="Core", options=[ 'SMA', 'EMA', 'WMA'], group="Core", inline="MA")


hl          = input.int     (title="Horizontal Lines Value      ",            defval=40, minval=0, group="Core")

alerthl     = input         (title="Alert on crossing Horizonal lines", defval=false, group="Alert")
alertMA     = input         (title="Alert on crossing MA",              defval=false, group="Alert")
// ———————————————————————————————————————————————————————————————————
// >>>>>>>>>>>>>>>>>>>>>>>>>>    Core    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// ———————————————————————————————————————————————————————————————————

ma_function(source, length, s) =>
	switch s
		"Classic ATR" => ta.rma(source, length)
		"SMA" => ta.sma(source, length)
		"EMA" => ta.ema(source, length)
		=> ta.wma(source, length)

atr     = ma_function(ta.tr(true), length, smoothing)
atrN    = ((atr - ta.lowest(atr, perNorma)) / (ta.highest(atr, perNorma) - ta.lowest(atr, perNorma)) - 0.5) * 100

ma      = ma_function(atrN, perMA, typeMA) 


// ———————————————————————————————————————————————————————————————————
// >>>>>>>>>>>>>>>>>>>>>>>>>>  Plotting  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// ———————————————————————————————————————————————————————————————————

plot(atrN, title='ATR', color=color.new(#B71C1C, 0))
plot(ma, title='ATR MA', color=color.new(color.yellow, 0), offset=0)
hline(hl, title="Upper Level", color=color.gray, linestyle=hline.style_dotted)
hline(-hl, title="Lower Level", color=color.gray, linestyle=hline.style_dotted) 

// ———————————————————————————————————————————————————————————————————
// >>>>>>>>>>>>>>>>>>>>>>>>>>   Alert    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// ———————————————————————————————————————————————————————————————————
if alerthl and ta.crossover(atrN,hl)
    alert("ATR is crossing up "+str.tostring(hl)+" level", alert.freq_once_per_bar_close)
if alerthl and ta.crossunder(atrN,-hl)
    alert("ATR is crossing down -"+str.tostring(hl)+" level", alert.freq_once_per_bar_close)
    
if alertMA and ta.crossover(atrN,ma)
    alert("ATR is crossing up Moving Average", alert.freq_once_per_bar_close)
if alertMA and ta.crossunder(atrN,ma)
    alert("ATR is crossing down Moving Average", alert.freq_once_per_bar_close)