ICT Index Futures Session Lines
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © geneclash

//@version=5
indicator("ICT Index Futures Session Lines",overlay=true,max_lines_count=500)

//INPUTS
i_tz = input.string('America/New_York', title='Timezone: ', tooltip='e.g. \'America/New_York\', \'Asia/Tokyo\', \'GMT-4\', \'GMT+9\'...', group='Session')
sessionFiveRange = input.session(title='H/L/O Range: ', defval='0000-0830', inline='e', group='Session')

sessionOneRange = input.session(title='Open of Day: ', defval='0830-0831', inline='a', group='Session')
sessionTwoRange = input.session(title='NY Lunch: ', defval='1200-1300', inline='b', group='Session')
sessionThreeRange = input.session(title='Algorithm: ', defval='1330-1331', inline='c', group='Session')
sessionFourRange = input.session(title='End of Day: ', defval='1630-1631', inline='d', group='Session')

colorOne = input.color(color.new(color.black,65),title="Open of Day:",inline="s_1",group="Style")
lineStyleOne = input.string(title="-", defval=line.style_solid, options=[line.style_dotted, line.style_dashed, line.style_solid],inline="s_1",group="Style")

colorTwo = input.color(color.new(color.black,65),title="Ny Lunch:",inline="s_2",group="Style")
colorTwoFill = input.color(color.new(color.orange,90),title="-",inline="s_2",group="Style")
lineStyleTwo = input.string(title="-", defval=line.style_solid, options=[line.style_dotted, line.style_dashed, line.style_solid],inline="s_2",group="Style")

colorThree = input.color(color.new(color.purple,65),title="Algorithm:",inline="s_3",group="Style")
lineStyleThree = input.string(title="-", defval=line.style_solid, options=[line.style_dotted, line.style_dashed, line.style_solid],inline="s_3",group="Style")

colorFour = input.color(color.new(color.black,65),title="End of Day:",inline="s_4",group="Style")
lineStyleFour = input.string(title="-", defval=line.style_solid, options=[line.style_dotted, line.style_dashed, line.style_solid],inline="s_4",group="Style")

colorFiveHL = input.color(color.new(color.black,65),title="H/L:",inline="s_5",group="Style")
colorFiveOpen = input.color(color.new(color.orange,0),title="Open:",inline="s_5",group="Style")
activeFive = input.color(color.new(color.yellow,95),title="-",inline="s_5",group="Style")
passiveFive = input.color(color.new(color.gray,95),title="/",inline="s_5",group="Style")
lineStyleFive = input.string(title="-", defval=line.style_solid, options=[line.style_dotted, line.style_dashed, line.style_solid],inline="s_5",group="Style")

//Sessions
in_session_one = time(timeframe.period, sessionOneRange,i_tz)
sessionOneActive = in_session_one and timeframe.multiplier <= 30

in_session_two = time(timeframe.period, sessionTwoRange,i_tz)
sessionTwoActive = in_session_two and timeframe.multiplier <= 30

in_session_three = time(timeframe.period, sessionThreeRange,i_tz)
sessionThreeActive = in_session_three and timeframe.multiplier <= 30

in_session_four = time(timeframe.period, sessionFourRange,i_tz)
sessionFourActive = in_session_four and timeframe.multiplier <= 30

in_session_five = time(timeframe.period, sessionFiveRange,i_tz)
sessionFiveActive = in_session_five and timeframe.multiplier <= 30

f_resInMinutes() =>
    _resInMinutes = timeframe.multiplier * (
      timeframe.isseconds ? 1. / 60             :
      timeframe.isminutes ? 1.                  :
      timeframe.isdaily   ? 60. * 24            :
      timeframe.isweekly  ? 60. * 24 * 7        :
      timeframe.ismonthly ? 60. * 24 * 30.4375  : na)

barHour = hour(time,i_tz)
barMin = minute(time,i_tz)

checkLastBar(sessionRange)=>
    splitSessionOne = str.split(sessionRange,'')
    
    hour1 = array.get(splitSessionOne,5)
    hour2 = array.get(splitSessionOne,6)
    hourSessionOne = str.tonumber(hour1+hour2)
    
    min1 = array.get(splitSessionOne,7)
    min2 = array.get(splitSessionOne,8)
    minSessionOne = str.tonumber(min1+min2)
    
    if hourSessionOne == 0
        hourSessionOne := 24
    
    if minSessionOne == 0
        minSessionOne := 60
        hourSessionOne := hourSessionOne -1
    
    _islastBarSession = (barHour == hourSessionOne) and (barMin >= minSessionOne - f_resInMinutes())
    islastBarSession = _islastBarSession and _islastBarSession[1] == false
    
islastBarSessionFive = checkLastBar(sessionFiveRange)

var float highVal = na, var float lowVal = na, var float openVal = na
var line hLine = na, var line lLine = na, var line oLine = na
var line tempHLine = na, var line tempLLine = na
var linefill fill1 = na

if sessionFiveActive
    if sessionFiveActive[1] == false
        openVal := open
    if high >= nz(highVal, 0)
        highVal := high
    else
        highVal := highVal

    if low <= nz(lowVal, high)
        lowVal := low
    else
        lowVal := lowVal
else
    highVal := na
    lowVal := na
    openVal := na

startIndexH = ta.valuewhen(na(highVal[1]) and highVal, bar_index, 0)
startIndexL = ta.valuewhen(na(lowVal[1]) and lowVal, bar_index, 0)

//Plot H/L lines
if sessionFiveActive
    oLine := line.new(startIndexH, openVal, bar_index, openVal, color=colorFiveOpen, width=1, style=lineStyleFive)
    tempHLine := line.new(startIndexH, highVal, bar_index, highVal, color=colorFiveHL, width=1, style=lineStyleFive)
    tempLLine := line.new(startIndexL, lowVal, bar_index, lowVal, color=colorFiveHL, width=1, style=lineStyleFive)
    fill1 := linefill.new(tempHLine, tempLLine, activeFive)
    linefill.delete(fill1[1]) 
    line.delete(tempHLine[1]), line.delete(tempLLine[1])

if islastBarSessionFive
    hLine := line.new(startIndexH, highVal, bar_index, highVal, color=colorFiveHL, width=1, style=lineStyleFive)
    lLine := line.new(startIndexL, lowVal, bar_index, lowVal, color=colorFiveHL, width=1, style=lineStyleFive)
    linefill.new(hLine, lLine, passiveFive)

var line openLine = na
if sessionOneActive and sessionOneActive[1] == false
    openLine := line.new(bar_index,high,bar_index,low,extend=extend.both,color=colorOne, style=lineStyleOne)
    
var line lunchOpen = na, var line lunchClose = na
if sessionTwoActive and sessionTwoActive[1] == false
    lunchOpen := line.new(bar_index,high,bar_index,low,extend=extend.both,color=colorTwo , style=lineStyleTwo)

if sessionTwoActive==false and sessionTwoActive[1]
    lunchClose := line.new(bar_index,high,bar_index,low,extend=extend.both,color=colorTwo, style=lineStyleTwo)
    linefill.new(lunchOpen, lunchClose, colorTwoFill)

var line algoLine = na
if sessionThreeActive and sessionThreeActive[1] == false
    algoLine := line.new(bar_index,high,bar_index,low,extend=extend.both,color=colorThree, style=lineStyleThree)    
    
var line endLine = na
if sessionFourActive and sessionFourActive[1] == false
    endLine := line.new(bar_index,high,bar_index,low,extend=extend.both,color=colorFour, style=lineStyleFour) 