Opening Range & Prior Day High/Low [Gorb]
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © GorbAlgo

//@version=5
indicator("Opening Range & Prior Day High/Low [Gorb]", shorttitle="ORB & PDH/L [Gorb]", overlay=true)

// Opening Range Break Configuration Settings
// Group: Customize opening range and trading session time to users preferances
_ORB = "Opening Range Break Settings"
ORBrange = input.session(title='Opening Range', defval='0930-0945', group=_ORB)
ORBextend = input.session(title='Extend Range Lines', defval='0930-1600', group=_ORB)

// Opening Range Break Color Style Settings
// Group: Customize opening range colors to meet users needs
_ORBcolors = "Opening Range Colors"
showORB = input(title='Enable Opening Range Box', defval=true, inline='ORB top', group=_ORBcolors)
BorderColor = input(color.new(color.purple, 40), 'Border', inline='ORB bottom', group=_ORBcolors)
ShadeColor = input(color.new(color.white, 90), 'Background', inline='ORB bottom', group=_ORBcolors)
linesWidth = input.int(1, 'Width', minval=1, maxval=4, inline='ORB bottom', group=_ORBcolors)

// Check if the current time is within the defined sessions
inSession = not na(time(timeframe.period, ORBrange))
inExtend = not na(time(timeframe.period, ORBextend))

// Initialize variables to store indices and price levels for ORB
var int orbRangeStartIndex = na
var int orbExtendEndIndex = na
var float orbExtendEndTime = na
var orb_low = 0.0
var orb_high = 0.0
var box orbBox = na
var bool isOrbBoxCreated = false

// Calculate and draw the ORB box at session start
if showORB and inSession and not inSession[1]
    orbRangeStartIndex := bar_index
    orb_low := low
    orb_high := high

if inExtend
    orbExtendEndIndex := bar_index

// Create the ORB box and set its properties
if showORB and inSession and not inSession[1]
    orbBox := box.new(left=orbRangeStartIndex, top=orb_high, right=orbExtendEndIndex, bottom=orb_low, border_color=BorderColor, border_width=linesWidth)
    box.set_bgcolor(orbBox, ShadeColor)
    isOrbBoxCreated := true

// Update ORB high and low within the session
if inSession
    if high > orb_high
        orb_high := high
        if isOrbBoxCreated
            box.set_top(orbBox, orb_high)
    if low < orb_low
        orb_low := low
        if isOrbBoxCreated
            box.set_bottom(orbBox, orb_low)


// Extend the ORB box to the end of the extend session
if inExtend and isOrbBoxCreated
    // Find the bar index for the end of the ORBextend session
    if na(orbExtendEndIndex) or time > orbExtendEndIndex
        orbExtendEndIndex := bar_index
    if time[1] < orbExtendEndTime and time >= orbExtendEndTime
        orbExtendEndIndex := bar_index

    // Set the right edge of the box to the end index of the ORBextend session
    box.set_right(orbBox, orbExtendEndIndex)
// End of Opening Range Break code

/// Starting Previous Day's High/Low
// Previous Day's High/Low Settings
// Group: Allows users to customize how they want to display the high/low levels from the previous trading day.
_PDHLGrp = "Previous Day High/lows"
showPreviousDayHigh  = input.bool(true, title='Enable PDH', inline='PD top', group= _PDHLGrp)
showPreviousDayLow   = input.bool(true, title='Enable PDL', inline='PD top', group= _PDHLGrp)
colorPDH = input(color.new(color.green, 0), title='PDH Color', inline='PD bottom', group= _PDHLGrp)
colorPDL = input(color.new(color.red, 0), title='PDL Color', inline='PD bottom', group= _PDHLGrp)

// Initializing lines and variables to store previous day's high and low prices.
var line highLinePreviousDay = na
var line lowLinePreviousDay = na
var float highPreviousDay = na
var float lowPreviousDay = na

// This block calculates the previous day's high and low by fetching data from the daily timeframe.
highPreviousDay := request.security(syminfo.tickerid, 'D', high[1], lookahead=barmerge.lookahead_on)
lowPreviousDay := request.security(syminfo.tickerid, 'D', low[1], lookahead=barmerge.lookahead_on)

// Adjusting Line Extensions and Creating New Lines
if session.isfirstbar
    line.set_extend(highLinePreviousDay, extend.none)
    line.set_extend(lowLinePreviousDay, extend.none)

    if timeframe.isintraday
        if showPreviousDayHigh
            highLinePreviousDay := line.new(bar_index, highPreviousDay, bar_index, highPreviousDay, color=colorPDH, style=line.style_solid, width=1)
        if showPreviousDayLow
            lowLinePreviousDay := line.new(bar_index, lowPreviousDay, bar_index, lowPreviousDay, color=colorPDL, style=line.style_solid, width=1)

// Setting the End Point of the High and Low Lines
line.set_x2(highLinePreviousDay, bar_index)
line.set_x2(lowLinePreviousDay, bar_index)
// End of indicator script