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

//@version=5
indicator("Market Sessions)", overlay = true, max_bars_back = 400, max_boxes_count = 300, max_labels_count = 300, max_lines_count = 54)
import PineCoders/Time/3

Yearnow = year(timenow)
Monthnow = month(timenow)
Daynow = dayofmonth(timenow)

_timezone = "UTC"

// Forex Sessions
Asia_start = timestamp(_timezone, Yearnow, Monthnow,Daynow - 1, 23, 00, 00) 
Asia_end   = timestamp(_timezone, Yearnow, Monthnow,Daynow, 8, 00, 00)
Euro_start = timestamp(_timezone, Yearnow, Monthnow, Daynow, 08, 00, 00)
Euro_end   = timestamp(_timezone, Yearnow, Monthnow, Daynow, 17, 00, 00)
USA_start  = timestamp(_timezone, Yearnow, Monthnow, Daynow, 13, 00, 00)
USA_end    = timestamp(_timezone, Yearnow, Monthnow, Daynow, 22, 00, 00)
// Stock Sessions
_Asia_start = timestamp(_timezone, Yearnow, Monthnow,Daynow, 00, 00, 00) 
_Asia_end   = timestamp(_timezone, Yearnow, Monthnow,Daynow, 8, 00, 00) 
_Euro_start = timestamp(_timezone, Yearnow, Monthnow,Daynow, 9, 00, 00) 
_Euro_end   = timestamp(_timezone, Yearnow, Monthnow,Daynow, 17, 30, 00) 
_USA_start  = timestamp(_timezone, Yearnow, Monthnow,Daynow, 15, 30, 00) 
_USA_end    = timestamp(_timezone, Yearnow, Monthnow,Daynow, 22, 00, 00) 

start_day = timestamp(_timezone, Yearnow, Monthnow,Daynow, 00, 00, 00) 
end_day = timestamp(_timezone, Yearnow, Monthnow,Daynow, 23, 59, 59) 

// Get Diffrent Sessions
Asia_sess  = time >= Asia_start and time < Asia_end
Euro_sess  = time >= Euro_start and time < Euro_end
USA_sess   = time >= USA_start and time < USA_end
EuUS_sess  = time >= USA_start and time < Euro_end
_Asia_sess = time >= _Asia_start and time < _Asia_end
_Euro_sess = time >= _Euro_start and time < _Euro_end
_USA_sess  = time >= _USA_start and time < _USA_end
UsAs_gap   = time >= USA_end and time < Asia_start
day_sess   = time >= start_day and time < end_day

t_mrktsess = "Market Session/Daily HiLO"
t_DLHiLo   = "Daily High/Low Inputs"
// Plot Inputs
p_sess = input.bool(true, "Sessions","","1",t_mrktsess)
p_bckgr = input.bool(true, "Background","","1",t_mrktsess)
bcktest = input.bool(false, "Shows Past", "Shows the Past Sessions for Backtest", "1", t_mrktsess) and day_sess == false
//
i_stok  = input.string("No Text", "Dashboard Position", ["No Text", "Text"], "", "4", t_mrktsess)
b_stok  = input.bool(true, "Stock Open Close","","4",t_mrktsess)
// Dashboard Inputs
i_dashpos = input.string("Top Right", "Dashboard Position", ["Bottom Right", "Bottom Left", "Top Right", "Top Left"], "", "2", t_mrktsess)
draw_testables = input.bool(true,"Dashboard","","2", t_mrktsess)

// Daily HiLo Inputs
i_textpos = input.string("Right", "Text Position", ["Right", "Middle", "Left"], "", "3", t_DLHiLo)
p_DHLO    = input.bool(true, "Last Day High/Low","","3",t_DLHiLo)
colhigh   = input.color(color.rgb(216, 216, 216), "Daily High", "", "3.1", t_DLHiLo)
collow    = input.color(color.rgb(216, 216, 216), "Daily Low", "", "3.1", t_DLHiLo)

Dhigh     = request.security(syminfo.tickerid, "D", high)
Dlow      = request.security(syminfo.tickerid, "D", low)




ASST_EUFX = "0000-0800:123456"
EUFX_EUST = "0800-0900:123456"
EUST_USFX = "0900-1300:123456"
USFX_USST = "1300-1530:123456"
USST_EUST = "1530-1730:123456"
EUST_USST = "1730-2200:123456"
USAS_gap  = "2200-2300:123456"

fun_session(session) =>
    sess_true = not na(time(timeframe.period, session, "UCT"))
    [sess_true]

[_ASST_EUFX] = fun_session(ASST_EUFX)
[_EUFX_EUST] = fun_session(EUFX_EUST)
[_EUST_USFX] = fun_session(EUST_USFX)
[_USFX_USST] = fun_session(USFX_USST)
[_USST_EUST] = fun_session(USST_EUST)
[_EUST_USST] = fun_session(EUST_USST)
[_USAS_gap]  = fun_session(USAS_gap)

