'Relative volume by time of day

Relative volume is the ratio of the current volume to the average volume during a prior number of bars.

Given that volume changes drastically by time of day, a better indicator aims to provide the ratio between the current volume and the average volume for the same time of day in prior days. Basically, how volume compares relative to the average volume at that given time of day.

I am trying to develop such a metric using the following logic, but it is not working and I wonder where I am going wrong.

Basically, I create a loop over a given number of days (i.e., N) looking for the number of bars since N number of days from the current time. Then I add the volume at those bars and divide them by the number of days, N.

For some reason, the barsince function gives me a zero, when checking when the time was equal to time minus N days.

Any idea how to declare that conditional inside the "barssince" function? Basically, how many bars are there between the current time and the current time minus say one day?

or is there an alternative logic to calculate the relative volume by same time of day?

//@version=4
study(title="Relative volume by time of day", shorttitle="RVTD", overlay=false, precision=2, resolution="")

Len = input(10, "Days to average")

ret_val = 0.0
for Dayi = 1 to Len
    PriorDayI = time - 86400000 * Dayi //86400000  is the number of miliseconds in a day
    Bars= barssince(time == PriorDayI )
    ret_val := ret_val + volume[Bars]

RVTD = ret_val / Len
plot (RVTD)


Solution 1:[1]

I'm about to code something similar. I want to get the average for each 15min bar. Trading view only goes back about a month on 15 min bars and that will do. I was thinking of using an array size 96 for each bar.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 unspecial