Files
Miha Kralj 35a6702b06 fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
Deep review of all indicator categories verified .md headers against .cs WarmupPeriod, parameters, inputs, and outputs. Fixes include warmup corrections, parameter documentation, output type accuracy, and Pine Script alignment.
2026-03-10 18:38:23 -07:00

39 lines
1.3 KiB
Plaintext

// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Normalized Average True Range", "NATR", overlay=false, format=format.percent, precision=2)
//@function Calculates the Normalized Average True Range (NATR)
//@param length The period length for the ATR calculation.
//@returns The NATR value as a percentage of close price.
//@optimized Beta precomputation for RMA warmup compensation
natr(simple int length) =>
float prevClose = nz(close[1], close)
float tr1 = high - low
float tr2 = math.abs(high - prevClose)
float tr3 = math.abs(low - prevClose)
float trueRange = math.max(tr1, math.max(tr2, tr3))
float alpha = 1.0 / float(length)
float beta = 1.0 - alpha
var float EPSILON = 1e-10
var float raw_rma = 0.0
var float e = 1.0
float atrValue = na
if not na(trueRange)
raw_rma := (raw_rma * (length - 1) + trueRange) / length
e *= beta
atrValue := e > EPSILON ? raw_rma / (1.0 - e) : raw_rma
float natrValue = close != 0 ? (atrValue / close) * 100 : 0
natrValue
// ---------- Main loop ----------
// Inputs
i_length = input.int(14, "Length", minval=1, tooltip="Number of bars used for the ATR calculation")
// Calculation
natrValue = natr(i_length)
// Plot
plot(natrValue, "NATR", color=color.yellow, linewidth=2)