RVM Relative Volume Momentum
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Mkas

//@version=4
study("Relative Volume Momentum")

var dayStarts = array.new_int(6, 0)
if (hour(time, "America/New_York") == 9 and minute(time, "America/New_York") == 30)
    array.unshift(dayStarts, bar_index)
    array.pop(dayStarts)

e = ema(volume, 5)

offsetToday = bar_index - array.get(dayStarts, 0)
idx1 = bar_index - (array.get(dayStarts, 1) + offsetToday)
idx2 = bar_index - (array.get(dayStarts, 2) + offsetToday)
idx3 = bar_index - (array.get(dayStarts, 3) + offsetToday)
idx4 = bar_index - (array.get(dayStarts, 4) + offsetToday)
idx5 = bar_index - (array.get(dayStarts, 5) + offsetToday)

max_bars_back(e, 5000)
a = avg(e[idx1], e[idx2], e[idx3])
relVol = e / a * 100

clr = color.new(color.gray, 80)
if (relVol > 100)
    clr := color.new(color.green, 80)
if (relVol > 200)
    clr := color.new(color.green, 60)
if (relVol > 300)
    clr := color.new(color.green, 40)
if (relVol > 400)
    clr := color.new(color.green, 20)
if (relVol > 500)
    clr := color.new(color.green, 0)

plot(relVol, color=clr, style=plot.style_area)
