Sessions and time
Opening Range
The high and low of the first minutes of a session, held across the rest of the day, which is the reference most intraday traders are using whether or not they draw it on the chart.
indicator
//@version=6
indicator("Opening Range", "Open Rng", overlay = true)
sessionSpec = input.session("0930-1600", "Session", group = "Sessions")
zone = input.string("America/New_York", "Time zone", group = "Sessions")
openMinutes = input.int(30, "Opening range minutes", minval = 1, group = "Settings")
rangeCol = input.color(#2962FF, "Range", group = "Style")
bull = input.color(#26A69A, "Break up", group = "Style")
bear = input.color(#EF5350, "Break down", group = "Style")
inSession = not na(time(timeframe.period, sessionSpec, zone))
sessionStart = inSession and not inSession[1]
var float rangeHigh = na
var float rangeLow = na
var int openedAt = na
if sessionStart
openedAt := time
rangeHigh := high
rangeLow := low
building = inSession and not na(openedAt) and time < openedAt + openMinutes * 60000
if building
rangeHigh := math.max(rangeHigh, high)
rangeLow := math.min(rangeLow, low)
settled = inSession and not building
pHigh = plot(inSession ? rangeHigh : na, "Range high", color = rangeCol, style = plot.style_linebr)
pLow = plot(inSession ? rangeLow : na, "Range low", color = rangeCol, style = plot.style_linebr)
fill(pHigh, pLow, color = color.new(rangeCol, 93), title = "Opening range")
brokeUp = settled and close > rangeHigh and close[1] <= rangeHigh[1]
brokeDown = settled and close < rangeLow and close[1] >= rangeLow[1]
plotshape(brokeUp, "Break above range", location = location.belowbar, style = shape.triangleup, color = bull, size = size.tiny)
plotshape(brokeDown, "Break below range", location = location.abovebar, style = shape.triangledown, color = bear, size = size.tiny)
alertcondition(brokeUp, "Opening range broken up", "Price closed above the opening range high")
alertcondition(brokeDown, "Opening range broken down", "Price closed below the opening range low")
This runs in TradingView, not here
Pine Script only executes inside TradingView. Paste the source into the Pine Editor and add it to a chart to see it plotted.
What it will not do
- The window is measured from bar open times, so on a timeframe that does not divide into it evenly the straddling bar is included and the range is slightly wider than asked for.
- A break of the range is a location, not a direction. Ranges are broken both ways on the same day more often than people remember.
- On a daily or higher chart there is no opening window to measure and nothing is drawn.
- Session times are given in the exchange time zone you choose. Check the shading against a known open before trusting the levels.
Written from this description
Mark the high and low of the first thirty minutes of a configurable session, extend both across the rest of the day, shade between them, and alert when price closes outside the range once it is set.
Educational only, not financial advice. The maths is simple arithmetic on the numbers you enter; it knows nothing about your broker, fees, slippage or the market. Something wrong with it? Say so in Site Feedback.