First, I calculate values for a series on every bar and on the last bar I want to find out if specific values are in the calculated series. Unfortunately, it seems that "valuewhen" doesn't work well in combination with "if barstate.islast == true". The result of "valuewhen" is always NaN and I don't understand why. When I use "valuewhen" outside of the if-statement I get the expected result on every bar (but I only need it on the last bar). Any ideas?
var hh = close[0]
curh = highest(high, 15)
if curh > hh[1]
hh := curh
if barstate.islast == true
level = 1.31428 // a value which is definitely in the hh series
high1 = valuewhen(level==hh, hh, 1)
myLabel = label.new(x=bar_index, y=low - 0.5 * tr,
color=color.orange, textcolor=color.white,
style=label.style_labelup)
label.set_text(id=myLabel, text=tostring(high1)) // only shows NaN, although I expect to see 1.31428