MILK (My Intraday Lazy Kit)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © noop42

//@version=5
indicator("My Intraday Lazy Kit", shorttitle="MILK", overlay=true)

// Options
hide_daily_levels = input.bool(true, "Hide daily levels above daily timeframe", group="Options")
show_clock = input.bool(true, "Show clock", group="Options")

// Levels
show_labels = input.bool(true, "Show labels", group="Options")

show_wk = input.bool(true, "Show weekly levels", group="Price Levels")
show_dl = input.bool(true, "Show daily levels", group="Price Levels")

daily_width = input.int(1, "Daily price level width", group="Price Levels")
weekly_width = input.int(3, "Weekly price level width", group="Price Levels")

open_col = input.color(color.aqua, "Previous open color", group="Price Levels")
close_col = input.color(color.blue, "Previous close color", group="Price Levels")
high_col = input.color(color.green, "Previous high color", group="Price Levels")
low_col = input.color(color.red, "Previous low color", group="Price Levels")

// Clock settings
inSession(sess) => na(time(timeframe.period, sess)) == false

in_session_color = input.color(#2da3f7, "Session Open", group="Clock")
out_of_session_color = input.color(#000540, "Session Open", group="Clock")

morning_color = input.color(#0046ad, "Morning color", group="Clock")
noon_color = input.color(#ad8d00, "Afternoon color", group="Clock")
evening_color = input.color(#9c4f03, "Evening color", group="Clock")
night_color = input.color(#00255c, "Night color", group="Clock")

city1_name = input.string("NY", title="City", group="City #1", group="Clock")
city1_delay = input.int(0, "Time delay", group="City #1", group="Clock")
city1_session = input.session("0800-1700", title="Session", group="City #1", group="Clock")

city2_name = input.string("London", title="City", group="City #2", group="Clock")
city2_delay = input.int(5, "Time delay", group="City #2", group="Clock")
city2_session = input.session("0300-1200", title="Session", group="City #2", group="Clock")

city3_name = input.string("Paris/Frankfurt", title="City", group="City #3", group="Clock")
city3_delay = input.int(6, "Time delay", group="City #3", group="Clock")
city3_session = input.session("0200-1100", title="Session", group="City #3", group="Clock")

city4_name = input.string("Tokyo", title="City", group="City #4", group="Clock")
city4_delay = input.int(14, "Time delay", group="City #4", group="Clock")
city4_session = input.session("1900-0400", title="Session", group="City #4", group="Clock")

city5_name = input.string("Sydney", title="City", group="City #5", group="Clock")
city5_delay = input.int(16, "Time delay", group="City #5", group="Clock")
city5_session = input.session("1700-0200", title="Session", group="City #5", group="Clock")

// Functions
get_time_color(h) =>
    h >= 0 and h < 6 ? night_color : h >= 6 and h < 12 ? morning_color : h >= 12 and h < 18 ? noon_color : evening_color

get_time_str(delay_h=0) =>
    h = (hour(timenow) + delay_h) % 24
    m = minute(timenow)
    [get_time_color(h), str.tostring(h, "00:")+str.tostring(m, "00")]

color_value(value, transp=0) =>
    step = (255/100.0)
    d = value * step
    x = 0.0
    y = 0.0
    z = 0.0
    x := value < 50 ? d : 127
    y := value < 50 ? 127: 255 - d
    color.rgb(math.round(x), math.round(y), math.round(z), transp)

ct1_sess_col = inSession(city1_session) ? in_session_color : out_of_session_color
ct2_sess_col = inSession(city2_session) ? in_session_color : out_of_session_color
ct3_sess_col = inSession(city3_session) ? in_session_color : out_of_session_color
ct4_sess_col = inSession(city4_session) ? in_session_color : out_of_session_color
ct5_sess_col = inSession(city5_session) ? in_session_color : out_of_session_color

[ct1_col, ct1_time] = get_time_str(city1_delay)
[ct2_col, ct2_time] = get_time_str(city2_delay)
[ct3_col, ct3_time] = get_time_str(city3_delay)
[ct4_col, ct4_time] = get_time_str(city4_delay)
[ct5_col, ct5_time] = get_time_str(city5_delay)

var tbl = table.new(position.top_right, 3, 10)
default_bg = #b8b8b8
dark_bg = #1f1f1f
invisible=#00000000

vix = request.security("CBOE:VIX", timeframe.period, close)

if show_clock
    table.cell(tbl, 0, 0, "🌍",  bgcolor = dark_bg, text_color=color.white)
    table.cell(tbl, 1, 0, "🕑", bgcolor = dark_bg, text_color=color.white)
    table.cell(tbl, 2, 0, "🏛️",  bgcolor = dark_bg, text_color=color.white)
    
    table.cell(tbl, 0, 1, city1_name,  bgcolor = dark_bg, text_color=color.white)
    table.cell(tbl, 1, 1, ct1_time, bgcolor=ct1_col, text_color=color.white)
    table.cell(tbl, 2, 1, "", bgcolor=ct1_sess_col, text_color=color.white)
    
    table.cell(tbl, 0, 2, city2_name,  bgcolor = dark_bg, text_color=color.white)
    table.cell(tbl, 1, 2, ct2_time, bgcolor=ct2_col, text_color=color.white)
    table.cell(tbl, 2, 2, "", bgcolor=ct2_sess_col, text_color=color.white)
    
    table.cell(tbl, 0, 3, city3_name,  bgcolor = dark_bg, text_color=color.white)
    table.cell(tbl, 1, 3, ct3_time, bgcolor=ct3_col, text_color=color.white)
    table.cell(tbl, 2, 3, "", bgcolor=ct3_sess_col, text_color=color.white)
    
    table.cell(tbl, 0, 4, city4_name,  bgcolor = dark_bg, text_color=color.white)
    table.cell(tbl, 1, 4, ct4_time, bgcolor=ct4_col, text_color=color.white)
    table.cell(tbl, 2, 4, "", bgcolor=ct4_sess_col, text_color=color.white)
    
    table.cell(tbl, 0, 5, city5_name,  bgcolor = dark_bg, text_color=color.white)
    table.cell(tbl, 1, 5, ct5_time, bgcolor=ct5_col, text_color=color.white)
    table.cell(tbl, 2, 5, "", bgcolor=ct5_sess_col, text_color=color.white)
    
    table.cell(tbl, 0, 6, "",  bgcolor = invisible, text_color=color.white)
    table.cell(tbl, 1, 6, "", bgcolor=invisible, text_color=color.white)
    table.cell(tbl, 2, 6, "", bgcolor=invisible, text_color=color.white)
    
    table.cell(tbl, 0, 7, "VIX",  bgcolor = dark_bg, text_color=color.white)
    table.cell(tbl, 1, 7, str.tostring(vix), bgcolor=color_value(vix, 30), text_color=color.white)

// levels
var line w_high = na
var line w_low = na
var line w_close = na
var line w_open = na

var line ytd_open = na
var line ytd_close = na
var line ytd_high = na
var line ytd_low = na

new_day = ta.change(time("D"))
new_week = ta.change(time("W"))

[y_o, y_h, y_l, y_c] = request.security(syminfo.tickerid, "D", [open, high, low, close])
[w_o, w_h, w_l, w_c] = request.security(syminfo.tickerid, "W", [open, high, low, close])
if show_wk
    if new_week
        w_high := line.new(bar_index[1], w_h, bar_index, w_h, color=high_col, width=weekly_width, style=line.style_dotted)
        w_low := line.new(bar_index[1], w_l, bar_index, w_l, color=low_col, width=weekly_width, style=line.style_dotted)
        w_close := line.new(bar_index[1], w_c, bar_index, w_c, color=close_col, width=weekly_width, style=line.style_dotted)
        w_open := line.new(bar_index[1], w_o, bar_index, w_o, color=open_col, width=weekly_width, style=line.style_dotted)
        if show_labels
            label.new(bar_index, w_h, text="PWH - "+str.tostring(w_h), style=label.style_none, textcolor=high_col, size=size.small)
            label.new(bar_index, w_l, text="PWL - "+str.tostring(w_l), style=label.style_none, textcolor=low_col, size=size.small)
            label.new(bar_index, w_c, text="PWC - "+str.tostring(w_c), style=label.style_none, textcolor=close_col, size=size.small)
            label.new(bar_index, w_o, text="PWO - "+str.tostring(w_o), style=label.style_none, textcolor=open_col, size=size.small)
        
    else
        line.set_x2(w_high, bar_index)
        line.set_x2(w_low, bar_index)
        line.set_x2(w_close, bar_index)
        line.set_x2(w_open, bar_index)

if show_dl and timeframe.isintraday
    if new_day
        ytd_high := line.new(bar_index[1], y_h, bar_index, y_h, color=high_col, width=daily_width)
        ytd_low := line.new(bar_index[1], y_l, bar_index, y_l, color=low_col, width=daily_width)
        ytd_open := line.new(bar_index[1], y_o, bar_index, y_o, color=open_col, width=daily_width)
        ytd_close := line.new(bar_index[1], y_c, bar_index, y_c, color=close_col, width=daily_width)
        if show_labels       
            label.new(bar_index, y_h, text="YTD-H - "+str.tostring(y_h), style=label.style_none, textcolor=high_col, size=size.small)
            label.new(bar_index, y_l, text="YTD-L - "+str.tostring(y_l), style=label.style_none, textcolor=low_col, size=size.small)
            label.new(bar_index, y_o, text="YTD-O - "+str.tostring(y_o), style=label.style_none, textcolor=open_col, size=size.small)
            
    else
        line.set_x2(ytd_high, bar_index)
        line.set_x2(ytd_low, bar_index)
        line.set_x2(ytd_open, bar_index)
        line.set_x2(ytd_close, bar_index)