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

//@version=5
indicator("ICT Index Schedule", overlay=true)

//––––. Functions
is_newbar(sess) =>
    t = time("D", sess, "America/New_York")
    na(t[1]) and not na(t) or t[1] < t

is_session(sess) =>
    not na(time(timeframe.period, sess, "America/New_York"))

is_tf = not timeframe.isweekly and not timeframe.ismonthly

isMon() =>
    dayofweek(time('1'), "America/New_York") == dayofweek.monday
isTue() =>
    dayofweek(time('1'), "America/New_York") == dayofweek.tuesday
isWed() =>
    dayofweek(time('1'), "America/New_York") == dayofweek.wednesday
isThu() =>
    dayofweek(time('1'), "America/New_York") == dayofweek.thursday 
isFri() =>
    dayofweek(time('1'), "America/New_York") == dayofweek.friday
isSat() =>
    dayofweek(time('1'), "America/New_York") == dayofweek.saturday 
isSun() =>
    dayofweek(time('1'), "America/New_York") == dayofweek.sunday


///////////////////
//––––. Settings //
///////////////////

////––. Key Levels / Times

showDays = input.bool(true, title="Show Days of The Week")
_kzMode = input.string("Line", title="Kill Zone Look", options=["Background", "Line"])
kzMode = _kzMode == "Background"
//Previous Day's Low / High
pdC = input.color(color.orange, inline='pd', title='', group='General')
showHL = input.bool(true, title="Previous Day Low/High", inline='pd', group='General')
nyHL = input.bool(false, title='NY Time?', inline='pd', group='General', tooltip='Use New York Time Daily Lows and Highs, from Midnight NY time instead of Daily Candle open time.')

//Midnight
mC = input.color(color.gray, title="", inline="mid", group='General')
midnightColor = color.new(mC, 75)
midnightON = input.bool(true, title='Midnight', inline="mid", group='General')
midnight = "0000-0001:1234567"
showMidPrice = input.bool(true, title='Open Price', inline="mid", group='General')

//Equities
eC = input.color(color.white, title="", inline="eq", group='General')
equitiesColor = color.new(eC, 85)
equitiesON = input.bool(true, title='Equities', inline='eq', group='General')
equities = "0930-0931:1234567"



////––. Sessions

//London
LC = input.color(color.purple, title="", inline="lonS", group='LONDON')
LondonColor = color.new(LC, 90)
londonON = input.bool(true, inline='lonS',title='Session', group='LONDON')
london = "0200-1100:1234567"

londonOpen = "0200-0201:1234567"
showLNOpenPrice = input.bool(true, title='Open', inline='lonS', group='LONDON')

londonKZON = input.bool(true,title='KZ',inline="lonS", group='LONDON')
londonKZ = "0200-0500:1234567"

//New York
NYC = input.color(color.green, title="", inline='nyS', group='NEW YORK')
NYColor = color.new(NYC, 90)
nyON = input.bool(true, inline='nyS',title="Session", group='NEW YORK')
ny = "0800-1700:1234567"
_ny = "0830-1700:1234567"

NYColorOpen = color.new(NYC, 70)
nyOpen = "0830-0831:1234567"
showNYOpenPrice = input.bool(true, title='Algo Open', inline='nyS', group='NEW YORK')

nyAMKZON = input.bool(true, inline='nyS',title='AM KZ', group='NEW YORK')
nyAMKZ = "0830-1100:1234567"

nyPMKZON = input.bool(true, inline='nyS',title='PM KZ', group='NEW YORK')
nyPMKZ = "1330-1600:1234567"

//Resetter
reset = "1700-0000:1234567"



////////////////
//––––. Logic //
////////////////

////––. isTime?
isReset = not is_session(reset)

//Midnight
isMidnight = is_newbar(midnight) and is_tf
bgcolor(isMidnight and midnightON ? midnightColor : na, 
         title="Midnight Line", 
         editable=false)

//Equisties
isEquities = is_newbar(equities) and is_tf
bgcolor(isEquities and equitiesON  ? equitiesColor : na, 
         title="Equities Open Line", 
         editable=false)

//London
isLondon = is_session(london)and is_tf
bgcolor(isLondon and londonON ? LondonColor : na, 
         title="London Session", 
         editable=false)

isLondonOpen = is_session(londonOpen)

isLondonKZ = is_session(londonKZ) and is_tf
bgcolor((kzMode and isLondonKZ and londonKZON) ? LondonColor : na, 
         title="London Kill Zone", 
         editable=false)
         
