Paste this complete indicator into Pine Editor and add it to a chart with at least eight loaded bars. Fixture mode assigns the synthetic data to the first eight loaded bars and keeps their results in a table; later bars have no fixture value. Leave length at 3 to match the tables. Disable fixture mode to calculate on actual chart OHLC. The real-time bar stays unavailable until its closing update.
Original TR/current-EMA indicator. Default length 3; fixture alerts disabled.pinescript
//@version=6
indicator("Pineify Volatility Ratio: TR / current EMA", overlay = false, precision = 6)
bool fixture = input.bool(true, "Use eight-bar synthetic fixture")
int length = input.int(3, "EMA length", minval = 2, maxval = 500)
float threshold = input.float(1.5, "Strictly above threshold", minval = 0, step = 0.01)
var array<float> os = array.from(100.0, 100, 100, 106, 107, 107, 108, 118)
var array<float> hs = array.from(101.0, 101, 101, 108, 108, 110, 108.5, 120)
var array<float> ls = array.from(99.0, 99, 99, 106, 106, 105, 107.5, 118)
var array<float> cs = array.from(100.0, 100, 100, 107, 107, 108, 108, 119)
int i = bar_index
bool inFixture = i < array.size(hs)
float o = fixture ? (inFixture ? array.get(os, i) : na) : open
float h = fixture ? (inFixture ? array.get(hs, i) : na) : high
float l = fixture ? (inFixture ? array.get(ls, i) : na) : low
float c = fixture ? (inFixture ? array.get(cs, i) : na) : close
bool valid = not na(o) and not na(h) and not na(l) and not na(c) and h >= l and o >= l and o <= h and c >= l and c <= h
var float previousClose = na
var float baseline = na
var int count = 0
float tr = na
float ratio = na
bool selected = false
float alpha = 2.0 / (length + 1)
if barstate.isconfirmed
if valid
tr := na(previousClose) ? h - l : math.max(h - l, math.max(math.abs(h - previousClose), math.abs(l - previousClose)))
baseline := na(baseline) ? tr : alpha * tr + (1 - alpha) * baseline
count += 1
ratio := count >= length and baseline > 0 ? tr / baseline : na
selected := not na(ratio) and ratio > threshold
previousClose := c
else
previousClose := na
baseline := na
count := 0
plot(ratio, "TR / current EMA", color = color.blue, linewidth = 2, style = plot.style_linebr)
hline(1, "Baseline", color = color.gray)
hline(threshold, "Selected threshold", color = color.orange)
plotshape(selected, "Selected bar", shape.circle, location.top, color.orange, size = size.tiny)
plot(tr, "True range", display = display.data_window)
plot(baseline, "EMA denominator", display = display.data_window)
alertcondition(not fixture and selected, "Range ratio above threshold", "Confirmed volatility ratio is above the configured threshold")
var table results = table.new(position.top_right, 5, 9, border_width = 1)
if barstate.isfirst and fixture
table.cell(results, 0, 0, "Bar")
table.cell(results, 1, 0, "TR")
table.cell(results, 2, 0, "EMA")
table.cell(results, 3, 0, "Ratio")
table.cell(results, 4, 0, "Selected")
if fixture and inFixture and barstate.isconfirmed
table.cell(results, 0, i + 1, str.tostring(i + 1))
table.cell(results, 1, i + 1, str.tostring(tr, "#.######"))
table.cell(results, 2, i + 1, str.tostring(baseline, "#.######"))
table.cell(results, 3, i + 1, na(ratio) ? "na" : str.tostring(ratio, "#.######"))
table.cell(results, 4, i + 1, selected ? "yes" : "no")