PriceTimeInteractive
// 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

// @description TODO: Get price of given time.input
library('PriceTimeInteractive', true)

// 0. ohlc_time
// 1. hlc_time
// 2. hl_time
// 3. o_time
// 4. h_time
// 5. l_time
// 6. c_time
// 7. price_time

// Functions
// ————————————————————————————————————————————————————————————————————————————— 0. ohlc_time {
// @function : Get OHLC price of given time.input
// @param    : Time (t) must be using time.input
// @returns  : OHLC
export ohlc_time(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]
// }
// ————————————————————————————————————————————————————————————————————————————— 1. hlc_time {
// @function : Get HLC price of given time.input
// @param    : Time (t) must be using time.input
// @returns  : HLC
export hlc_time(int t = na)=>
    var float h = na
    var float l = na
    var float c = na
    if time[1] <= t and time >= t and na(c)
        h := high
        l := low
        c := close
    [h, l, c]
// }
// ————————————————————————————————————————————————————————————————————————————— 2. hl_time {
// @function : Get HL price of given time.input
// @param    : Time (t) must be using time.input
// @returns  : HL
export hl_time(int t = na)=>
    var float h = na
    var float l = na
    if time[1] <= t and time >= t and na(l)
        h := high
        l := low
    [h, l]
// }
// ————————————————————————————————————————————————————————————————————————————— 3. o_time {
// @function : Get Open price of given time
// @param    : Time (t)
// @returns  : Open
export o_time(int t = na)=>
    var float o = na
    if time[1] <= t and time >= t and na(o)
        o := open
    o
// }
// ————————————————————————————————————————————————————————————————————————————— 4. h_time {
// @function : Get High price of given time
// @param    : Time (t)
// @returns  : High
export h_time(int t = na)=>
    var float h = na
    if time[1] <= t and time >= t and na(h)
        h := high
    h
// }
// ————————————————————————————————————————————————————————————————————————————— 5. l_time {
// @function : Get Low price of given time
// @param    : Time (t)
// @returns  : Low
export l_time(int t = na)=>
    var float l = na
    if time[1] <= t and time >= t and na(l)
        l := low
    l
// }
// ————————————————————————————————————————————————————————————————————————————— 6. c_time {
// @function : Get Close price of given time
// @param    : Time (t)
// @returns  : Close
export c_time(int t = na)=>
    var float c = na
    if time[1] <= t and time >= t and na(c)
        c := close
    c
// }
// ————————————————————————————————————————————————————————————————————————————— 7. price_time {
// @function : Get any OHLC price of given time
// @param    : Time (t)
// @returns  : OHLC
export price_time(int a = 0, int t = na)=>
    float expr = switch a
        0 => open
        1 => high
        2 => low
        3 => close
    var float price = na
    if time[1] <= t and time >= t and na(price)
        price := expr
    price
// }

// Examples
// ————————————————————————————————————————————————————————————————————————————— 0. ohlc_time { 
int point_X = timestamp('2022-03')
int  time_X = input.time(point_X, 'Point X', confirm = true)
[open_X, high_X, low_X, close_X] = ohlc_time(time_X)
plot( open_X,  'open_X = ohlc_time(time_X)', trackprice = true)
plot( high_X,  'high_X = ohlc_time(time_X)', trackprice = true)
plot(  low_X,   'low_X = ohlc_time(time_X)', trackprice = true)
plot(close_X, 'close_X = ohlc_time(time_X)', trackprice = true)
label.new(time_X,  open_X,  'Open X', xloc.bar_time, style =label.style_label_right)
label.new(time_X,  high_X,  'High X', xloc.bar_time, style =label.style_label_lower_right)
label.new(time_X,   low_X,  'Low  X', xloc.bar_time, style =label.style_label_upper_right)
label.new(time_X, close_X, 'Close X', xloc.bar_time, style =label.style_label_right)
// }
// ————————————————————————————————————————————————————————————————————————————— 0. ohlc_time with _ { 
int point_Y = timestamp('2022-03')
int  time_Y = input.time(point_Y, 'Point Y', confirm = true)
[open_Y, _, _, close_Y] = ohlc_time(time_Y)
plot( open_Y,  'open_Y = ohlc_time(time_Y)', color.purple, trackprice = true)
plot(close_Y, 'close_Y = ohlc_time(time_Y)', color.purple, trackprice = true)
label.new(time_Y,  open_Y,  'Open Y', xloc.bar_time, color = color.purple, style =label.style_label_right)
label.new(time_Y, close_Y, 'Close Y', xloc.bar_time, color = color.purple, style =label.style_label_right)
// }
// ————————————————————————————————————————————————————————————————————————————— 1. hlc_time {
int point_A = timestamp('2022-04')
int  time_A = input.time(point_A, 'Point A', confirm = true)
[high_A, low_A, close_A] = hlc_time(time_A)
plot( high_A,  'high_A = hlc_time(time_A)', color.red, trackprice = true)
plot(  low_A,   'low_A = hlc_time(time_A)', color.red, trackprice = true)
plot(close_A, 'close_A = hlc_time(time_A)', color.red, trackprice = true)
label.new(time_A,  high_A,  'High A', xloc.bar_time, color = color.red, style =label.style_label_lower_right)
label.new(time_A,   low_A,  'Low  A', xloc.bar_time, color = color.red, style =label.style_label_upper_right)
label.new(time_A, close_A, 'Close A', xloc.bar_time, color = color.red, style =label.style_label_right)
// }
// ————————————————————————————————————————————————————————————————————————————— 2. hl_time {
int point_B = timestamp('2022-04')
int  time_B = input.time(point_B, 'Point B', confirm = true)
[high_B, low_B] = hl_time(time_B)
plot( high_B,  'high_B = hl_time(time_B)', color.green, trackprice = true)
plot(  low_B,   'low_B = hl_time(time_B)', color.green, trackprice = true)
label.new(time_B,  high_B,  'High B', xloc.bar_time, color = color.green, style =label.style_label_lower_right)
label.new(time_B,   low_B,  'Low  B', xloc.bar_time, color = color.green, style =label.style_label_upper_right)
// }
// ————————————————————————————————————————————————————————————————————————————— 6. c_time {
int point_C = timestamp('2022-04')
int  time_C = input.time(point_C, 'Point C', confirm = true)
close_C = c_time(time_C)
plot(close_C,  'close_C = c_time(time_C)', color.orange, trackprice = true)
label.new(time_C,  close_C,  'Close C', xloc.bar_time, color = color.orange, style =label.style_label_right)
// }
// ————————————————————————————————————————————————————————————————————————————— 7. price_time {
int point_O = timestamp('2022-04')
int  time_O = input.time(point_O, 'Point O', confirm = true)
open_O = price_time(0, time_O)
plot(open_O,  'open_C = price_time(0, time_O)', color.yellow, trackprice = true)
label.new(time_O,  open_O,  'Open O', xloc.bar_time, color = color.yellow, style =label.style_label_right)
// }