Indicator value not as expected, trying to determine the cause

I try to calculate a simple Average Daily Range based on daily H - L to use in intraday context.
I used the TimeFrameSet method and manually checked if the result were as expected which they were not.

Using a more granular and manual approach, I calculated the same average with TimeFrameGetPrice to see what values were used. Still calculation was not what I expected.

I suspect it is due to daily H and L values that are used when transitioning from one day to the other.

To illustrate the issue, here are illustrations.
I use 1mn ES data which I set intraday to show "day session only" (although the database holds the 24 hours data).

Here are my DB settings:


First, here is the daily chart with current average H-L range for latest 2 days of 34. This is the level I expect to find on intraday data:


Now Here is my intraday Chart with a Value of 50.75 for latest day (the 27 latest data is possibly a reason of the problem, see below).


And here is the code.
Notice how value of dailyHigh, dailyHigh1..... change at 16.19.59.
I expected they would only change on next day: 16.19.59 is still same day as previous value, and I suspect that it is because of this that my end result is not what expected.


And indeed, altough dailyHigh value is what I expected it to use, dailyHigh1 is not the high of day based on daily, but high of last bar of previous day, which is not Daily high.

First question, is why is there a brief change of values for various variables on last day bar? Is there a way to prevent this?
Second question is, is this the reason why my calculation is not what is expected?

First of all if you need help on code you should insert code via code tags but not via picture only.

As for difference in avg. daily range...

Your dailyRange variable of TimeFrameSet gets expanded via expandLast. Study manual for expand modes.

As for using TimeFrameGetPrice (BTW, default expand mode here is expandFirst).

Range1 = Ref(Range, -1);

That upper line calls previous intraday bar's value but not previous day's bar if you are on intraday time frame!

So it would rather have to be

Range1 = dailyHigh1 - dailyLow1;

Thank you for your answer, I'll post real code in next post.

Indeed the expand modes helped understand the issue.
I set the intraday database preference to the recommended "START time of interval" and it also resolved the issue I was reporting on the debug where it would briefly use the last intrabar for High/Low.
For some reason I had it set to "END" which made it more difficult to understand.

And thanks for pointing out the TimeFrameGetPrice issue, indeed, this was further corrupting my result.

By setting "START time of interval" and using your calculation, I was able to get correct result. I was able to remove the manual calculation and simply use the intended TimeFrameSet method.