// Stock Open
_open  = time == _Asia_start or time == _Euro_start or time == _USA_start
_close = time == _Asia_end or time == _Euro_end or time == _USA_end 

_20EMA  = input.int(20, "1 EMA", inline = "1")
_50EMA  = input.int(50, "2 EMA", inline = "2")
_200EMA = input.int(200, "3 EMA", inline = "3")

_20col  = input.color(color.rgb(150, 211, 135), "", inline = "1")
_50col  = input.color(color.rgb(221, 156, 119), "", inline = "2")
_200col = input.color(color.rgb(108, 201, 224), "", inline = "3")

b20EMA  = input.bool(true, "", inline = "1")
b50EMA  = input.bool(true, "", inline = "2")
b200EMA = input.bool(true, "", inline = "3")

_20_EMA  = ta.ema(close, _20EMA)
_50_EMA  = ta.ema(close, _50EMA)
_200_EMA = ta.ema(close, _200EMA)

frame_sess = timeframe.isminutes and timeframe.multiplier < 61 ? true : false
frame_sess1 = timeframe.isminutes and timeframe.multiplier < 59 ? true : false

daysinc = ta.barssince(day_sess[1] == false and day_sess)
asiasinc = ta.barssince(Asia_sess[1] == false and Asia_sess)

var label linetxt = na
var label linetxt2 = na

_opentrue = false
_closetrue = false

txtpos = switch i_textpos
    "Right"  => bar_index 
    "Middle" => bar_index - (asiasinc / 2)
    "Left"   => bar_index - asiasinc
s_stok = switch i_stok
    "Text"    => true
    "No Text" => false

if day_sess and barstate.islastconfirmedhistory and p_DHLO
    line.new(bar_index - asiasinc, Dhigh, bar_index + 1, Dhigh, color = colhigh)
    line.new(bar_index - asiasinc, Dlow, bar_index + 1, Dlow, color = collow)
    label.delete(linetxt)
    linetxt := label.new(txtpos, Dhigh, text = "Last Day High", textcolor = colhigh, style = label.style_none)
    label.delete(linetxt2)
    linetxt2 :=  label.new(txtpos, Dlow, text = "Last Day Low", textcolor = collow, style = label.style_none)

