'Get an angle of two lines

I write a code that draws two the angle of the two lines I get the function for that , but I have a problem with the number of bars according to the condition on ( a1 = ta.valuewhen(time==time_s,bar_index,astart) and i tried to plot a new lable for that value but i think it give me the first bar index on chart

//@version=5
indicator("test",overlay=true)

astart = input(0,"last bar")
src = input(close,"Source")
ho = hour==04?hour:hour==00?hour:hour==08?hour:hour==12?hour:hour==16?hour:hour==20?hour:na

time_s = timestamp(year, month, dayofmonth, ho, 00, 00)
a1 = ta.valuewhen(time==time_s,bar_index,astart)
b1 = ta.valuewhen(time==time_s,close,astart)

line_color = b1 < src ? color.lime : color.red
upper = line.new(a1,b1,bar_index,b1, extend=extend.right,color=line_color,width=2)
lower = line.new(a1,b1,bar_index,src, extend=extend.right,color=line_color,width=2)
line.delete(upper[1])
line.delete(lower[1])


calcDegree(srcc, period) =>
    rad2degree = 180 / 3.14159265359
    ang = rad2degree * math.atan((srcc[0] - srcc[period]) / period)

angle = calcDegree(close, b1)

lab = label.new(bar_index, high*1.02, text=str.tostring(angle), color=color.green)
label.delete(lab[1])


Sources

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

Source: Stack Overflow

Solution Source