Opening Range Box
An opening range box is a tool that marks the high and low price during a designated period after market open (typically the first 30-90 minutes). Traders use it to identify key support and resistance levels, spot intraday breakouts beyond the range, and establish reference zones for mean-reversion strategies. This indicator draws a shaded box showing that range, with configurable duration in minutes, and plots horizontal lines at the breakout levels for quick visual reference.
//@version=6
indicator("Opening Range Box", overlay=true)
// Inputs
orMinutes = input.int(60, "Opening Range Duration (minutes)", minval=1, maxval=480)
showORBox = input.bool(true, "Show Opening Range Box")
showBreakoutLevels = input.bool(true, "Show Breakout Levels")
boxBgColor = input.color(color.new(color.orange, 85), "Box Background Color")
boxBorderColor = input.color(color.orange, "Box Border Color")
levelColor = input.color(color.orange, "Level Color")
// Session and OR tracking variables
var float orHigh = na
var float orLow = na
var int orBarStart = na
var box orBox = na
var time orEndTime = na
// Detect new trading session (daily reset)
newSession = ta.change(time("D")) != 0 or barindex == 0
if newSession
orHigh := na
orLow := na
orBarStart := na
orBox := na
orEndTime := na
// Initialize opening range on first bar of new session
if na(orBarStart)
orBarStart := bar_index
orHigh := high
orLow := low
orEndTime := time + (orMinutes * 60 * 1000) // Convert minutes to milliseconds
// Track high and low prices during opening range window
if time < orEndTime
orHigh := math.max(orHigh, high)
orLow := math.min(orLow, low)
// Draw box once opening range period ends (no repainting)
if time >= orEndTime and na(orBox) and showORBox
orBox := box.new(left=orBarStart, top=orHigh, right=bar_index - 1,
bottom=orLow, xloc=xloc.bar_index, bgcolor=boxBgColor,
border_color=boxBorderColor, border_width=2)
// Extend box to include all subsequent bars
if time >= orEndTime and not na(orBox)
box.set_right(orBox, bar_index - 1)
// Plot high and low breakout levels
plot(showBreakoutLevels and not na(orHigh) ? orHigh : na, "OR High", levelColor, linewidth=1)
plot(showBreakoutLevels and not na(orLow) ? orLow : na, "OR Low", levelColor, linewidth=1)
How the code works
The indicator starts a new session each day using ta.change(time("D")), which detects when the date changes. On the first bar of the session, it records the bar index and sets the initial high and low to the open candle's levels. As subsequent candles arrive, it compares their highs and lows against the existing range using math.max() and math.min(), expanding the opening range box to capture the true high and low.
The end of the opening range period is calculated by adding the user's configured minutes (converted to milliseconds) to the opening time. Once the current candle's timestamp exceeds that end time, the box is drawn exactly once using box.new(), positioned from the first bar to the bar just before the range closed. After that, the box's right edge is extended on each subsequent bar to keep it visible and readable as the chart advances. The two horizontal level plots show the high and low boundaries and persist indefinitely for reference.
Because the box is only drawn after the opening range period closes, there is no repainting: the high, low, and box position are final and never recalculated.
Reading it on a chart
The opening range box appears as a shaded rectangle anchored to the left edge of the first candle in the session. The top edge shows the high price reached during the opening period, and the bottom edge shows the low. The horizontal lines plotted above and below the box mark those exact levels across the entire chart.
Traders watch for breaks above or below the opening range as a potential signal of directional momentum. A break above the high (especially on volume) may indicate buyers are in control; a break below the low may suggest selling pressure. The box itself also serves as an intraday support or resistance level: price often gravitates toward the opening range midpoint as the session progresses, or bounces off the top and bottom edges during consolidation.
The indicator works best on intraday timeframes (1, 5, 15, 30-minute bars) where the opening range captures a meaningful sample of early price action. It is configurable from 1 to 480 minutes, permitting customization for different markets and trading styles.
Limitations
Opening range boxes assume a single, contiguous trading session per day. Indices or equities that trade multiple sessions (pre-market, regular, extended hours) will not produce separate boxes for each session unless the indicator is manually reset or a more sophisticated session-definition input is added.
The indicator uses calendar date (time("D")) to detect session boundaries, which may not align exactly with market open times depending on the user's timezone and how the data is transmitted. A session that spans midnight (such as an overnight futures contract) will reset the box mid-trade if the calendar date changes before the market closes.
Because the indicator relies on bar timestamps to determine when the opening range has closed, accuracy depends on the timeframe chosen. On a 1-minute chart, the box closes at the configured minute; on a 5-minute chart, it closes at or shortly after that minute. Lower-resolution timeframes (hourly or daily) will produce a box that represents a wider opening window than expected.
The box drawing code updates the right edge every bar after the opening range ends, which is safe (non-repainting) but can be visually noisy if the chart is being actively scrolled or zoomed during the day. Disabling "Show Opening Range Box" and keeping only "Show Breakout Levels" produces cleaner breakout-only references.
The indicator has no access to session schedules or market holidays, so it will create a box even if the market is closed on a particular day. Users working with gapped or irregular data should validate that the opening range reflects actual market open conditions.
Key definitions
Opening range: the high and low prices established during a fixed period immediately after market open, typically 30 to 120 minutes.
Session: a single day or period of continuous trading, usually from market open to market close.
Breakout: a price movement that exceeds the high or low of a prior reference level (e.g., the opening range).
Repainting: recalculation of indicator values on historical bars due to lookahead bias or conditional logic that retroactively applies new data; this indicator avoids repainting by finalizing the box only after the opening range period expires.
Support and resistance: price levels at which buying or selling interest historically accumulates, causing price reversals or pauses; the opening range high and low often serve these roles intraday.
Bar index: the chronological position of a candle on the chart, numbered from the earliest visible bar (0 or 1) to the most recent.
References
-
CME Group, "E-Mini S&P 500 Futures (ES) Contract Specifications", CME Education, https://www.cmegroup.com/education/contract-specs.html (no date; market-standard contract hours).
-
TradingView, "time() Function", Pine Script v6 Reference Manual, https://www.tradingview.com/pine-script-reference/ (2025; documentation for time measurement and session detection in Pine Script).
-
Wilder, J. Welles, "New Concepts in Technical Trading Systems", Trend Research Ltd. (1978; foundational text on support/resistance and range-based trading concepts).
-
O'Neil, William J., "How to Make Money in Stocks", McGraw-Hill (2002; practitioner coverage of opening range concepts in equity trading).
Educational research on historical data only. Not investment advice, not a signal, and never a performance promise. Past results do not predict future performance. Every reference is link-verified before publication and every paper is re-audited weekly against the library's editorial standard.
Last reviewed by the PropLedger research pipeline: 2026-09-13. Educational research on historical data, not financial advice.
Keep reading
Fair Value Gap Marker with Mitigation Tracking
Free open-source Pine Script indicator: fair value gap marker with mitigation tracking. Full code and a plain-English walkthrough.
Session VWAP with Standard Deviation Bands
Free open-source Pine Script indicator: session VWAP with standard deviation bands. Full code and a plain-English walkthrough.
Prior Day Levels
Free open-source Pine Script indicator: prior day high, low, and settlement levels. Full code and a plain-English walkthrough.