Time Anchored Intraday High/Low Trendline
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © schroederjoa

// Oftentimes, intraday trendlines that are started at specific times, e.g. 8:00am or market open 9:30am, are well respected throughout the trading day. 
// This indicator draws up tp 3 intraday trendlines that are anchored at user defined times, respectively at the corresponding candle's high and low points. 
// From there, the line*s xy2 are connected in a way that all following candles are enclosed. 
 
//@version=5
indicator("Time Anchored Intraday High/Low Trendline", shorttitle="Intra Trend", overlay=true)

start_hour_01 = input.int(4, "Trendline 01   Start Hour  ", minval=-1, maxval=23, step=1, inline = "01", tooltip="Enter hour/minute to anchor trendline point x1. Use -1 to disable.")
line_width_01 = input.int(2, title="Line Width", inline = "01")
start_minute_01 = input.int(0, "              Start Minute", minval=-1, maxval=59, step=1, inline = "01_1")
line_color_01 = input.color(color.new(color.orange,40), title="Line Color ", inline = "01_1")

start_hour_02 = input.int(8, "Trendline 02   Start Hour  ", minval=-1, maxval=23, step=1, inline = "02")
line_width_02 = input.int(2, title="Line Width", inline = "02")
start_minute_02 = input.int(0, "              Start Minute", minval=-1, maxval=59, step=1, inline = "02_1")
line_color_02 = input.color(color.new(color.orange,40), title="Line Color ", inline = "02_1")

start_hour_03 = input.int(9, "Trendline 03   Start Hour  ", minval=-1, maxval=23, step=1, inline = "03")
line_width_03 = input.int(2, title="Line Width", inline = "03")
start_minute_03 = input.int(30, "              Start Minute", minval=-1, maxval=59, step=1, inline = "03_1")
line_color_03 = input.color(color.new(color.orange,40), title="Line Color ", inline = "03_1")

update_close = input.bool(true, "Await Candle Close before updating Trendline")
 
var line_high_01 = line.new(na,na,na,na, xloc=xloc.bar_index, style=line.style_solid, color=line_color_01, width=line_width_01, extend=extend.right)
var line_low_01 = line.new(na,na,na,na, xloc=xloc.bar_index, style=line.style_solid, color=line_color_01, width=line_width_01, extend=extend.right)

var line_high_02 = line.new(na,na,na,na, xloc=xloc.bar_index, style=line.style_solid, color=line_color_02, width=line_width_02, extend=extend.right)
var line_low_02 = line.new(na,na,na,na, xloc=xloc.bar_index, style=line.style_solid, color=line_color_02, width=line_width_02, extend=extend.right)

var line_high_03 = line.new(na,na,na,na, xloc=xloc.bar_index, style=line.style_solid, color=line_color_03, width=line_width_03, extend=extend.right)
var line_low_03 = line.new(na,na,na,na, xloc=xloc.bar_index, style=line.style_solid, color=line_color_03, width=line_width_03, extend=extend.right)

t_01 = (hour*100 + minute >= start_hour_01*100 + start_minute_01) and ((hour[1]*100 + minute[1] < start_hour_01*100 + start_minute_01) or (dayofmonth[1] != dayofmonth)) and start_hour_01 != -1 and start_minute_01 != -1
t_02 = (hour*100 + minute >= start_hour_02*100 + start_minute_02) and ((hour[1]*100 + minute[1] < start_hour_02*100 + start_minute_02) or (dayofmonth[1] != dayofmonth)) and start_hour_02 != -1 and start_minute_02 != -1
t_03 = (hour*100 + minute >= start_hour_03*100 + start_minute_03) and ((hour[1]*100 + minute[1] < start_hour_03*100 + start_minute_03) or (dayofmonth[1] != dayofmonth)) and start_hour_03 != -1 and start_minute_03 != -1

if t_01[1] and not t_01
    line.set_xy1(line_high_01, bar_index[1], high[1])
    line.set_xy2(line_high_01, bar_index, high)
    line.set_xy1(line_low_01, bar_index[1], low[1])
    line.set_xy2(line_low_01, bar_index, low)

if t_02[1] and not t_02
    line.set_xy1(line_high_02, bar_index[1], high[1])
    line.set_xy2(line_high_02, bar_index, high)
    line.set_xy1(line_low_02, bar_index[1], low[1])
    line.set_xy2(line_low_02, bar_index, low)

if t_03[1] and not t_03
    line.set_xy1(line_high_03, bar_index[1], high[1])
    line.set_xy2(line_high_03, bar_index, high)
    line.set_xy1(line_low_03, bar_index[1], low[1])
    line.set_xy2(line_low_03, bar_index, low)

update = update_close ? barstate.isconfirmed : true

if update    
    if line.get_price(line_high_01, bar_index) < high
        line.set_xy2(line_high_01, bar_index, high)
    if line.get_price(line_low_01, bar_index) > low
        line.set_xy2(line_low_01, bar_index, low)
        
    if line.get_price(line_high_02, bar_index) < high
        line.set_xy2(line_high_02, bar_index, high)
    if line.get_price(line_low_02, bar_index) > low
        line.set_xy2(line_low_02, bar_index, low)
        
    if line.get_price(line_high_03, bar_index) < high
        line.set_xy2(line_high_03, bar_index, high)
    if line.get_price(line_low_03, bar_index) > low
        line.set_xy2(line_low_03, bar_index, low)
    
    