if _open and b_stok and frame_sess1 and p_sess
    line.new(bar_index, high, bar_index, low, extend = extend.both, color = color.new(#ffffff, 15), style = line.style_dotted
         ,   width = 1)
if _close and b_stok and frame_sess1 and p_sess
    line.new(bar_index, high, bar_index, low, extend = extend.both, color = color.new(#706f6f, 0), style = line.style_dotted
         ,   width = 1)

if bcktest
    if _ASST_EUFX[1] == false and _ASST_EUFX or _EUST_USFX[1] == false and _EUST_USFX or _USST_EUST[1] == false and _USST_EUST and frame_sess1
        line.new(bar_index, high, bar_index, low, extend = extend.both, color = color.new(#ffffff, 15), style = line.style_dotted
             ,   width = 1)
        _opentrue := true
    if _ASST_EUFX[1] and _ASST_EUFX == false or _USST_EUST[1] and _USST_EUST == false or _USAS_gap[1] == false and _USAS_gap and frame_sess1
        line.new(bar_index, high, bar_index, low, extend = extend.both, color = color.new(#706f6f, 0), style = line.style_dotted
             ,   width = 1)
        _closetrue := true

var int pl_hold = na
var int svpl_hold = na
if time == Asia_start
    pl_hold := 1
if not na(pl_hold)
    pl_hold := pl_hold + 1
if time == _Asia_start
    svpl_hold := pl_hold[1] / 2
    pl_hold := na    
plotshape(_USAS_gap)
plot(b20EMA ? _20_EMA : na, "1 EMA", _20col, display = display.pane)
plot(b50EMA ? _50_EMA : na, "2 EMA", _50col, display = display.pane)
plot(b200EMA ? _200_EMA : na, "3 EMA", _200col, display = display.pane)

plot(Dhigh, color=color.new(#bc3a3a, 0), display = display.status_line)
plot(Dlow, color=color.new(#5aa53f, 0), display = display.status_line)

d_sess = p_sess ? display.pane : display.data_window
plotshape(frame_sess ? time == Asia_start : na, "Sydney/Tokyo Sesssion", color = na,
          text = "Sydney/Tokyo" , location = location.bottom, textcolor = color.rgb(255, 255, 255, 24), display = d_sess)
plotshape(frame_sess ? time == Euro_start : na, "London Sesssion", color = na, 
          text = "London", location = location.bottom, textcolor = color.rgb(255, 255, 255, 24), display = d_sess)
plotshape(frame_sess ? time == USA_start : na, "NewYork Sesssion", color = na, 
          text = "NewYork", location = location.bottom, textcolor = color.rgb(255, 255, 255, 24), display = d_sess)

plotshape(frame_sess1 and s_stok ? time == _Asia_start : na, "Open Stock Asia", color = na, offset = svpl_hold, 
          text = "Open" , location = location.top, textcolor = color.rgb(255, 255, 255, 24), display = d_sess)
plotshape(frame_sess1 and s_stok ? time == _Euro_start : na,  "Open Stock Euro", color = na, offset = svpl_hold, 
          text = "Open", location = location.top, textcolor = color.rgb(255, 255, 255, 24), display = d_sess)
plotshape(frame_sess1 and s_stok ? time == _USA_start : na, "Open Stock USA", color = na, offset = svpl_hold, 
          text = "Open", location = location.top, textcolor = color.rgb(255, 255, 255, 24), display = d_sess)

plotshape(frame_sess1 and s_stok ? time == _Asia_end : na, "Close Stock Asia", color = na, offset = -svpl_hold, 
          text = "Close" , location = location.top, textcolor = color.rgb(255, 255, 255, 24), display = d_sess)
plotshape(frame_sess1 and s_stok ? time == _Euro_end : na,  "Close Stock Euro", color = na, offset = -svpl_hold, 
          text = "Close", location = location.top, textcolor = color.rgb(255, 255, 255, 24), display = d_sess)
plotshape(frame_sess1 and s_stok ? time == _USA_end : na, "Close Stock USA", color = na, offset = -svpl_hold, 
          text = "Close", location = location.top, textcolor = color.rgb(255, 255, 255, 24), display = d_sess)

plotshape(frame_sess ? time >= Asia_start and time < _Asia_start : na, "Asia Forex to Asia Stock", shape.square, location.bottom,
         color.rgb(156, 237, 35, 55), display = d_sess)
plotshape(frame_sess ? time >= _Asia_start and time < Euro_start : na, "Asia Stock to Euro Forex", shape.square, location.bottom, 
         color.rgb(244, 236, 0, 55), display = d_sess)
plotshape(frame_sess ? time >= Euro_start and time < _Euro_start : na, "Euro Forex To Euro Stock", shape.square, location.bottom,
         color.rgb(255, 191, 0, 55), display = d_sess)
plotshape(frame_sess ? time >= _Euro_start and time < USA_start : na, "Euro Stock to USA Forex", shape.square, location.bottom, 
         color.rgb(255, 140, 0, 55), display = d_sess)
plotshape(frame_sess ? time >= USA_start and time < _USA_start : na, "USA Forex to USA Stock", shape.square, location.bottom, 
         color.rgb(230, 87, 4, 55), display = d_sess)
plotshape(frame_sess ? time >= _USA_start and time < _Euro_end : na, "USA Forex to Euro Stock", shape.square, location.bottom,
         color.rgb(255, 0, 0, 55), display = d_sess)   
plotshape(frame_sess ? time >= _Euro_end and time < _USA_end : na, "Euro Stock to USA Stock", shape.square, location.bottom,
         color.rgb(236, 63, 5, 55), display = d_sess)

plotshape(Asia_sess, "Asia Session", display = display.data_window)
plotshape(Euro_sess, "Euro Session", display = display.data_window)
plotshape(USA_sess, "USA Session", display = display.data_window)

bgcolor(frame_sess and p_sess and _USAS_gap ? color.rgb(255, 255, 255, 95) : na)
bgcolor(frame_sess and p_sess and p_bckgr and Asia_sess ? color.rgb(255, 255, 255, 98) : na)
bgcolor(frame_sess and p_sess and p_bckgr and (Euro_sess or _Euro_sess) ? color.rgb(255, 255, 255, 97) : na)
bgcolor(frame_sess and p_sess and p_bckgr and USA_sess ? color.rgb(255, 255, 255, 98) : na)

plotshape(frame_sess1 and bcktest and _opentrue, "Open Stock Asia", color = na, offset = svpl_hold, 
          text = "Open" , location = location.top, textcolor = color.rgb(255, 255, 255, 24), display = d_sess)
plotshape(frame_sess1 and bcktest and _closetrue, "Close Stock Asia", color = na, offset = -svpl_hold, 
          text = "Close" , location = location.top, textcolor = color.rgb(255, 255, 255, 24), display = d_sess)


plotshape(bcktest ? _ASST_EUFX[1] == false and _ASST_EUFX : na, "Sydney/Tokyo Sesssion", color = na,
          text = "Sydney/Tokyo" , location = location.bottom, textcolor = color.rgb(255, 255, 255, 24), display = display.pane, show_last = 900)
plotshape(bcktest ? _EUFX_EUST[1] == false and _EUFX_EUST : na, "London Sesssion", color = na, 
          text = "London", location = location.bottom, textcolor = color.rgb(255, 255, 255, 24), display = display.pane, show_last = 900)
plotshape(bcktest ? _USST_EUST[1] == false and _USST_EUST : na, "NewYork Sesssion", color = na, 
          text = "NewYork", location = location.bottom, textcolor = color.rgb(255, 255, 255, 24), display = display.pane, show_last = 900)

plotshape(bcktest ? _ASST_EUFX : na, "Asia Stock to Euro Forex", shape.square, location.bottom, 
         color.rgb(244, 236, 0, 55), display = display.pane, show_last = 900)
plotshape(bcktest ?_EUFX_EUST : na, "Euro Forex To Euro Stock", shape.square, location.bottom,
         color.rgb(255, 191, 0, 55), display = display.pane, show_last = 900)
plotshape(bcktest ? _EUST_USFX : na, "Euro Stock to USA Forex", shape.square, location.bottom, 
         color.rgb(255, 140, 0, 55), display = display.pane, show_last = 900)
plotshape(bcktest ? _USFX_USST : na, "USA Forex to USA Stock", shape.square, location.bottom, 
         color.rgb(230, 87, 4, 55), display = display.pane, show_last = 900)
plotshape(bcktest ? _USST_EUST : na, "USA Forex to Euro Stock", shape.square, location.bottom,
         color.rgb(255, 0, 0, 55), display = display.pane, show_last = 900)
plotshape(bcktest ? _EUST_USST : na, "Euro Stock to USA Stock", shape.square, location.bottom,
         color.rgb(236, 63, 5, 55), display = display.pane, show_last = 900)

bgcolor(bcktest and _USAS_gap ? color.rgb(255, 255, 255, 95) : na)

fun_filltaCell(_table,_column,_row,_title,_value,_textpos,_bgcolor,_textcolor) => // Get Sell Function
    _celltext = _title + _value
    table.cell(_table,_column,_row,_celltext,text_halign=_textpos,bgcolor = _bgcolor,text_color = _textcolor)
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// prepare stats Tables
tablespos = switch i_dashpos
    "Bottom Right" => position.bottom_right
    "Bottom Left"  => position.bottom_left
    "Top Right"    => position.top_right
    "Top Left"     => position.top_left
    => position.top_right

var tradetable = table.new(tablespos,2,15,color.new(#166aaf, 100),color.new(#49464d, 100),4,color.new(#49464d, 100),4)
// Color Variables For Tables
// raw Stats Tables
if draw_testables
    if barstate.islastconfirmedhistory
        // Update Tables
        fun_filltaCell(tradetable,0,0, "Current Session","",text.align_center,color.new(#3497e8, 80),color.new(#f1f1f1, 0))
        fun_filltaCell(tradetable,1,0, Asia_sess ? "Sydney/Tokyo" : time >= Euro_start and time < USA_start ? "London" :
                      time >= USA_start and time < _Euro_end ? "London/NewYork" :
                      time >= _Euro_end and time < _USA_end ? "NewYork" : _USAS_gap ? "Low Volume Zone" : na, "",text.align_center,
                      Asia_sess or Euro_sess or USA_sess ? color.new(#20adc0, 80) : _USAS_gap ? color.rgb(195, 195, 195, 64) : 
                      color.new(#20adc0, 80) ,color.new(#f1f1f1, 0))

        fun_filltaCell(tradetable,0,1,"Next Session","",text.align_center,color.new(#3497e8, 80),color.new(#f1f1f1, 0))
        fun_filltaCell(tradetable,1,1, Asia_sess ? "London" : Euro_sess and not USA_sess ? "NewYork" : Euro_sess or _Euro_sess and USA_sess ? "Low Volume Zone" : 
                      USA_sess ? "Low Volume Zone" : _USAS_gap ? "Sydney/Tokyo" : na,"",text.align_center,color.new(#20adc0, 80),color.new(#f1f1f1, 0))

        fun_filltaCell(tradetable,0,2,"Session Start in","",text.align_center,color.new(#3497e8, 80),color.new(#f1f1f1, 0))
        fun_filltaCell(tradetable,1,2,  str.format_time(Asia_sess ? Euro_start - timenow : Euro_sess and not USA_sess ? USA_start - timenow :
                      Euro_sess and (USA_sess or _USA_sess) ? USA_end - timenow : _USAS_gap ? Asia_start - timenow : na,
                      "HH:mm ", _timezone),"",text.align_center,color.new(#20adc0, 80),color.new(#f1f1f1, 0))
        // fun_filltaCell(tradetable,1,3, str.format_time(week_sess, "dd:MM:yyyy", _timezone),"",text.align_center,color.new(#20adc0, 80),color.new(#f1f1f1, 0))
