Black Friday: 2 years for the price of 1.Or 3 months for the price of 1. On Pro.2 d 5 hleftSee plans →
Free tool

How do I get the opening range high and low in Pine Script for any session?

Any start on any clock for any number of minutes. We ran the code on MNQ and MGC 5-minute bars from 4 November 2024. Written in Pine Script? Paste it here: a free account with no card runs it on CME Group futures with $2.50 a contract and a tick of slippage charged.

Free plan · unlimited backtests · no card
MNQ's 15-minute opening range at 2R · 9:30 New York against the zone left outMNQ1! · 5-minute · 4 November 2024 → 22 October 2026
9:30 New York over 2 years
+$1,222
Zone left out over 2 years
−$4,237
9:30 New York 24 Feb 2026 → 22 Oct 2026
−$7,628
Zone left out 24 Feb 2026 → 22 Oct 2026
+$875.50
Range from 9:30 New York+$1,222
Zone left out: 10:30 New York−$4,237
Nov 2024Feb 2026Oct 2026

The $50,000 account after each trade. One MNQ contract pays $2.50 a contract each side and a tick of slippage. Dashed: the same code with the zone left out. Shaded: from 24 February 2026.

Paste your script

Pine v6, as TradingView has it.

MNQ's 15-minute opening range at 2R · 9:30 New York against the zone left outMNQ1! · 5-minute · 4 November 2024 → 22 October 2026
9:30 New York over 2 years
+$1,222
Zone left out over 2 years
−$4,237
9:30 New York 24 Feb 2026 → 22 Oct 2026
−$7,628
Zone left out 24 Feb 2026 → 22 Oct 2026
+$875.50
Range from 9:30 New York+$1,222
Zone left out: 10:30 New York−$4,237
Nov 2024Feb 2026Oct 2026

The $50,000 account after each trade. One MNQ contract pays $2.50 a contract each side and a tick of slippage. Dashed: the same code with the zone left out. Shaded: from 24 February 2026.

Count the minutes since the range's start on a clock you name.

Start the high and low on the range's first bar and use them once its last bar has closed. The code is below. Its settings take the start, the length in minutes and the clock. Name the clock in hour() and minute(): with no zone they read the exchange's clock. That is Chicago on CME's NQ and ES and New York on COMEX gold. On MNQ a 9:30 range with the zone left out started at 10:30 New York. We ran the same 15-minute breakout both ways on MNQ 5-minute bars from 4 November 2024 to 22 October 2026. From 9:30 New York it made $1,222 over two years. With the zone left out it lost $4,237. On MGC the zone changed nothing.

The range on its own

Opening range high and low · 33 lines
//@version=6
indicator("Opening range high and low for any session", overlay=true)

// The range: its start on a clock you name and its length in minutes
startHour = input.int(9, "Range start: hour", minval=0, maxval=23)
startMin  = input.int(30, "Range start: minute", minval=0, maxval=59)
rangeLen  = input.int(15, "Range length (minutes)", minval=1, maxval=720)
tz        = input.string("America/New_York", "Clock (a time zone name)")

if timeframe.in_seconds() > rangeLen * 60
    runtime.error("The chart's bars are longer than the range. Use a chart of " + str.tostring(rangeLen) + " minutes or less.")

// Minutes since the range's start on that clock (0 to 1439): a range may cross midnight
since   = (hour(time, tz) * 60 + minute(time, tz) - (startHour * 60 + startMin) + 1440) % 1440
inRange = since < rangeLen

// The high and low start on the range's first bar. They are set once its last bar has closed
// and cleared when the next futures day opens (18:00 New York)
var float orHigh = na
var float orLow  = na
var bool  isSet  = false
if session.isfirstbar
    isSet := false
if inRange
    orHigh := inRange[1] ? math.max(orHigh, high) : high
    orLow  := inRange[1] ? math.min(orLow, low) : low
    isSet  := false
else if inRange[1]
    isSet := true

