//@version=5
indicator(title="Universal ORB/H/L", shorttitle="Uni ORB", overlay=true)
// --- 1. Inputs ---
orbTimeframe = input.string("5", title="ORB Timeframe", options=["1", "5", "10", "15", "30", "60"], group="ORB Settings")
showHigh = input.bool(true, title="OR High", group="ORB Settings")
showLow = input.bool(true, title="OR Low", group="ORB Settings")
showMid = input.bool(true, title="OR Mid", group="ORB Settings")
showPDHL = input.bool(true, title="Prev Day H/L", group="HTF Levels")
showPWHL = input.bool(true, title="Prev Week H/L", group="HTF Levels")
// --- 2. Logic ---
// Detect the start of a new day
is_new_day = ta.change(time("D")) != 0
// Fetch Previous Day/Week High and Low
// We use "lookahead_on" with [1] to get the truly completed previous period value
pdh = request.security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
pdl = request.security(syminfo.tickerid, "D", low[1], lookahead=barmerge.lookahead_on)
pwh = request.security(syminfo.tickerid, "W", high[1], lookahead=barmerge.lookahead_on)
pwl = request.security(syminfo.tickerid, "W", low[1], lookahead=barmerge.lookahead_on)
// Fetch the High and Low of the Opening Range timeframe
orbH_raw = request.security(syminfo.tickerid, orbTimeframe, high, lookahead=barmerge.lookahead_off)
orbL_raw = request.security(syminfo.tickerid, orbTimeframe, low, lookahead=barmerge.lookahead_off)
// Variables to store the fixed ORB for the rest of the day
var float dailyORH = na
var float dailyORL = na
if is_new_day
dailyORH := orbH_raw
dailyORL := orbL_raw
orbMid = (dailyORH + dailyORL) / 2
// --- 3. Plotting ---
// Previous Day Levels
plot(showPDHL ? pdh : na, title="PDH", color=color.new(#910099, 0), style=plot.style_line, linewidth=2)
plot(showPDHL ? pdl : na, title="PDL", color=color.new(#995400, 0), style=plot.style_line, linewidth=2)
// Previous Week Levels
plot(showPWHL ? pwh : na, title="PWH", color=color.new(#009994, 0), style=plot.style_line, linewidth=2)
plot(showPWHL ? pwl : na, title="PWL", color=color.new(#999900, 0), style=plot.style_line, linewidth=2)
// Opening Range Levels
hPlot = plot(showHigh ? dailyORH : na, title="OR High", color=color.new(#009924, 0), style=plot.style_line, linewidth=2)
lPlot = plot(showLow ? dailyORL : na, title="OR Low", color=color.new(#990000, 0), style=plot.style_line, linewidth=2)
plot(showMid ? orbMid : na, title="OR Mid", color=color.new(#000d99, 0), style=plot.style_line, linewidth=1)
// Shading the Opening Range
fill(hPlot, lPlot, color=color.new(color.gray, 90), title="ORB Fill")