New York Sessions Morning, Lunch and afternoon. AMK
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mickeyahs, tony-ho

//@version=4
study("New York Sessions with Timezone", shorttitle="NY Sessions", overlay=true)

// User Inputs for New York Sessions and Times
show_rectangleNY = input(true, title="Show New York Session?")
colorMorning = input(color.blue, title="Morning Session Color")
colorLunch = input(color.orange, title="Lunchtime Session Color")
colorAfternoon = input(color.green, title="Afternoon Session Color")

// User Input for Timezone (in minutes, default is EST)
timeZoneOffset = input(0, title="Timezone offset in minutes", minval=-720, maxval=720)

timeMorning_start = input(9 * 60 + 30, title="Morning Start Time in minutes from midnight EST")
timeMorning_end = input(11 * 60 + 30, title="Morning End Time in minutes from midnight EST")

timeLunch_start = input(11 * 60 + 30, title="Lunch Start Time in minutes from midnight EST")
timeLunch_end = input(13 * 60 + 30, title="Lunch End Time in minutes from midnight EST")

timeAfternoon_start = input(13 * 60 + 30, title="Afternoon Start Time in minutes from midnight EST")
timeAfternoon_end = input(16 * 60, title="Afternoon End Time in minutes from midnight EST")

// Time Session Function
isSession(timeStart, timeEnd) =>
    adjustedTime = (timestamp("America/New_York", year, month, dayofmonth, hour, minute) - timestamp("America/New_York", year, month, dayofmonth, 0, 0) + timeZoneOffset * 60 * 1000) % 86400000
    adjustedTime >= timeStart * 60 * 1000 and adjustedTime <= timeEnd * 60 * 1000

// Apply Background Color for each sub-session
bgcolor(show_rectangleNY and isSession(timeMorning_start, timeMorning_end) ? colorMorning : na, transp=90)
bgcolor(show_rectangleNY and isSession(timeLunch_start, timeLunch_end) ? colorLunch : na, transp=90)
bgcolor(show_rectangleNY and isSession(timeAfternoon_start, timeAfternoon_end) ? colorAfternoon : na, transp=90)
