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

//@version=5
indicator("Sessions Indicator [CodeDNL]", overlay = true, max_lines_count = 500, max_boxes_count = 500)

//INPUTS
i_tz = input.string('America/New_York', title='Timezone:', tooltip='e.g. \'America/New_York\', \'Asia/Tokyo\', \'UTC-4\', \'UTC+9\'...', group='Sessions')  

sessionOneRange = input.session(title='Asia:', defval='1800-0030',inline="c_1" , group='Sessions')  
boxBgClr1 = input.color(color.new(color.blue,0),title="-",inline="c_1",group="Sessions")
textClr1 = input.color(color.black,title="-",inline="c_1",group="Sessions")

sessionTwoRange = input.session(title='London:', defval='0200-0600', inline="c_4", group='Sessions')
boxBgClr2 = input.color(color.new(color.orange,0),title="-",inline="c_4",group="Sessions")
textClr2 = input.color(color.black,title="-",inline="c_4",group="Sessions")

sessionThreeRange = input.session(title='New York:', defval='0700-1200', inline="c_8", group='Sessions')
boxBgClr3 = input.color(color.new(color.green,0),title="-",inline="c_8",group="Sessions")
textClr3 = input.color(color.black,title="-",inline="c_8",group="Sessions")

//CALCS
//ONE
in_session_one = time(timeframe.period, sessionOneRange,i_tz)
sessionOneActive = in_session_one and timeframe.multiplier <= 240 and timeframe.isdwm == false

//TWO
in_session_two = time(timeframe.period, sessionTwoRange,i_tz)
sessionTwoActive = in_session_two and timeframe.multiplier <= 240 and timeframe.isdwm == false

//THREE
in_session_three = time(timeframe.period, sessionThreeRange,i_tz)
sessionThreeActive = in_session_three and timeframe.multiplier <= 240 and timeframe.isdwm == false

//CALCS
var int sessionOneGap = na, var int sessionOneStartIndex = na, var int sessionOneEndIndex = na, var int sessionOneOffset = 0
var int sessionTwoGap = na, var int sessionTwoStartIndex = na, var int sessionTwoEndIndex = na, var int sessionTwoOffset = 0
var int sessionThreeGap = na, var int sessionThreeStartIndex = na, var int sessionThreeEndIndex = na, var int sessionThreeOffset = 0

//Set Session Gaps
if sessionOneActive and sessionOneActive[1] == false
    sessionOneStartIndex := bar_index

if sessionOneActive == false and sessionOneActive[1]
    sessionOneEndIndex := bar_index
    sessionOneGap := (sessionOneEndIndex - sessionOneStartIndex)
    sessionOneOffset := math.floor(sessionOneGap/2)

if sessionTwoActive and sessionTwoActive[1] == false
    sessionTwoStartIndex := bar_index

if sessionTwoActive == false and sessionTwoActive[1]
    sessionTwoEndIndex := bar_index
    sessionTwoGap := (sessionTwoEndIndex - sessionTwoStartIndex)
    sessionTwoOffset := math.floor(sessionTwoGap/2)

if sessionThreeActive and sessionThreeActive[1] == false
    sessionThreeStartIndex := bar_index

if sessionThreeActive == false and sessionThreeActive[1]
    sessionThreeEndIndex := bar_index
    sessionThreeGap := (sessionThreeEndIndex - sessionThreeStartIndex)
    sessionThreeOffset := math.floor(sessionThreeGap/2)

timeGap = math.abs(time - time_close)
off = (86400000/timeGap)*1

oneFirst = sessionOneActive and sessionOneActive[1] == false
oneLast = sessionOneActive == false and sessionOneActive[1]

plotshape(sessionOneActive, style = shape.circle, location = location.top, color=color.new(boxBgClr1,70), size = size.tiny, offset = off)
plotshape(sessionTwoActive, style = shape.circle, location = location.top, color=color.new(boxBgClr2,70), size = size.tiny, offset = off)
plotshape(sessionThreeActive, style = shape.circle, location = location.top, color=color.new(boxBgClr3,70), size = size.tiny, offset = off) 

plotshape(sessionOneActive and sessionOneActive[1] == false, text = "Asia", style=shape.circle, title = "Asia Session", location = location.top, textcolor= boxBgClr1, color = boxBgClr1, offset = sessionOneOffset, size = size.tiny)
plotshape(sessionTwoActive and sessionTwoActive[1] == false, text = "London", style=shape.circle, title = "London Session", location = location.top, textcolor= boxBgClr2, color = boxBgClr2, offset = sessionTwoOffset, size = size.tiny)
plotshape(sessionThreeActive and sessionThreeActive[1] == false, text = "New York", style=shape.circle, title = "NY Session", location = location.top, textcolor= boxBgClr3, color = boxBgClr3, offset = sessionThreeOffset, size = size.tiny)