OPEN-SOURCE SCRIPT

Average ATR (%) — No Spikes

62
//version=5
indicator("Average ATR (%) — No Spikes", overlay=true)

///////////////////////////
// Settings
atrLen = input.int(14, title="ATR Length")
barsBack = input.int(150, title="Bars to Average")
priceRef = input.string("close", title="Reference Price", options=["close", "hl2", "open"])
level1 = input.float(1.0, title="Moderate Volatility Threshold (%)")
level2 = input.float(1.5, title="High Volatility Threshold (%)")
showLabel = input.bool(true, title="Show Value on Chart")

///////////////////////////
// ATR percentage calculation
refPrice = priceRef == "close" ? close : priceRef == "hl2" ? hl2 : open
atr = ta.atr(atrLen)
atrPct = atr / refPrice * 100

// Average ATR % over N bars
var float sum = 0.0
var int count = 0

sum := 0.0
count := 0
for i = 0 to barsBack - 1
sum += nz(atrPct)
count += 1
avgAtrPct = count > 0 ? sum / count : na

///////////////////////////
// Line color based on thresholds
lineColor = avgAtrPct > level2 ? color.red : avgAtrPct > level1 ? color.orange : color.green
plot(avgAtrPct, title="Average ATR (%)", color=lineColor, linewidth=2)

///////////////////////////
// Right-side label
var label infoLabel = na

if showLabel
txt = "Average ATR: " + str.tostring(avgAtrPct, "#.##") + " %"
if na(infoLabel)
infoLabel := label.new(bar_index, close, txt, style=label.style_label_right, size=size.normal, color=color.blue, textcolor=color.white)
else
label.set_xy(infoLabel, bar_index, close)
label.set_text(infoLabel, txt)
else
if not na(infoLabel)
label.delete(infoLabel)
infoLabel := na

Penafian

Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.