High & Low Of Custom Session
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goofoffgoose
// This script boxes a custom session and sets the box at the high and low of the session and draws that box to the next session. 
// Box color is determined by price in relation to the box position. Box color is set at the start of the next session. This allows
// user to lookback at multiple box sets to see how each day closed in relation to the session highlighed. 
// Table is for debuging 
//@version=5

indicator('High & Low Of Custom Session', shorttitle='High/Low ', overlay=true, max_boxes_count=500)

// Inputs
tf = input.timeframe("5", "Open Range Timeframe", tooltip="Set this to the timeframe you are viewing")

hilo_session = input.session('0831-0930', title='Custom Session')

show_open_box = input.bool   (title="Show Open Range Box?", defval=true       , group="Session") // show open range box

hiColor = input.color(color.rgb(0,220,255,85) , title="Close Hi Color"      , group="Session",   inline="Open Box color")
loColor = input.color(color.rgb(200,100,255,85) , title="Close Lo Color"      , group="Session",   inline="Open Box color" )
sessionBoxColor = input.color(color.rgb(70,70,70,50), title="Extended Range Color", group="Session",   inline="Open Box color")
hiloBoxColor = input.color(color.rgb(70,70,70,50), title="Open Range Color", group="Session",   inline="Open Box color")
show_bg = input.bool (title="Show background", defval=true, group="Background", inline="bg")
bgcolor = input.color(color.rgb(70,70,70,80), title="Background Color", group="Background", inline='bg')

//Create box
boxBorderSize = input.int(0, title="Box border size", minval=0)
// Variables
currentValue = request.security(syminfo.tickerid,"1" , close)
var dayHighPrice = 0.0
var dayLowPrice  = 0.0
var prevDayClose = 0.0
var box sessionBox = na
var box hiloBox = na

// See if a new calendar day started on the intra-day time frame
newDayStart = dayofmonth != dayofmonth[1] and 
     timeframe.isintraday


/////Sessions
inSession(session, sessionTimeZone=syminfo.timezone) => 
    na(time(tf, session + ":1234567")) == false 

//Functions
IsSessionStart(sessionTime, sessionTimeZone=syminfo.timezone) =>
    inSess = not na(time(timeframe.period, sessionTime, sessionTimeZone))
    inSess and not inSess[1]

IsLastBarSession(sessionTime, sessionTimeZone=syminfo.timezone) =>
    var int lastBarHour   = na
    var int lastBarMinute = na
    var int lastBarSecond = na
    inSess = not na(time(timeframe.period, sessionTime, sessionTimeZone))

    if not inSess and inSess[1]
        lastBarHour   := hour[1]
        lastBarMinute := minute[1]
        lastBarSecond := second[1]
    
    hour == lastBarHour and minute == lastBarMinute and second == lastBarSecond
   
SessionHigh(sessionTime, sessionTimeZone=syminfo.timezone) =>
    insideSession = not na(time(timeframe.period, sessionTime, sessionTimeZone))
    var float sessionHighPrice = na

    if insideSession and not insideSession[1]
        sessionHighPrice := high
    else if insideSession
        sessionHighPrice := math.max(sessionHighPrice, high)
    
    sessionHighPrice   
    
    
SessionLow(sessionTime, sessionTimeZone=syminfo.timezone) =>
    insideSession = not na(time(timeframe.period, sessionTime, sessionTimeZone))
    var float sessionLowPrice = na

    if insideSession and not insideSession[1]
        sessionLowPrice := low
    else if insideSession
        sessionLowPrice := math.min(sessionLowPrice, low)
    
    sessionLowPrice    

// Hi/low varibles
var float s_low = 0.0
var float s_high = 0.0

// In Session High/Low Calculations
var float hilo_low = close
var float hilo_high = close
var box  HiLoBox = na

// If a new day starts, set the high and low to that bar's data. Else
// during the day track the highest high and lowest low.
if IsSessionStart(hilo_session)
    dayHighPrice := high
    dayLowPrice  := low
    prevDayClose := close[1]
else
    dayHighPrice := math.max(dayHighPrice, high)
    dayLowPrice  := math.min(dayLowPrice, low)

// When a new day start, create a new box for that day.
// Else, during the day, update that day's box color
if IsSessionStart(hilo_session)
    sessionBox := box.new(left=bar_index, top=dayHighPrice,
         right=bar_index + 1, bottom=dayLowPrice,
         border_width=boxBorderSize,border_color=sessionBoxColor, bgcolor=sessionBoxColor)//, extend=extend.right)
    HiLoBox := box.new(left=bar_index, top=dayHighPrice,    
         right=bar_index +1, bottom=dayLowPrice,
         border_width=boxBorderSize, border_color=sessionBoxColor, bgcolor=hiloBoxColor)


var color boxColor = color(color.rgb(0,70,0,50))

isFirstBarOfSession = IsSessionStart(hilo_session)
isSessionLast = IsLastBarSession(hilo_session)

// Plot High/Low box 
if isFirstBarOfSession and show_open_box 
    box.set_left(id=sessionBox, left=bar_index - 1)
    box.set_left(id=HiLoBox, left=bar_index - 1)
else
    na
if isSessionLast and show_open_box
    s_low := dayLowPrice
    s_high := dayHighPrice
    box.set_right(id=sessionBox, right=bar_index)
    box.set_top(id=sessionBox, top=dayHighPrice)
    box.set_bottom(id=sessionBox, bottom=dayLowPrice)
    box.set_right(id =HiLoBox, right=bar_index)
    box.set_top(id   =HiLoBox, top=dayHighPrice)
    box.set_bottom(id=HiLoBox, bottom=dayLowPrice)
    
else

    box.set_right(id=sessionBox, right=bar_index )
    boxColor := (currentValue < (s_low)) ? (loColor) : (currentValue > (s_high)) ? (hiColor) : (hiloBoxColor)
    box.set_bgcolor(sessionBox, color=(boxColor))
    box.set_bgcolor(HiLoBox, color=(hiloBoxColor))
    int(na)

// Bg session color
bgcolor(inSession(hilo_session) and show_bg ?  color(bgcolor) : na)



//Debug table
var table myTable = table.new(position.top_right, 20, 20) 

a = "high"
b = s_high
c = "low"
d = s_low
e = "current"
f = currentValue


if barstate.isconfirmed  // defines the table



    table.cell(myTable, 6,2, str.tostring(a), text_color = color.teal) 
    table.cell(myTable, 6,3, str.tostring(b), text_color = color.teal) 
    table.cell(myTable, 7,2, str.tostring(c), text_color = color.purple) 
    table.cell(myTable, 7,3, str.tostring(d), text_color = color.purple)
    table.cell(myTable, 8,2, str.tostring(e), text_color = color.lime) 
    table.cell(myTable, 8,3, str.tostring(f), text_color = color.lime) 