plot(isSet ? orHigh : na, "Range high", color=color.new(#16A56A, 0), style=plot.style_linebr)
plot(isSet ? orLow : na, "Range low", color=color.new(#E5484D, 0), style=plot.style_linebr)

Paste it as a new indicator or copy its lines into your strategy above your entries. Use orHigh and orLow once isSet is true.

  1. 1

    Minutes on a named clock

    With the zone named, hour() and minute() give the minutes since the start. The count wraps at midnight: a range may cross it.

  2. 2

    The high and low

    They start on the range's first bar and widen on each bar inside it.

  3. 3

    Set and then used

    They are used only once the range's last bar has closed. The next futures day's 18:00 New York open clears them.

Settings for other sessions

RANGESTARTMINUTESCLOCK
New York's first 15 minutes9:3015America/New_York
London's first hour8:0060Europe/London
Tokyo's first 30 minutes9:0030Asia/Tokyo
The futures day's first hour18:0060America/New_York

Name the clock

One breakout script ran 6 ways. The range lasts 15 minutes. For the next 135 minutes the first 5-minute close outside it enters. The stop sits at the range's other side and the target at 2R. There is one trade a day. The 6 settings were fixed before the runs.

MNQ

RANGE STARTTRADESWON2 YEARS4 NOV 2024 → 23 FEB 202624 FEB 2026 → 22 OCT 2026WORST DRAWDOWN
9:30 New York50045.2%+$1,222+$8,850−$7,628−$10,373.50
9:30 with the zone left outChicago's 9:30 = 10:30 New York49939.1%−$4,237−$5,112.50+$875.50−$7,975.50

From 9:30 New York the code gave the same 500 trades as the unfiltered 2R run in our VWAP filter test. That script builds its range with time() and a session string. With the zone left out our range started at 10:30 New York: the run equals one started at 10:30 trade for trade. Removing the zone from that script's time() calls moved it to 10:30 too. The two hours are different tests. From 9:30 New York the breakout made $1,222 over two years and lost $7,628 from 24 February 2026. From 10:30 it lost $4,237 over two years and made $875.50 from 24 February 2026.

MGC

RANGE STARTTRADESWON2 YEARS4 NOV 2024 → 23 FEB 202624 FEB 2026 → 2 NOV 2026WORST DRAWDOWN
9:30 New York50444.4%−$1,132+$459−$1,591−$5,061
9:30 with the zone left outCOMEX's clock is New York's50444.4%−$1,132+$459−$1,591−$5,061
8:00 London50533.1%+$1,168+$4,321−$3,153−$4,290
3:00 New York50533.5%−$44+$4,377−$4,421−$5,929

On MGC the zone left out changed nothing: the two runs matched trade for trade. London's 8:00 is 3:00 New York for most of the year. On 40 weekdays of our two years it was 4:00: 10 to 28 March 2025, 27 to 31 October 2025, 9 to 27 March 2026 and 26 to 30 October 2026. The US and UK clocks changed on different dates. A range written as 3:00 New York traded a different hour on all 40 of them. London's clock made $726 on those days and 3:00 New York lost $486.

One contract on a $50,000 account pays $2.50 a contract each side and a tick of slippage. MNQ's bars run from 4 November 2024 to 22 October 2026 and MGC's to 2 November 2026. The worst drawdown is the deepest fall from a high over the whole run.

The script as it ran

Opening range breakout for any session

The range starts at 9:30 New York and lasts 15 minutes. For the next 135 minutes the first 5-minute close outside it enters. The stop sits at the range's other side and the target at 2R. There is one trade a day. Its inputs set the start, the length in minutes and the clock.

Trades
500
Won
45.2%
Profit factor
1.02
Net
+2.4%
Worst drawdown
−17.2%
Worst trade
−$749

MNQ1! · 5-minute · 4 Nov 2024 → 22 Oct 2026 · $2.50 a contract each side and a tick of slippage · one contract on a $50,000 account

+$8,850 from 4 November 2024 to 23 February 2026, −$7,628 from 24 February 2026 to 22 October 2026.

Pine Script v6 · 64 linesRun this script
//@version=6
// Suggested timeframe: 5-minute (the chart's bars must be no longer than the range)
strategy("Opening range breakout for any session", overlay=true, initial_capital=50000, default_qty_type=strategy.fixed, default_qty_value=1,
     commission_type=strategy.commission.cash_per_contract, commission_value=2.5, slippage=1, margin_long=0, margin_short=0)

// The range: its start on a clock you name and its length in minutes. Name the clock: with no zone,
// hour() and minute() read the exchange's clock (Chicago on CME's NQ and ES, New York on COMEX and NYMEX)
startHour = input.int(9, "Range start: hour", minval=0, maxval=23)
startMin  = input.int(30, "Range start: minute", minval=0, maxval=59)
rangeLen  = input.int(15, "Range length (minutes)", minval=1, maxval=720)
tz        = input.string("America/New_York", "Clock (a time zone name)")
entryLen  = input.int(135, "Entries for (minutes after the range)", minval=1, maxval=1440)
rMult     = input.float(2.0, "Target (R)", minval=0.5, step=0.5)

if timeframe.in_seconds() > rangeLen * 60
    runtime.error("The chart's bars are longer than the range. Use a chart of " + str.tostring(rangeLen) + " minutes or less.")

// Minutes since the range's start on that clock (0 to 1439): a range may cross midnight
since   = (hour(time, tz) * 60 + minute(time, tz) - (startHour * 60 + startMin) + 1440) % 1440
inRange = since < rangeLen
inEntry = since >= rangeLen and since < rangeLen + entryLen

// The range's high and low start on its first bar. They are set once its last bar has closed
// and cleared when the next futures day opens (18:00 New York)
var float orHigh      = na
var float orLow       = na
var bool  isSet       = false
var int   tradesToday = 0
if session.isfirstbar
    isSet       := false
    tradesToday := 0
if inRange
    orHigh := inRange[1] ? math.max(orHigh, high) : high
    orLow  := inRange[1] ? math.min(orLow, low) : low
    isSet  := false
else if inRange[1]
    isSet := true

// The breakout: the first close outside the set range and one trade a day. The stop sits at the
// range's other side and the target at rMult times the risk. The trade closes on the day's last bar
var float stopLevel   = na
var float targetLevel = na
canTrade = isSet and inEntry and strategy.position_size == 0 and tradesToday < 1
if canTrade and close > orHigh and close[1] <= orHigh
    stopLevel   := orLow
    targetLevel := close + (close - orLow) * rMult
    strategy.entry("Long", strategy.long)
    strategy.exit("Long exit", from_entry="Long", stop=stopLevel, limit=targetLevel)
    tradesToday += 1
if canTrade and close < orLow and close[1] >= orLow
    stopLevel   := orHigh
    targetLevel := close - (orHigh - close) * rMult
    strategy.entry("Short", strategy.short)
    strategy.exit("Short exit", from_entry="Short", stop=stopLevel, limit=targetLevel)
    tradesToday += 1
if strategy.position_size > 0
    strategy.exit("Long exit", from_entry="Long", stop=stopLevel, limit=targetLevel)
if strategy.position_size < 0
    strategy.exit("Short exit", from_entry="Short", stop=stopLevel, limit=targetLevel)
if session.islastbar
    strategy.close_all("Session end", immediately=true)

plot(isSet ? orHigh : na, "Range high", color=color.new(#16A56A, 0), style=plot.style_linebr)
plot(isSet ? orLow : na, "Range low", color=color.new(#E5484D, 0), style=plot.style_linebr)

The same breakout is tested on the retest in Breakout or retest on NQ's opening range and with a trailing stop in Trailing stop or fixed target on NQ's opening range. A VWAP filter on it is tested in Does a VWAP filter improve the NQ opening range breakout?

Free account

No card, no time limit.

$0
  • Unlimited backtests, costs charged, with the out-of-sample check
  • The Pine script audit and the full trade list
  • The Pine Copilot writes and edits your script
  • 50 AI credits a day
The Alpha Charts terminal a free account opens: the Pine Copilot beside the chart

Prop Ready opens all two years of futures minute bars on a computer for $39 paid once and has 7 days money back. Pro adds the optimiser and every AI tool and has 30 days money back. See the plans

Questions

How do I get the opening range high and low in Pine Script for any session?

Count the minutes since the range's start on a named clock: (hour(time, tz) * 60 + minute(time, tz) - start + 1440) % 1440. A bar is in the range while that count is under the range's length. Start the high and low on the range's first bar and widen them on each bar inside it. Use them only once the range's last bar has closed and clear them when the next futures day opens. The code on this page does this for any start and any length.

Why does my opening range start an hour late on NQ?

Your script reads the exchange's clock. With no time zone named, Pine's hour(), minute() and time() use the symbol's own: Chicago for CME's NQ and ES and New York for COMEX gold. A range written for 9:30 New York then starts at 9:30 Chicago: 10:30 New York. Name the zone in each call: hour(time, "America/New_York") reads New York's clock. On MNQ the same 15-minute breakout made $1,222 over two years from 9:30 New York and lost $4,237 with the zone left out.

How do I set London's or Tokyo's open?

Name its own clock: 8:00 on Europe/London or 9:00 on Asia/Tokyo. Written in New York's time they move when the clocks change. London's 8:00 was 4:00 New York on 40 weekdays of our two years and 3:00 on the rest. Tokyo keeps no summer time: its 9:00 is 19:00 New York in winter and 20:00 in New York's summer.

Does it repaint?

No. The high and low come from the range's own bars. The script uses them only once the range's last bar has closed. It reads no other timeframe: nothing from a later bar reaches an earlier one.

What chart does it need?

Bars no longer than the range: a 5-minute chart for a 15-minute range. The range's start should fall on a bar's open. On longer bars the script stops with a message instead of drawing a wrong range.

Can I run it myself?

Yes. Run opens the script on MNQ 5-minute bars in the Alpha Charts terminal. On a computer, a free account with no card runs it as published on all two years. Its inputs set the start, the length in minutes and the clock. A changed input or line makes it your own script: a free account then tests the newest 30 days of a chart under an hour. Prop Ready ($39 paid once) opens all two years for 30 days.

Is a backtest a prediction?

No. It is what a strategy's rules would have done on past prices with costs charged. Live fills, news and your own execution differ. Read it as a test of the rules and not as a forecast.

Every number comes from Alpha Charts' tester on MNQ 5-minute bars from 4 November 2024 to 22 October 2026 and MGC 5-minute bars from 4 November 2024 to 2 November 2026. The runs were made on 27 November 2026. Alpha Charts is software for testing trading scripts and gives no investment advice. A backtest shows what rules would have done on past data. It does not show what they will do. Our methodology is in Data and methodology. Updated 28 November 2026.

Test your Pine Script freeFree account, no cardPaste your script