Daily Session Windows background highlight indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SacroMacro

//@version=5
indicator("Daily Session Windows", overlay=true)
same_as_instrument = "Same as instrument"
group = "Daily segments"
gmtSelect       = input.string(title="Reference time zone", defval=same_as_instrument, options=[same_as_instrument, "Pacific/Honolulu","America/Los_Angeles","America/Phoenix","America/Vancouver","America/El_Salvador","America/Bogota","America/Chicago","America/New_York","America/Toronto","America/Argentina/Buenos_Aires","America/Sao_Paulo" ,"Etc/UTC" ,"Europe/London" ,"Europe/Berlin" ,"Europe/Madrid" ,"Europe/Paris" ,"Europe/Warsaw" ,"Europe/Athens" ,"Europe/Moscow" ,"Asia/Tehran" ,"Asia/Dubai" ,"Asia/Ashkhabad" ,"Asia/Kolkata" ,"Asia/Almaty" ,"Asia/Bangkok" ,"Asia/Hong_Kong" ,"Asia/Shanghai" ,"Asia/Singapore" ,"Asia/Taipei" ,"Asia/Seoul" ,"Asia/Tokyo" ,"Australia/ACT" ,"Australia/Adelaide" ,"Australia/Brisbane" ,"Australia/Sydney" ,"Pacific/Auckland" ,"Pacific/Fakaofo" ,"Pacific/Chatham"], group=group, confirm=true)
premarketColor      = input.color(color.blue, "", group=group, confirm=true, inline="premarket")
premarketTime       = input.session('0830-0930',  title = "Premarket", group=group, confirm=true, inline="premarket")
coreColor           = input.color(color.lime, "", group=group, confirm=true, inline="core")
coreTime            = input.session('0930-1630',  title = "Core session", group=group, confirm=true, inline="core")
lunchColor          = input.color(color.yellow, "", group=group, confirm=true, inline="lunch")
lunchTime           = input.session('1200-1300',  title = "Lunch hour", group=group, confirm=true, inline="lunch")
extColor            = input.color(color.blue, "", group=group, confirm=true, inline="ext")
extTime             = input.session('1630-2000',  title = "Aftermarket", group=group, confirm=true, inline="ext")
offEnabled          = input.bool(defval=false, title="Market closed", tooltip="Enable to also highlight market-closed time", group=group, confirm=true, inline="closed")
offColor            = input.color(color.black, "", group=group, confirm=true, inline="closed")
transparency        = input.int(title="Trasparency (%)", defval=90, confirm=true, group=group, minval = 0, maxval = 100)

weekdays = "Week days"
on_mon = input.bool(defval=true, title="Monday", group=weekdays, confirm=true, inline="r1")
on_tue = input.bool(defval=true, title="Tuesday", group=weekdays, confirm=true, inline="r1")
on_wed = input.bool(defval=true, title="Wednesday", group=weekdays, confirm=true, inline="r1")
on_thu = input.bool(defval=true, title="Thursday", group=weekdays, confirm=true, inline="r2")
on_fri = input.bool(defval=true, title="Friday", group=weekdays, confirm=true, inline="r2")
on_sat = input.bool(defval=false, title="Saturday", group=weekdays, confirm=true, inline="r2")
on_sun = input.bool(defval=false, title="Sunday", group=weekdays, confirm=true)

session_weekdays = ':'
if on_sun
    session_weekdays := session_weekdays + "1"
if on_mon
    session_weekdays := session_weekdays + "2"
if on_tue
    session_weekdays := session_weekdays + "3"
if on_wed
    session_weekdays := session_weekdays + "4"
if on_thu
    session_weekdays := session_weekdays + "5"
if on_fri
    session_weekdays := session_weekdays + "6"
if on_sat
    session_weekdays := session_weekdays + "7"

if gmtSelect == same_as_instrument
    gmtSelect := syminfo.timezone

_premarketColor = color.new(premarketColor, transparency)
_coreColor = color.new(coreColor, transparency)
_lunchColor = color.new(lunchColor, transparency)
_extColor  = color.new(extColor, transparency)
_offColor  = color.new(offColor, 100)
if offEnabled
    _offColor := color.new(offColor, transparency)


theColor() => // return bgcolor for current timeframe
    _ret = _offColor
    if not na(time(timeframe.period, premarketTime+session_weekdays, gmtSelect))
        _ret := _premarketColor
    if not na(time(timeframe.period, extTime+session_weekdays, gmtSelect))
        _ret := _extColor
    if not na(time(timeframe.period, coreTime+session_weekdays, gmtSelect))
        _ret := _coreColor
    if not na(time(timeframe.period, lunchTime+session_weekdays, gmtSelect))
        _ret := _lunchColor
    _ret

bgcolor(color = theColor())
