I'm trying to use varip variables to capture intrabar data in real time. A simplified example would be capturing the high at the 2 minute point of a 5 minute bar. The simplified code below illustrates what works, and what doesn't.
//@version=5
indicator("My script", overlay=true)
varip float h0 = na
varip float h2 = na
varip float h5 = na
d = 8
hr = 0
min = 10
capture_time_0 = time == timestamp(year, month, d, hr, min + 0)
capture_time_2 = time == timestamp(year, month, d, hr, min + 2)
capture_time_5 = time == timestamp(year, month, d, hr, min + 5)
h0 := capture_time_0 ? high + 0 : h0
h2 := capture_time_2 ? high + 2 : h2
h5 := capture_time_5 ? high + 5 : h5
plot(h0, color=color.white, linewidth = 3)
plot(h2, color=color.fuchsia, linewidth = 3)
plot(h5, color=color.yellow, linewidth = 3)
plot(high, color=color.blue)
Varip variables h0
, h2
, h5
were initiated to na
. "Capture time" is defined as 0, 2 & 5 minutes after a particular starting point (Jul 8, 00:10 exchange time). When exchange time reaches each of the 3 designated capture times, h0
, h2
, h5
are re-assigned to the high
value that exists at that time. For graphical clarity in this example, 0
,2
,5
are added to the highs associated with h0
, h2
, h5
, respectively. As time passes and exchange time is no longer equal to capture time, h0
, h2
, h5
are maintained at the value that existed at capture time. All 3 values are plotted (h0=white
, h2=fuchsia
, h5=yellow
) as is the high
itself (blue line).
The top chart shows the results on a live 1 min chart. Everything works as expected. The h0=white
, h2=fuchsia
, h5=yellow
lines show that all 3 were captured at the designated times, assigned the correct values and maintained as constant values for the remainder of the chart. The problem arises in the bottom chart when one switches to the 5 minute time frame (or when viewing the 5 min chart in real-time). Everything works as above EXCEPT the h2=fuchsia
line is missing. In this case the missing h2
is the only value that is captured intrabar (2 minutes into the 5 minutes bar). Troubleshooting shows that this problem is repeatable on every time frame, e.g. you can capture values at 0 and 5 minutes on a 5 minute chart, but not at 1-4 minutes (intrabar). Similarly, you can capture values at the beginning an end of a 15 minute bar, but not at 1-14 minutes, etc.
Bottom line: although varip is designed to retain intrabar values, in this use case it is effectively not doing so. Any ideas how to get around this?