Simple FX Market Hours
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © BongHuyn

//@version=5
indicator('Simple FX Market Hours', overlay=true)

color DEF_IN_SESS_COL = color.new(#00de94, 0)
color DEF_OUT_SESS_COL = color.black

var string tz_info = input.string(defval = "GMT+0", title = "Timezone", options=["GMT-12", "GMT-11", "GMT-10", "GMT-9", "GMT-8", "GMT-7", "GMT-6", "GMT-5", "GMT-4", "GMT-3", "GMT-2", "GMT-1", "GMT+0", "GMT+1", "GMT+2", "GMT+3", "GMT+4", "GMT+5", "GMT+6", "GMT+7", "GMT+8", "GMT+9", "GMT+10", "GMT+11", "GMT+12"])
var string table_position = input.string(defval = "Top Right", title = "Session Position", options = ["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"])
var color in_sess_col = input.color(DEF_IN_SESS_COL, "In Session")
var color out_sess_col = input.color(DEF_OUT_SESS_COL, "out Session")
var bool override_sess = input.bool(false, "Override", tooltip="Because this indicator doesn't support daylight saving yet! So this is a temporary way for you to override the session time.", group="Override")
var string sydney_sess_override = input.session("2200-0700", "Sydney Session", group="Override")
var string tokyo_sess_override = input.session("0000-0900", "Tokyo Session", group="Override")
var string london_sess_override = input.session("0800-1700", "London Session", group="Override")
var string newyork_sess_override = input.session("1300-2200", "Newyork Session", group="Override")
var int weekend_start_end_override = input.int(22, "Weekend", tooltip="Weekend Start & End. For example, for GMT+0 timezone the weekend start from 22:00 (10PM) on Friday and end at 22:00 (10PM) on Sunday", group="Override")

var string t_pos = switch table_position
    "Top Left" => position.top_left
    "Top Center" => position.top_center
    "Top Right" => position.top_right
    "Middle Left" => position.middle_left
    "Middle Center" => position.middle_center
    "Middle Right" => position.middle_right
    "Bottom Left" => position.bottom_left
    "Bottom Center" => position.bottom_center
    "Bottom Right" => position.bottom_right

var table ms_table = table.new(position = t_pos, columns = 4, rows = 1, bgcolor = color(na), frame_color = color(na), border_color = color.white, border_width = 1)
var bool is_sym_forex = syminfo.type == "forex"
if is_sym_forex
    table.cell(ms_table, 0, 0, "  SYDNEY  ", text_color = out_sess_col)
    table.cell(ms_table, 1, 0, "  TOKYO  ", text_color = out_sess_col)
    table.cell(ms_table, 2, 0, "  LONDON  ", text_color = out_sess_col)
    table.cell(ms_table, 3, 0, " NEW YORK ", text_color = out_sess_col)

var string SYDNEY_SESS_GMT_0 = "2200-0700"
var string TOKYO_SESS_GMT_0 = "0000-0900"
var string LONDON_SESS_GMT_0 = "0800-1700"
var string NEWYORK_SESS_GMT_0 = "1300-2200"
var int WEEKEND_GMT_0 = 22

var string SYDNEY_SESS_GMT_1 = "2300-0800"
var string TOKYO_SESS_GMT_1 = "0100-1000"
var string LONDON_SESS_GMT_1 = "0900-1800"
var string NEWYORK_SESS_GMT_1 = "1400-2300"

var string SYDNEY_SESS_GMT_2 = "0000-0900"
var string TOKYO_SESS_GMT_2 = "0200-1100"
var string LONDON_SESS_GMT_2 = "1000-1900"
var string NEWYORK_SESS_GMT_2 = "1500-0000"

var string SYDNEY_SESS_GMT_3 = "0100-1000"
var string TOKYO_SESS_GMT_3 = "0300-1200"
var string LONDON_SESS_GMT_3 = "1100-2000"
var string NEWYORK_SESS_GMT_3 = "1600-0100"

var string SYDNEY_SESS_GMT_4 = "0200-1100"
var string TOKYO_SESS_GMT_4 = "0400-1300"
var string LONDON_SESS_GMT_4 = "1200-2100"
var string NEWYORK_SESS_GMT_4 = "1700-0200"

var string SYDNEY_SESS_GMT_5 = "0300-1200"
var string TOKYO_SESS_GMT_5 = "0500-1400"
var string LONDON_SESS_GMT_5 = "1300-2200"
var string NEWYORK_SESS_GMT_5 = "1800-0300"

var string SYDNEY_SESS_GMT_6 = "0400-1300"
var string TOKYO_SESS_GMT_6 = "0600-1500"
var string LONDON_SESS_GMT_6 = "1400-2300"
var string NEWYORK_SESS_GMT_6 = "1900-0400"

var string SYDNEY_SESS_GMT_7 = "0500-1400"
var string TOKYO_SESS_GMT_7 = "0700-1600"
var string LONDON_SESS_GMT_7 = "1500-0000"
var string NEWYORK_SESS_GMT_7 = "2000-0500"

var string SYDNEY_SESS_GMT_8 = "0600-1500"
var string TOKYO_SESS_GMT_8 = "0800-1700"
var string LONDON_SESS_GMT_8 = "1600-0100"
var string NEWYORK_SESS_GMT_8 = "2100-0600"

var string SYDNEY_SESS_GMT_9 = "0700-1600"
var string TOKYO_SESS_GMT_9 = "0900-1800"
var string LONDON_SESS_GMT_9 = "1700-0200"
var string NEWYORK_SESS_GMT_9 = "2200-0700"

var string SYDNEY_SESS_GMT_10 = "0800-1700"
var string TOKYO_SESS_GMT_10 = "1000-1900"
var string LONDON_SESS_GMT_10 = "1800-0300"
var string NEWYORK_SESS_GMT_10 = "2300-0800"

var string SYDNEY_SESS_GMT_11 = "0900-1800"
var string TOKYO_SESS_GMT_11 = "1100-2000"
var string LONDON_SESS_GMT_11 = "1900-0400"
var string NEWYORK_SESS_GMT_11 = "0000-0900"

var string SYDNEY_SESS_GMT_12 = "1000-1900"
var string TOKYO_SESS_GMT_12 = "1200-2100"
var string LONDON_SESS_GMT_12 = "2000-0500"
var string NEWYORK_SESS_GMT_12 = "0100-1000"

var string SYDNEY_SESS_GMT_01 = "2100-0600"
var string TOKYO_SESS_GMT_01 = "2300-0800"
var string LONDON_SESS_GMT_01 = "0700-1600"
var string NEWYORK_SESS_GMT_01 = "1200-2100"

var string SYDNEY_SESS_GMT_02 = "2000-0500"
var string TOKYO_SESS_GMT_02 = "2200-0700"
var string LONDON_SESS_GMT_02 = "0600-1500"
var string NEWYORK_SESS_GMT_02 = "1100-2000"

var string SYDNEY_SESS_GMT_03 = "1900-0400"
var string TOKYO_SESS_GMT_03 = "2100-0600"
var string LONDON_SESS_GMT_03 = "0500-1400"
var string NEWYORK_SESS_GMT_03 = "1000-1900"

var string SYDNEY_SESS_GMT_04 = "1800-0300"
var string TOKYO_SESS_GMT_04 = "2000-0500"
var string LONDON_SESS_GMT_04 = "0400-1300"
var string NEWYORK_SESS_GMT_04 = "0900-1800"

var string SYDNEY_SESS_GMT_05 = "1700-0200"
var string TOKYO_SESS_GMT_05 = "1900-0400"
var string LONDON_SESS_GMT_05 = "0300-1200"
var string NEWYORK_SESS_GMT_05 = "0800-1700"

var string SYDNEY_SESS_GMT_06 = "1600-0100"
var string TOKYO_SESS_GMT_06 = "1800-0300"
var string LONDON_SESS_GMT_06 = "0200-1100"
var string NEWYORK_SESS_GMT_06 = "0700-1600"

var string SYDNEY_SESS_GMT_07 = "1500-0000"
var string TOKYO_SESS_GMT_07 = "1700-0200"
var string LONDON_SESS_GMT_07 = "0100-1000"
var string NEWYORK_SESS_GMT_07 = "0600-1500"

var string SYDNEY_SESS_GMT_08 = "1400-2300"
var string TOKYO_SESS_GMT_08 = "1600-0100"
var string LONDON_SESS_GMT_08 = "0000-0900"
var string NEWYORK_SESS_GMT_08 = "0500-1400"

var string SYDNEY_SESS_GMT_09 = "1300-2200"
var string TOKYO_SESS_GMT_09 = "1500-0000"
var string LONDON_SESS_GMT_09 = "2300-0800"
var string NEWYORK_SESS_GMT_09 = "0400-1300"

var string SYDNEY_SESS_GMT_010 = "1200-2100"
var string TOKYO_SESS_GMT_010 = "1400-2300"
var string LONDON_SESS_GMT_010 = "2200-0700"
var string NEWYORK_SESS_GMT_010 = "0300-1200"

var string SYDNEY_SESS_GMT_011 = "1100-2000"
var string TOKYO_SESS_GMT_011 = "1300-2200"
var string LONDON_SESS_GMT_011 = "2100-0600"
var string NEWYORK_SESS_GMT_011 = "0200-1100"

var string SYDNEY_SESS_GMT_012 = "1000-1900"
var string TOKYO_SESS_GMT_012 = "1200-2100"
var string LONDON_SESS_GMT_012 = "2000-0500"
var string NEWYORK_SESS_GMT_012 = "0100-1000"

var string sydney_sess = if override_sess 
    sydney_sess_override
else
    switch (tz_info)
        "GMT-12" => SYDNEY_SESS_GMT_012
        "GMT-11" => SYDNEY_SESS_GMT_011
        "GMT-10" => SYDNEY_SESS_GMT_010
        "GMT-9" => SYDNEY_SESS_GMT_09
        "GMT-8" => SYDNEY_SESS_GMT_08
        "GMT-7" => SYDNEY_SESS_GMT_07
        "GMT-6" => SYDNEY_SESS_GMT_06
        "GMT-5" => SYDNEY_SESS_GMT_05
        "GMT-4" => SYDNEY_SESS_GMT_04
        "GMT-3" => SYDNEY_SESS_GMT_03
        "GMT-2" => SYDNEY_SESS_GMT_02
        "GMT-1" => SYDNEY_SESS_GMT_01
        "GMT+0" => SYDNEY_SESS_GMT_0
        "GMT+1" => SYDNEY_SESS_GMT_1
        "GMT+2" => SYDNEY_SESS_GMT_2
        "GMT+3" => SYDNEY_SESS_GMT_3
        "GMT+4" => SYDNEY_SESS_GMT_4
        "GMT+5" => SYDNEY_SESS_GMT_5
        "GMT+6" => SYDNEY_SESS_GMT_6
        "GMT+7" => SYDNEY_SESS_GMT_7
        "GMT+8" => SYDNEY_SESS_GMT_8
        "GMT+9" => SYDNEY_SESS_GMT_9
        "GMT+10" => SYDNEY_SESS_GMT_10
        "GMT+11" => SYDNEY_SESS_GMT_11
        "GMT+12" => SYDNEY_SESS_GMT_12
        => ""

var string tokyo_sess = if override_sess
    tokyo_sess_override
else
    switch tz_info
        "GMT-12" => TOKYO_SESS_GMT_012
        "GMT-11" => TOKYO_SESS_GMT_011
        "GMT-10" => TOKYO_SESS_GMT_010
        "GMT-9" => TOKYO_SESS_GMT_09
        "GMT-8" => TOKYO_SESS_GMT_08
        "GMT-7" => TOKYO_SESS_GMT_07
        "GMT-6" => TOKYO_SESS_GMT_06
        "GMT-5" => TOKYO_SESS_GMT_05
        "GMT-4" => TOKYO_SESS_GMT_04
        "GMT-3" => TOKYO_SESS_GMT_03
        "GMT-2" => TOKYO_SESS_GMT_02
        "GMT-1" => TOKYO_SESS_GMT_01
        "GMT+0" => TOKYO_SESS_GMT_0
        "GMT+1" => TOKYO_SESS_GMT_1
        "GMT+2" => TOKYO_SESS_GMT_2
        "GMT+3" => TOKYO_SESS_GMT_3
        "GMT+4" => TOKYO_SESS_GMT_4
        "GMT+5" => TOKYO_SESS_GMT_5
        "GMT+6" => TOKYO_SESS_GMT_6
        "GMT+7" => TOKYO_SESS_GMT_7
        "GMT+8" => TOKYO_SESS_GMT_8
        "GMT+9" => TOKYO_SESS_GMT_9
        "GMT+10" => TOKYO_SESS_GMT_10
        "GMT+11" => TOKYO_SESS_GMT_11
        "GMT+12" => TOKYO_SESS_GMT_12
        => ""

var string london_sess = if override_sess
    london_sess_override
else
    switch tz_info
        "GMT-12" => LONDON_SESS_GMT_012
        "GMT-11" => LONDON_SESS_GMT_011
        "GMT-10" => LONDON_SESS_GMT_010
        "GMT-9" => LONDON_SESS_GMT_09
        "GMT-8" => LONDON_SESS_GMT_08
        "GMT-7" => LONDON_SESS_GMT_07
        "GMT-6" => LONDON_SESS_GMT_06
        "GMT-5" => LONDON_SESS_GMT_05
        "GMT-4" => LONDON_SESS_GMT_04
        "GMT-3" => LONDON_SESS_GMT_03
        "GMT-2" => LONDON_SESS_GMT_02
        "GMT-1" => LONDON_SESS_GMT_01
        "GMT+0" => LONDON_SESS_GMT_0
        "GMT+1" => LONDON_SESS_GMT_1
        "GMT+2" => LONDON_SESS_GMT_2
        "GMT+3" => LONDON_SESS_GMT_3
        "GMT+4" => LONDON_SESS_GMT_4
        "GMT+5" => LONDON_SESS_GMT_5
        "GMT+6" => LONDON_SESS_GMT_6
        "GMT+7" => LONDON_SESS_GMT_7
        "GMT+8" => LONDON_SESS_GMT_8
        "GMT+9" => LONDON_SESS_GMT_9
        "GMT+10" => LONDON_SESS_GMT_10
        "GMT+11" => LONDON_SESS_GMT_11
        "GMT+12" => LONDON_SESS_GMT_12
        => ""

var string newyork_sess = if override_sess
    newyork_sess_override
else
    switch tz_info
        "GMT-12" => NEWYORK_SESS_GMT_012
        "GMT-11" => NEWYORK_SESS_GMT_011
        "GMT-10" => NEWYORK_SESS_GMT_010
        "GMT-9" => NEWYORK_SESS_GMT_09
        "GMT-8" => NEWYORK_SESS_GMT_08
        "GMT-7" => NEWYORK_SESS_GMT_07
        "GMT-6" => NEWYORK_SESS_GMT_06
        "GMT-5" => NEWYORK_SESS_GMT_05
        "GMT-4" => NEWYORK_SESS_GMT_04
        "GMT-3" => NEWYORK_SESS_GMT_03
        "GMT-2" => NEWYORK_SESS_GMT_02
        "GMT-1" => NEWYORK_SESS_GMT_01
        "GMT+0" => NEWYORK_SESS_GMT_0
        "GMT+1" => NEWYORK_SESS_GMT_1
        "GMT+2" => NEWYORK_SESS_GMT_2
        "GMT+3" => NEWYORK_SESS_GMT_3
        "GMT+4" => NEWYORK_SESS_GMT_4
        "GMT+5" => NEWYORK_SESS_GMT_5
        "GMT+6" => NEWYORK_SESS_GMT_6
        "GMT+7" => NEWYORK_SESS_GMT_7
        "GMT+8" => NEWYORK_SESS_GMT_8
        "GMT+9" => NEWYORK_SESS_GMT_9
        "GMT+10" => NEWYORK_SESS_GMT_10
        "GMT+11" => NEWYORK_SESS_GMT_11
        "GMT+12" => NEWYORK_SESS_GMT_12
        => ""

int weekend_start_end = override_sess ? weekend_start_end_override : WEEKEND_GMT_0

f_time_in_range(tf, session, tz) => not na(time(tf, session, tz)) ? 1 : 0

bool is_sydney_sess = f_time_in_range(timeframe.period, sydney_sess, tz_info)
bool is_tokyo_sess = f_time_in_range(timeframe.period, tokyo_sess, tz_info)
bool is_london_sess = f_time_in_range(timeframe.period, london_sess, tz_info)
bool is_newyork_sess = f_time_in_range(timeframe.period, newyork_sess, tz_info)

bool is_weekend = (dayofweek(timenow, "GMT+0") == dayofweek.friday and hour(timenow, "GMT+0") >= weekend_start_end) or dayofweek(timenow, "GMT+0") == dayofweek.saturday or (dayofweek(timenow, "GMT+0") == dayofweek.sunday and hour(timenow, "GMT+0") < weekend_start_end)

if is_sym_forex
    if not is_weekend
        table.cell_set_text_color(ms_table, 0, 0, is_sydney_sess ? in_sess_col : out_sess_col)
        table.cell_set_text_color(ms_table, 1, 0, is_tokyo_sess ? in_sess_col : out_sess_col)
        table.cell_set_text_color(ms_table, 2, 0, is_london_sess ? in_sess_col : out_sess_col)
        table.cell_set_text_color(ms_table, 3, 0, is_newyork_sess ? in_sess_col : out_sess_col)
    else
        table.cell_set_text_color(ms_table, 0, 0, out_sess_col)
        table.cell_set_text_color(ms_table, 1, 0, out_sess_col)
        table.cell_set_text_color(ms_table, 2, 0, out_sess_col)
        table.cell_set_text_color(ms_table, 3, 0, out_sess_col)
        
        