plotshape((not kzMode) and isLondonKZ and londonKZON,
         color=LC, 
         style=shape.square,
         location=location.bottom,
         size=size.auto,
         title="London Kill Zone", 
         editable=false)

//New York
isNewYork = is_session(ny)and is_tf
bgcolor(isNewYork and nyON ? NYColor : na, 
         title="New York Session", 
         editable=false)

_isNewYork = is_session(_ny)and is_tf //used to display algo open
isNewYorkOpen = is_session(nyOpen)

isNewYorkAM = is_session(nyAMKZ) and is_tf
bgcolor((kzMode and isNewYorkAM and nyAMKZON) ? NYColor : na, 
         title="New York AM Kill Zone", 
         editable=false)
         
plotshape((not kzMode) and isNewYorkAM and nyAMKZON,
         color=NYC, 
         style=shape.square,
         location=location.bottom,
         size=size.auto,
         title="New York AM Kill Zone", 
         editable=false)

isNewYorkPM = is_session(nyPMKZ) and is_tf
bgcolor((kzMode and isNewYorkPM and nyPMKZON) ? NYColor : na, 
         title="New York PM Kill Zone", 
         editable=false)
         
plotshape((not kzMode) and isNewYorkPM and nyPMKZON,
         color=NYC, 
         style=shape.square,
         location=location.bottom,
         size=size.auto,
         title="New York PM Kill Zone", 
         editable=false)



////––. Days of the Week
textColor = color.new(color.black, 30)

plotchar(isMidnight and isMon() and is_tf and showDays,
         'Mon', 'M', location.bottom, 
         textColor, 
         size=size.tiny,
         offset=0, 
         editable=false)

plotchar(isMidnight and isTue() and is_tf and showDays,
         'Tue', 'T', location.bottom, 
         textColor, 
         size=size.tiny, 
         offset=0, 
         editable=false)

plotchar(isMidnight and isWed() and is_tf and showDays,
         'Wed', 'W', location.bottom, 
         textColor, 
         size=size.tiny,
         offset=0, 
         editable=false)

plotchar(isMidnight and isThu() and is_tf and showDays,
         'Thu', 'T', location.bottom, 
         textColor, 
         size=size.tiny,
         offset=0, 
         editable=false)

plotchar(isMidnight and isFri() and is_tf and showDays,
         'Fri', 'F', location.bottom, 
         textColor, 
         size=size.tiny,
         offset=0, 
         editable=false)

plotchar(isMidnight and isSat() and is_tf and showDays,
         'Sat', 'S', location.bottom, 
         textColor, 
         size=size.tiny,
         offset=0, 
         editable=false)

plotchar(isMidnight and isSun() and is_tf and showDays,
         'Sun', 'S', location.bottom, 
         textColor, 
         size=size.tiny,
         offset=0, 
         editable=false)



////––. Price Levels
var float prevDayLow = na
var float prevDayHigh = na
var float _nyLow = na
var float _nyHigh = na
_nyLow := isMidnight[1] ? low : (low < _nyLow ? low : _nyLow)
_nyHigh := isMidnight[1] ? high : (high > _nyHigh ? high : _nyHigh)

prevDayLow := isMidnight ? (nyHL ? _nyLow : request.security(syminfo.tickerid, "1D", low)) : prevDayLow
prevDayHigh := isMidnight ? (nyHL ? _nyHigh : request.security(syminfo.tickerid, "1D", high)) : prevDayHigh

plot(showHL and isReset ? prevDayLow : na, 
     color=pdC, 
     linewidth=1, 
     title='PDL', 
     style=plot.style_linebr)
plot(showHL and isReset ? prevDayHigh : na, 
     color=pdC, 
     linewidth=1, 
     title='PDH', 
     style=plot.style_linebr)


var float midnightPrice = na
midnightPrice := isMidnight ? open : midnightPrice
plot(showMidPrice and isReset ? midnightPrice : na, 
     color=mC, 
     linewidth=1, 
     title='Midnight Price', 
     style=plot.style_linebr)


var float openLNPrice = na
openLNPrice := isLondonOpen ? open : openLNPrice
plot(showLNOpenPrice and isLondon ? openLNPrice : na, 
     color=LC, 
     linewidth=1, 
     title='London Open Price', 
     style=plot.style_linebr)


var float openNYPrice = na //This will not show on all timeframes 
openNYPrice := isNewYorkOpen ? open : openNYPrice
plot(showNYOpenPrice and _isNewYork ? openNYPrice : na, 
     color=NYC, 
     linewidth=1, 
     title='New York Open Price', 
     style=plot.style_linebr)