RVI Relative Volume Intraday
// 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 Intraday", overlay=true)

yearNow   = year(timenow)
monthNow  = month(timenow)
dateNow   = dayofmonth(timenow)
hourNow   = hour(timenow)
minuteNow = minute(timenow)

timeinrange(res, sess) => not na(time(res, sess, "America/New_York")) ? 1 : 0

isNewbar(res) =>
    t = time(res)
    not na(t) and (na(t[1]) or t > t[1])

isToday() => year == yearNow and month == monthNow and dayofmonth == dateNow

cumVol = 0.0
cumVol := nz(cumVol[1])
if (timeinrange("1", "0930-1600"))
    if (hour < hourNow or (hour == hourNow and minute <= minuteNow))
        cumVol := cumVol + volume
    else
        cumVol := 0
else
    cumVol := 0

var cumVolD = array.new_float(6)

if (isNewbar("D"))
    array.unshift(cumVolD, cumVol)
    array.pop(cumVolD)
    bool(na)
else if (cumVol > 0)
    array.set(cumVolD, 0, cumVol)
    bool(na)

avgVol = array.avg(array.slice(cumVolD, 1, 6))
relVol = isToday() ? round(cumVol / avgVol * 100) : 0

if timeframe.isminutes and timeframe.period != "60"
    l = label.new(bar_index, na, style=label.style_label_left, size=size.small, tooltip="Volume compared to the same intraday period for the previous 5 days")
    label.set_xy(l, bar_index + 1, close)
    label.set_text(l, "RV: " + tostring(relVol) + "%")
    label.set_textcolor(l, color.black)
    label.delete(l[1])
    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)
        label.set_textcolor(l, color.white)
    if (relVol > 500)
        clr := color.new(color.green, 0)
        label.set_textcolor(l, color.white)
    label.set_color(l, clr)
