Day High_Low levels with Candle high low
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © convicted


//@version=4

study(title="High_Low levels", shorttitle="HL levels", overlay=true)

//Daily high and low
dailyhigh = security(syminfo.tickerid, 'D', high)
dailylow = security(syminfo.tickerid, 'D', low)

//Yesterday high and low
previousdayhigh = security(syminfo.tickerid, 'D', high[1])
previousdaylow = security(syminfo.tickerid, 'D', low[1])

//Premarket high and low
t = time("1440","0400-0930") // 1440 is the number of minutes in a whole day.
is_first = na(t[1]) and not na(t) or t[1] < t
ending_hour = 9
ending_minute = 30

pm_high = float(na)
pm_low = float(na)
k = int(na)

if is_first and barstate.isnew and ((hour < ending_hour or hour >= 16) or (hour == ending_hour and minute < ending_minute))
    pm_high := high
    pm_low := low
else 
    pm_high := pm_high[1]
    pm_low := pm_low[1]

if high > pm_high and ((hour < ending_hour or hour >= 1600) or (hour == ending_hour and minute < ending_minute))
    pm_high := high
    
if low < pm_low and ((hour < ending_hour or hour >= 1600) or (hour == ending_hour and minute < ending_minute))
    pm_low := low

LastOnly = true

if LastOnly==true
    k:=-9999
else
    k:=0
    
    //After Hours high and low
ti = time("1440","1600-2000") // 1440 is the number of minutes in a whole day.
is_first1 = na(ti[1]) and not na(ti) or ti[1] < ti
ending_hour1 = 20
ending_minute1 = 00

ah_high = float(na)
ah_low = float(na)
g = int(na)

if is_first1 and barstate.isnew and ((hour < ending_hour1 or hour >= 20) or (hour == ending_hour1 and minute < ending_minute1))
    ah_high := high
    ah_low := low
else 
    ah_high := ah_high[1]
    ah_low := ah_low[1]

if high > ah_high and ((hour < ending_hour1 or hour >= 2000) or (hour == ending_hour1 and minute < ending_minute1))
    ah_high := high
    
if low < ah_low and ((hour < ending_hour1 or hour >= 2000) or (hour == ending_hour1 and minute < ending_minute1))
    ah_low := low

LastOnly1 = true

if LastOnly1==true
    g:=-9999
else
    g:=0

//Just a variable here for the label coordinates
td = time - time[5]

//Daily high and low lines
plot(dailyhigh, style=plot.style_line,title="Daily high", color=#1b5e20, linewidth=2, trackprice=true,offset=k)
dh = label.new(x=time+td, y=dailyhigh, text = "Day High", xloc = xloc.bar_time, style = label.style_none, textcolor = #1b5e20, size = size.small, textalign = text.align_right)
label.delete(dh[1])

plot(dailylow, style=plot.style_line,title="Daily low",color=#b71c1c, linewidth=2, trackprice=true,offset=k)
dl = label.new(x=time+td, y=dailylow, text = "Day low", xloc = xloc.bar_time, style = label.style_none, textcolor = #b71c1c, size = size.small, textalign = text.align_right)
label.delete(dl[1])

//Previous day high and low lines
plot(previousdayhigh, style=plot.style_line,title="Yesterday's high",color=#a5d6a7, linewidth=2, trackprice=true,offset=k)
pdh = label.new(x=time+td, y=previousdayhigh, text = "Y H", xloc = xloc.bar_time, style = label.style_none, textcolor = #a5d6a7, size = size.small, textalign = text.align_right)
label.delete(pdh[1])

plot(previousdaylow, style=plot.style_line,title="Yesterday's low",color=#f8bbd0, linewidth=2, trackprice=true,offset=k)
pdl = label.new(x=time+td, y=previousdaylow, text = "Y L", xloc = xloc.bar_time, style = label.style_none, textcolor = #f8bbd0, size = size.small, textalign = text.align_right)
label.delete(pdl[1])

//Premarket high and low lines
plot(pm_high, style=plot.style_line,title="Premarket high", trackprice=true, color=#4caf50, linewidth=2,offset=k)
pmh = label.new(x=time+td, y=pm_high, text = "PM High", xloc = xloc.bar_time, style = label.style_none, textcolor = #4caf50, size = size.small, textalign = text.align_right)
label.delete(pmh[1])

plot(pm_low, style=plot.style_line,title="Premarket low", trackprice=true, color=#f44336, linewidth=2,offset=k)
pml = label.new(x=time+td, y=pm_low, text = "PM Low", xloc = xloc.bar_time, style = label.style_none, textcolor = #f44336, size = size.small, textalign = text.align_right)
label.delete(pml[1])

//AfterHours high and low lines
plot(ah_high, style=plot.style_line,title="After Hours high", trackprice=true, color=#679eca, linewidth=2,offset=k)
ahh = label.new(x=time+td, y=ah_high, text = "AH High", xloc = xloc.bar_time, style = label.style_none, textcolor = #679eca, size = size.small, textalign = text.align_right)
label.delete(ahh[1])

plot(ah_low, style=plot.style_line,title="After Hours low", trackprice=true, color=#679eca, linewidth=2,offset=k)
ahl = label.new(x=time+td, y=ah_low, text = "AH Low", xloc = xloc.bar_time, style = label.style_none, textcolor = #679eca, size = size.small, textalign = text.align_right)
label.delete(ahl[1])

patternLabelPosLow = low[1] - abs(atr(30) * 0.6)
patternLabelPosHigh = high[1] + abs(atr(30) * 0.6)

l1 = label.new(bar_index[1], patternLabelPosHigh, text=tostring(high[1]), style=label.style_label_down, color = color.green, textcolor=color.white)
l2 = label.new(bar_index[1], patternLabelPosLow, text=tostring(low[1]), style=label.style_label_up, color = color.red, textcolor=color.white)

label.delete(l1[1])
label.delete(l2[1])