Hourly Midline
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sabricat

//@version=5
indicator("Hourly Midline", overlay=true, max_lines_count=500)

src = input.string('High & Low', options=['High & Low', 'Open & Close'], title='Source')
width = input.int(2, title="Line width")
col = input.color(color.blue, title="Line color")
dc = input.bool(false, title="Show 1H candle levels")

f(src) =>
    v_ = src == 'High & Low' ? ((high - low) / 2 + low) : (math.abs((close - open) / 2) + math.min(close, open))
    lh_ = src == 'High & Low' ? high : math.max(open, close)
    ll_ = src == 'High & Low' ? low : math.min(open, close)
    [v_, lh_, ll_]

[v, lh, ll] = request.security(syminfo.tickerid, '60', f(src))
nh = ta.change(hour)

var line li = na
var line lih = na
var line lil = na

if not na(li)
    li.set_y1(v)
    li.set_y2(v)
    li.set_x2(time)
    if dc
        lih.set_y1(lh)
        lih.set_y2(lh)
        lih.set_x2(time)
        lil.set_y1(ll)
        lil.set_y2(ll)
        lil.set_x2(time)

if na(li) or nh
    li := line.new(time, v, time, v, xloc=xloc.bar_time, width=width, color=col)
    if dc
        lih := line.new(time, lh, time, lh, xloc=xloc.bar_time, width=1, color=color.green, style=line.style_dotted)
        lil := line.new(time, ll, time, ll, xloc=xloc.bar_time, width=1, color=color.red, style=line.style_dotted)
        
