Name New York's clock and add session.islastbar.
The script below closes every trade on the 15:55 New York bar or on the session's last bar. It cancels every order still waiting and takes no entry after it. On two years of MNQ it closed 227 trades at 15:55 and 15 on holiday early closes. No trade stayed open past its session.
The common line hour == 15 and minute >= 55 reads the chart's clock: Chicago on CME's NQ. It closed 217 trades an hour late at 16:55 New York. It also missed all 15 holiday early closes. Those trades ran into the next session and lost $581 where the rule above made $377 on them.
The code in three parts
1. Name the clock
time(timeframe.period, "1555-1600", "America/New_York") is the 15:55 bar on New York's clock. hour and minute with no zone read the exchange's clock. On CME's NQ and ES that is Chicago: an hour behind New York.
2. Catch the early close
session.islastbar marks the session's last bar on every day including a holiday's early close. A clock time alone never comes on those days.
3. Close and cancel
strategy.close_all with immediately=true closes at the bar's close and strategy.cancel_all removes orders still waiting. Put not flatNow in front of each entry's condition.
The rule on its own · 12 lines
// ── Flat before the session ends ───────────────────────────────────────────────────────────────── flatOn = input.bool(true, "Flat before the session ends", group="Session end") flatAt = input.session("1555-1600", "Flat from (New York)", group="Session end") // Name the clock: with no zone, hour() and minute() read the exchange's clock, Chicago on CME's NQ // and ES, an hour behind New York. session.islastbar also catches a holiday's early close flatNow = flatOn and (not na(time(timeframe.period, flatAt, "America/New_York")) or session.islastbar) // From the flat time on: no order left waiting and every trade closed at the bar's close if flatNow strategy.cancel_all() strategy.close_all("Flat", immediately=true) Paste it under your strategy() line and put not flatNow in front of each entry's condition. The full script below does the same.
Four ways to end the day
| HOW THE DAY ENDS | TRADES | WON | OPEN OVERNIGHT | 2 YEARS | 4 NOV 2024 → 23 FEB 2026 | 24 FEB 2026 → 22 OCT 2026 | WORST DRAWDOWN |
|---|---|---|---|---|---|---|---|
| Flat at 15:55 New York or the session's last bar | 500 | 45.4% | 0 | −$125 | +$8,255 | −$8,380 | −$10,460 |
| hour == 15 and minute >= 55 with no time zone | 500 | 44.6% | 15 | +$264 | +$8,222.50 | −$7,958.50 | −$10,704 |
| 15:55 New York with no session.islastbar | 500 | 44.8% | 15 | −$1,083 | +$7,627.50 | −$8,710.50 | −$11,095 |
| No flat rule | 383 | 38.1% | 182 | +$11,968.50 | +$15,437 | −$3,468.50 | −$11,151.50 |
One MNQ contract on a $50,000 account pays $2.50 a contract each side and a tick of slippage. The 4 runs were fixed before they ran. The worst drawdown is the deepest fall from a high between 4 November 2024 and 22 October 2026.
Flat at 15:55 New York the breakout lost $125 over two years. Held to the session's last bar at 16:55, as in our VWAP filter test, the same trades made $1,222: the last hour added $1,347 to the 227 trades still open at 15:55. With no flat rule 182 of 383 trades stayed open overnight and it made $11,968.50. All four lost money from 24 February 2026.
The holiday early closes with a trade open were 28 Nov 2024, 29 Nov 2024, 20 Jan 2025, 17 Feb 2025, 26 May 2025, 19 Jun 2025, 3 Jul 2025, 4 Jul 2025, 1 Sept 2025, 28 Nov 2025, 24 Dec 2025, 19 Jan 2026, 19 Jun 2026, 3 Jul 2026 and 7 Sept 2026. On each the market closed at 13:00 or 13:15 New York and the 15:55 bar never came.
The script as it ran
MNQ opening range breakout flat by 15:55 New York
A 15-minute opening range from 9:30 New York. The first close through it after a close inside it enters from 9:45 to 12:00 and the day takes one trade. The stop sits at the range's other side and the target at 2R. Every trade closes on the 15:55 New York bar or on the session's last bar. No order is left waiting.
- Trades: 500
- Won: 45.4%
- Profit factor: 1.00
- Net: −0.2%
- Worst drawdown: −17.6%
- 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,255 from 4 November 2024 to 23 February 2026, −$8,380 from 24 February 2026 to 22 October 2026.
Pine Script v6 · 62 lines
//@version=6 // Suggested timeframe: 5-minute strategy("MNQ opening range breakout, flat before the session ends", 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) // ── Flat before the session ends ───────────────────────────────────────────────────────────────── flatOn = input.bool(true, "Flat before the session ends", group="Session end") flatAt = input.session("1555-1600", "Flat from (New York)", group="Session end") // Name the clock: with no zone, hour() and minute() read the exchange's clock, Chicago on CME's NQ // and ES, an hour behind New York. session.islastbar also catches a holiday's early close flatNow = flatOn and (not na(time(timeframe.period, flatAt, "America/New_York")) or session.islastbar) // From the flat time on: no order left waiting and every trade closed at the bar's close if flatNow strategy.cancel_all() strategy.close_all("Flat", immediately=true) // ── The example strategy: a 15-minute opening range breakout, one trade a day ────────────────── // Your own entries go here, behind "not flatNow" orSess = input.session("0930-0945", "Opening range (New York)", group="Strategy") tradeSess = input.session("0945-1200", "Entry window (New York)", group="Strategy") rMult = input.float(2.0, "Target (R)", minval=0.5, step=0.5, group="Strategy") inOR = not na(time(timeframe.period, orSess, "America/New_York")) inTrade = not na(time(timeframe.period, tradeSess, "America/New_York")) var float orHigh = na var float orLow = na var int tradesToday = 0 var float stopLevel = na var float targetLevel = na if session.isfirstbar orHigh := na orLow := na tradesToday := 0 if inOR orHigh := na(orHigh) ? high : math.max(orHigh, high) orLow := na(orLow) ? low : math.min(orLow, low) // The setup: the first close through the range after a close inside it. The stop sits at the // range's other side and the target at rMult times the risk rangeSet = not inOR and not na(orHigh) longSetup = rangeSet and close > orHigh and close[1] <= orHigh shortSetup = rangeSet and close < orLow and close[1] >= orLow canTrade = inTrade and strategy.position_size == 0 and tradesToday < 1 and not flatNow if canTrade and longSetup and orLow < close 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 else if canTrade and shortSetup and orHigh > close 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 plot(rangeSet ? orHigh : na, "Range high", color=color.new(#16A56A, 0), style=plot.style_linebr) plot(rangeSet ? orLow : na, "Range low", color=color.new(#E5484D, 0), style=plot.style_linebr) A daily profit target and a limit on trades per day for the same breakout are on our daily profit target page and our trades per day page.
Questions
How do I close every position before the session ends in Pine Script?
Name New York's clock and close on the 15:55 bar or on the session's last bar. In the script flatNow is true when time(timeframe.period, "1555-1600", "America/New_York") is not na or when session.islastbar is true. On that bar call strategy.cancel_all() and strategy.close_all(immediately=true). Take no new entry after it. The script on this page does it in a block that goes on top of your own strategy.
Why does my strategy close at the wrong time?
Because hour and minute with no time zone read the exchange's clock. On CME's NQ and ES that is Chicago: an hour behind New York. In our test hour == 15 and minute >= 55 closed 217 trades at 16:55 New York. Name the zone: hour(time, "America/New_York").
What happens on a holiday when the market closes early?
A clock time never comes. On 15 holiday sessions in our test a trade was open when the market closed at 13:00 or 13:15 New York. session.islastbar closed them and they made $377. Without it they ran into the next session and lost $581.
Does closing before the session ends cost money?
On this breakout it did. Flat at 15:55 New York it lost $125 over two years. Held to the session's last bar at 16:55 the same trades made $1,222. With no flat rule 182 trades stayed open overnight and it made $11,968.50. All four lost money from 24 February 2026.
Should strategy.close_all use immediately=true?
With immediately=true the trade closes at the bar's close: 16:00 for the 15:55 bar. Without it the close waits for the next bar's open. On the session's last bar the next open is the next session's: without immediately=true a holiday's early close becomes an overnight trade.
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 switch the rule off and set the flat time. 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. 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 27 November 2026.
Originally published at getalphacharts.com.
