Opening Range & Daily and Weekly Pivots
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © StockJustice

//@version=5
indicator('Opening Range & Daily and Weekly Pivots', shorttitle='ORB + Pivots', overlay=true)

YesterdayHigh = request.security(syminfo.tickerid, 'D', high[1], lookahead=barmerge.lookahead_on)
YesterdayLow = request.security(syminfo.tickerid, 'D', low[1], lookahead=barmerge.lookahead_on)

LastWeekHigh = request.security(syminfo.tickerid, 'W', high[1], lookahead=barmerge.lookahead_on)
LastWeekLow = request.security(syminfo.tickerid, 'W', low[1], lookahead=barmerge.lookahead_on)

today = year == year(timenow) and month == month(timenow) and dayofmonth == dayofmonth(timenow)

plot(today ? YesterdayHigh : na, style=plot.style_circles, color=color.new(color.green, 0), linewidth=2)
plot(today ? YesterdayLow : na, style=plot.style_circles, color=color.new(color.red, 0), linewidth=2)
plot(dayofweek==dayofweek(timenow) ? LastWeekHigh : na, style=plot.style_circles, color=color.new(#3912e7, 0), linewidth=2)
plot(dayofweek==dayofweek(timenow) ? LastWeekLow : na, style=plot.style_circles, color=color.new(color.purple, 0), linewidth=2)

alertcondition(close > YesterdayHigh, title='Breakout Above Yesterday High', message='We are now trading higher than the previous daily high.')
alertcondition(close < YesterdayLow, title='Breakdown Below Yesterday Low', message='We are now trading lower than the previous daily low.')
alertcondition(close > LastWeekHigh, title='Breakout Above Last Week High', message='We are now trading higher than the last week high.')
alertcondition(close < LastWeekLow, title='Breakdown Below Last Week Low', message='We are now trading lower than the last week low.')

inputMax = input(30, title= "ORB total time (minutes)")
sessionInput = input.session("0930-1000", title="ORB Timeframe")

t = time(timeframe.period, sessionInput)
hide = (timeframe.isintraday and timeframe.multiplier <= inputMax)

in_session = not na(t)
is_first = in_session and not in_session[1]

var float orb_high = 0.0
var float orb_low = 0.0

if is_first
    orb_high := high
    orb_low := low
else
    orb_high := orb_high[1]
    orb_low := orb_low[1]
if high > orb_high and in_session
    orb_high := high
if low < orb_low and in_session
    orb_low := low

plot(hide ? orb_high : na, style=plot.style_circles, color = close > orb_high ? color.rgb(168, 255, 17) : color.blue, title = "ORB High", linewidth = 1)
plot(hide ? orb_low : na, style=plot.style_circles, color = close < orb_low ? color.rgb(199, 55, 125) : color.white, title = "ORB Low", linewidth = 1)

alertcondition(close > orb_high, title='Breakout Above ORB High', message='Price has broken above the opening range.')
alertcondition(close < orb_low, title='Breakdown Below ORB Low', message='Price has broken below the opening range.')

////