MOJO Opening Range Box
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © yoursimplechoice

//@version=5
indicator("MOJO Opening Range Box", overlay=true)


 
InSession(sessionTime, sessionTimeZone=syminfo.timezone) =>
    not na(time(timeframe.period, sessionTime, sessionTimeZone))


// Define custom session trading times with inputs
sessionTime = input.session("0900-0955", title="Session time")
sessionZone = input.string("GMT-5", title="Session time zone")

// Other inputs for the boxes' visual appearance
boxBorderSize   = input.int(2, title="Box border size", minval=0)
upBoxColor      = input.color(color.new(color.green, 100), title="Up box")
upBorderColor   = input.color(color.green, title="Up border")
downBoxColor    = input.color(color.new(color.red, 100), title="Down box")
downBorderColor = input.color(color.red, title="Down border")

// Create variables
var sessionHighPrice = 0.0
var sessionLowPrice  = 0.0
var sessionOpenPrice = 0.0
var box sessionBox = na


// See if the session is currently active and just started
inSession    = InSession(sessionTime, sessionZone) and timeframe.isintraday
sessionStart = inSession and not inSession[1]


// When a new session starts, set the session high and low to the data
// of the bar in the session.
if sessionStart
    sessionHighPrice := high
    sessionLowPrice  := low
    sessionOpenPrice := open
// Else, during the session, track the highest high and lowest low
else if inSession
    sessionHighPrice := math.max(sessionHighPrice, high)
    sessionLowPrice  := math.min(sessionLowPrice, low)


// When a session begins, make a new box for that session
if sessionStart
    sessionBox := box.new(left=bar_index, top=na, right=na, bottom=na,
         border_width=boxBorderSize)


// During the session, update that session's existing box
if inSession
    box.set_top(sessionBox, sessionHighPrice)
    box.set_bottom(sessionBox, sessionLowPrice)

    box.set_right(sessionBox, bar_index + 1)

    // See if bar closed higher than session open. When it did, make
    // box green (and use red otherwise).
    if close > sessionOpenPrice
        box.set_bgcolor(sessionBox, upBoxColor)
        box.set_border_color(sessionBox, upBorderColor)
    else
        box.set_bgcolor(sessionBox, downBoxColor)
        box.set_border_color(sessionBox, downBorderColor)
//end range box