Cumulative volume average at current time of day indicator

Hello,
this is the most complicated indicator I tackle. I know there were several threads about this but with no definitive answer.

My first step is to calculate cumulative volume for each day, which I did successfully with:

timeNow = TimeNum();
dateNow = DateNum();
firstBarTime = 093000; // Might use ETH AND RTH so better to determine new day with specific time.

firstBarOfDay = TimeNum() == firstBarTime;
barsSinceFirstBar = BarsSince( firstBarOfDay );
cumulativeVolumeToday = Sum( Ref( Volume, -1 ), barsSinceFirstBar );

Plot( cumulativeVolumeToday, "cumulativeVolumeToday", colorGreen, styleLine );
Plot( firstBarOfDay, "First bar", ColorRGB( 75, 75, 75 ), styleArea | styleOwnScale | styleNoLabel | styleNoTitle, 1, 0, 0, -1, 1 );

Now I need for each bar, to cumulate the value of cumulativeVolumeToday for same bar over X days ago and average it. So for example at 10:00 , I must get cumulativeVolumeToday for latest X 10:00 bars, sum them and average it.

I realize i'm not even capable of getting value of the indicator at say a fixed time like 100000 over X days ago.
Anyway, the answer to this question will give me direction to pursue:

Should I go for "for loop" to accomplish my goal, or can it be done with arrays?
Also do I need to use SparseCompress with the array method?

Got a flash of insight while insomnia at 4AM:

averageCumulativeVolume = MA( ValueWhen(timeNow == 110000, cumulativeVolumeToday), 2);

I wanted to test it over specific bar so I chose 11am:

example

On 110000 I get almost correct value (need to shift it to previous day): average of indicator over latest 2 occurrences (and not latest 2 bars) in this case.

Next step would be to make the system "dynamic", that is, instead of specifying a specifc time, have it calculate it for each bar.
Any guidance would be welcome.

Using ValueWhen will not lead to actual goal. It outputs your expected value in that case of period 2 (and at selected time 11:00) because you have just two bars to deal with... "current" bar and previous bar. But it won't output expected result if dealing with larger MA periods. You have to compress array, then do calculation(s) and then expand again.

Wait for next flash of insight while insomnia at 4AM tomorrow morning.

Hope the insight will come sooner... but I noticed they come when away form keyboard.

I should look into SparseCompress/SparseExpand right?

Progressing without insomnia....

onlyWhen = timeNow == 110000;
x = SparseCompress( onlyWhen, cumulativeVolumeToday );
y = Ref(MA( x, 2 ), -1);
y = SparseExpand( onlyWhen, y );

This is the array compress version which works with any length of MA and compute correct value for designated time: 110000.

Last step: figure how to have it calculated for all bars, not just 110000.
Thanks for your guidance.

Hi, bad insights today.
I think I hit a wall. I don't see how I can get this setup and feel the bitter taste of coding defeat.

onlyWhen = timeNow == 110000;
x = SparseCompress( onlyWhen, cumulativeVolumeToday );
y = Ref(MA( x, 2 ), -1);
y = SparseExpand( onlyWhen, y );

Is the best I can get. But It's for only one specified time, not all bars.
My experiments at trying to have it on all bars defeat the purpose of sparsecompress as it ads values from other bars preventing the MA calculation to calculate desired values.

I tried some loops, but the code was so ugly and slow that nobody wants to see it.
I would appreciate more guidance here as I'm sure what I try to achieve is possible.