W. Wilder ATR VI
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © JakeTheDane


//@version=5
indicator(title="W. Wilder ATR VI", shorttitle="ATR VI", overlay=false)

// Initialize ATR
atrLength = input(7, "ATR Length")
var float atr = na
var float trSum = na

// Initialize VI
var float vi = na

// True Range Calculation
var float trueRange = na
diff1 = high - low
diff2 = close[1] - high
diff3 = close[1] - low

if (diff1 >= diff2 and diff1 >= diff3)
    trueRange := diff1
else if (diff2 >= diff1 and diff2 >= diff3)
    trueRange := diff2
else
    trueRange := diff3

// ATR Calculation
if (bar_index < atrLength)
    trSum := na(trSum) ? trueRange : trSum + trueRange
    atr := trSum / atrLength
else
    atr := ((atr * (atrLength - 1)) + trueRange) / atrLength

// VI Calculation
vi := na(vi) ? atr : (13 * vi + trueRange) / 14

// Plot the ATR and VI
plot(atr, title="ATR", color=color.blue)
plot(vi, title="VI", color=color.red)
