Draw Line For High Low Custom Range Interactive
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RozaniGhani-RG

//@version=5
indicator('Draw Line For High Low Custom Range Interactive', 'DLFHLCRI', true)

//#region ———————————————————— 0. Inputs
G0 = 'POINTS'
int stamp1 = timestamp('2023-04')
int stamp2 = timestamp('2023-05')
int time1  = input.time(stamp1, 'Point 1', group = G0, confirm = true, tooltip = 'Point 1 must BEFORE Point 2')
int time2  = input.time(stamp2, 'Point 2', group = G0, confirm = true, tooltip = 'Point 2 must AFTER Point 1')

G1 = 'SETUP'
T0 = 'Left : Color Up\nRight : Color Down'
T1 = 'Values:\nMin : 1\nMax : 4'
i_c_Up    = input.color(color.teal,      'Color', group = G1, inline = '1')
i_c_Dn    = input.color(color.red,            '', group = G1, inline = '1', tooltip = T0)
i_s_style = input.string(    'arrow', 'Line Style', group = G1, options = ['solid', 'dashed', 'dotted', 'arrow'])
i_i_width = input.int(             2, 'Line Width', group = G1, minval = 1, maxval = 4, tooltip = T1)
//#endregion

//#region ———————————————————— 1. Types
// @type        Used for point
// @field bar   Int value for bar time
// @field price Int value for price
type point
    int     bar = na
    float price = na

// @type        Used for setting
// @field  col1 Color value for col1
// @field  col2 Color value for col2
// @field style String value for line style
// @field width Int value for line width
type setting
    color    col1 = na
    color    col2 = na
    string  style = na
    int     width = na
//#endregion

//#region ———————————————————— 2. Customs Functions
// @function get price based on given time
// @param    t
// @returns  o, h, l, c
getPrice(int t = na)=>
    var float o = na, var float h = na, var float l = na, var float c = na
    if time[1] <= t and time >= t and na(c)
        o := open, h := high, l := low, c := close
    [o, h, l, c]

// @function compare High Low points to get correct trend
// @param    price1, price2
// @returns  o, h, l, c
compareHL(int t1 = na, int t2 = na) =>
    [O1, H1, L1, C1] = getPrice(t1)
    [O2, H2, L2, C2] = getPrice(t2)
    [price1, price2] = switch C1 < C2
        true  => [L1, H2] 
        false => [H1, L2]
    [price1, price2]

// @function draw line if point is correct, otherwise draw label
// @param    point1, point2, setup
// @returns  lineID, labelID
createDrawing(point point1 = na, point point2 = na, setting setup = na) =>
    col = switch point1.price < point2.price
        true  => setup.col1
        false => setup.col2
    var line   lineID = na,  lineID.delete()
    var label labelID = na, labelID.delete()
    if point1.bar < point2.bar
        lineID  := line.new(point1.bar, point1.price, point2.bar, point2.price, xloc.bar_time,
                             color = col, width = setup.width, style = setup.style)
    else
        labelID := label.new(point2.bar, point2.price, 'Please move this point\nafter blue point', xloc.bar_time,
                             style = label.style_label_right, color = color.red)    
    [lineID, labelID]

// @function select line style
// @param    value
// @returns  style
selectLineStyle(string value = na) =>
    style = switch value
        'solid'  => line.style_solid
        'dashed' => line.style_dashed
        'dotted' => line.style_dotted
        'arrow'  => line.style_arrow_right
    style
//#endregion

//#region ———————————————————— 3. Constructs
[price1, price2] = compareHL(time1, time2)
point1 = point.new(time1, price1)
point2 = point.new(time2, price2)
setup  = setting.new(i_c_Up, i_c_Dn, selectLineStyle(i_s_style), i_i_width)

//      // Point 1 on right side (False)                                // Point 1 on left side (Correct) 
bgcolor(time1 > time2 and time == time1 ? color.new(color.blue, 90) : time1 < time2 and time == time1 ? color.new(color.blue, 90) : na, 0, false)
bgcolor(time1 > time2 and time == time2 ? color.new(color.red,  90) : time1 < time2 and time == time2 ? color.new(color.blue, 90) : na, 0, false)

if barstate.islast
    [trendLine, wrongPoint2] = createDrawing(point1, point2, setup)
//#endregion