How to determine which 1H bar produced the low of the day?
I'm trying to calculate which bar (of the 24-hour bars in a day), produced the low for the day. This I want to calculate only for days that produced a green bar on 1D.
The code below practically waits until the first hour bar of the day (01h00), then checks whether yesterday was a green bar on 1D and then looks back 24-hour bars to determine which one of them produced the low for the day. Or that is what it's supposed to do. The results from this code are confusing and I cannot figure out what is wrong. Any advice would be highly appreciated, thanks.
//@version=5
indicator("Low of day bar")
endOfDay = 0100 //session end, in exchange local time, in 24hours format: 9:30AM=930, 4pm=1600
lastBarOfDay = (hour(time_close)*60 + minute(time_close)==(60*(endOfDay/100)+endOfDay%100))?1:0
dayIsGreen = request.security(syminfo.tickerid, 'D', close)[1] >= request.security(syminfo.tickerid, 'D', open)[1]
longSLBar = 0
if lastBarOfDay == 1 and dayIsGreen
longSLBar := ta.lowestbars(low, 24)[2] //without current bar
label.new(bar_index, low, text =str.tostring(longSLBar))
plot(longSLBar)
Found the problem. The code:
ta.lowestbars(low, 24)[1]
Needed to run every single iteration, so I moved it out of the if statement.
//@version=5
indicator("Low of day bar")
endOfDay = 0100 //session end, in exchange local time, in 24hours format: 9:30AM=930, 4pm=1600
lastBarOfDay = (hour(time_close)*60 + minute(time_close)==(60*(endOfDay/100)+endOfDay%100))?1:0
dayIsGreen = request.security(syminfo.tickerid, 'D', close)[1] >= request.security(syminfo.tickerid, 'D', open)[1]
var int longSLBar = na
last24bars = ta.lowestbars(low, 24)[1]
if lastBarOfDay == 1 and dayIsGreen
longSLBar := last24bars
label.new(bar_index, low, text =str.tostring(longSLBar))
plot(longSLBar)