Market Sessions [Kaspricci]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Kaspricci

//@version=5
indicator("Market Sessions [Kaspricci]", shorttitle="Sessions", precision=0)

grp_title   = "Defaul Session Times in GMT"

tooltip_lse = "London Stock Exchange opening hours from 7:00 - 15:30 GMT"
tooltip_nse = "New York Stock Exchange opening hours from 13:30 - 20:00 GMT"
tooltip_jse = "Tokyo Stock Exchange opening hours from 0:00 - 2:30 and 3:30 - 6:00 GMT"
tooltip_sse = "Sydney Stock Exchange opening hours from 23:00 - 05:00 GMT"

lse_session = "0700-1530:23456" // London Stock Exchange    -  7:00 to 15:30 GMT
nse_session = "1330-2000:23456" // New York Stock Exchange  - 13:30 to 20:00 GMT
tse_session = "0000-0600:23456" // Tokyo Stock Exchange  -  0:00 to  2:30, 3:30 to 6:00 GMT
sse_session = "2300-0500:23456" // Sydney Stock Exchange    - 23:00 to 05:00 GMT 

show_lse_ses    = input.bool(true, "", inline="LSE", group=grp_title)
timeframe_lse   = input.session(defval=lse_session, group=grp_title, title="London Stock Exchange ---", inline="LSE", tooltip=tooltip_lse)

show_nse_ses    = input.bool(true, "", inline="NSE", group=grp_title)
timeframe_nse   = input.session(defval=nse_session, group=grp_title, title="New York Stock Exchange -", inline="NSE", tooltip=tooltip_nse)

show_tse_ses    = input.bool(true, "", inline="JSE", group=grp_title)
timeframe_tse   = input.session(defval=tse_session, group=grp_title, title="Tokyo Stock Exchange -----", inline="JSE", tooltip=tooltip_jse)

show_sse_ses    = input.bool(true, "", inline="SSE", group=grp_title)
timeframe_sse   = input.session(defval=sse_session, group=grp_title, title="Sydney Stock Exchange ---", inline="SSE", tooltip=tooltip_sse)

lse_time        = time(timeframe.period, lse_session, "GMT")
nse_time        = time(timeframe.period, nse_session, "GMT")
tse_time        = time(timeframe.period, tse_session, "GMT")
sse_time        = time(timeframe.period, sse_session, "GMT")

// transparnet line as lower border to avoid chart rescaling
hline(0, editable=false, color=color.new(color.black, 100))

// start position for session bars
float plot_position = -0.5

// top and bottom start position for session lables 
float lbl_top_position = -0.2
float lbl_btm_position = -0.8


// London Session
var box lse_lable_box = na

if (show_lse_ses)
    lbl_top_position += 1
    lbl_btm_position += 1
    
    plot_position += 1

    box.delete(lse_lable_box)
    lse_lable_box := box.new(last_bar_index+1, lbl_top_position, last_bar_index + 20, lbl_btm_position, text="London", bgcolor=na, border_color=color.new(color.black, 100), text_halign=text.align_left, text_color=color.black)

plot(show_lse_ses and lse_time ? plot_position : na, style=plot.style_linebr, linewidth=10, title="London Session", color=color.new(color.green, 50))


// New York Session
var box nse_lable_box = na

if (show_nse_ses)
    lbl_top_position += 1
    lbl_btm_position += 1
    
    plot_position += 1

    box.delete(nse_lable_box)
    nse_lable_box := box.new(last_bar_index+1, lbl_top_position, last_bar_index + 20, lbl_btm_position, text="New York", bgcolor=na, border_color=color.new(color.black, 100), text_halign=text.align_left, text_color=color.black)

plot(show_nse_ses and nse_time ? plot_position : na, style=plot.style_linebr, linewidth=10, title="New York Session", color=color.new(color.blue, 50))


// Tokyo Session
var box tse_lable_box = na

if (show_tse_ses)
    lbl_top_position += 1
    lbl_btm_position += 1

    plot_position += 1

    box.delete(tse_lable_box)
    tse_lable_box := box.new(last_bar_index+1, lbl_top_position, last_bar_index + 20, lbl_btm_position, text="Tokyo", bgcolor=na, border_color=color.new(color.black, 100), text_halign=text.align_left, text_color=color.black)

plot(show_tse_ses and tse_time ? plot_position : na, style=plot.style_linebr, linewidth=10, title="Tokyo Session", color=color.new(color.red, 50))


// Sydney Session
var box sse_lable_box = na

if (show_sse_ses)
    lbl_top_position += 1
    lbl_btm_position += 1

    plot_position += 1
    
    box.delete(sse_lable_box)
    sse_lable_box := box.new(last_bar_index+1, lbl_top_position, last_bar_index + 20, lbl_btm_position, text="Sydney", bgcolor=na, border_color=color.new(color.black, 100), text_halign=text.align_left, text_color=color.black)

plot(show_sse_ses and sse_time ? plot_position : na, style=plot.style_linebr, linewidth=10, title="Sydney Session", color=color.new(color.orange, 50))


// transparent line as upper border to avoid chart rescaling
plot(lbl_top_position + 0.2, color=color.new(color.black, 100))

// last bar index line
var line currentbar = na
line.delete(currentbar[1])
currentbar := line.new(last_bar_index, 0, last_bar_index, lbl_top_position + 0.2, color=color.red, width=1)

