Pine Script Time of Day Filter
Gates entries to an hour range on the NY clock, with an optional lunch-exclusion window for midday chop.
//@version=6
strategy("Time of Day Filter", overlay=true, calc_on_every_tick=false)
// ── Inputs ──────────────────────────────────────────────
startHr = input.int(9, "Start Hour (ET)", minval=0, maxval=23)
startMin = input.int(30, "Start Minute", minval=0, maxval=59)
endHr = input.int(15, "End Hour (ET)", minval=0, maxval=23)
endMin = input.int(30, "End Minute", minval=0, maxval=59)
skipLunch = input.bool(false, "Exclude Lunch Window")
lunchStartHr = input.int(12, "Lunch Start Hour (ET)", minval=0, maxval=23)
lunchEndHr = input.int(13, "Lunch End Hour (ET)", minval=0, maxval=23)
// ── NY-clock minutes of day ──────────────────────────────
nyMins = hour(time, "America/New_York") * 60 + minute(time, "America/New_York")
startMins = startHr * 60 + startMin
endMins = endHr * 60 + endMin
inWindow = nyMins >= startMins and nyMins < endMins
inLunch = skipLunch and nyMins >= lunchStartHr * 60 and nyMins < lunchEndHr * 60
tradeOK = inWindow and not inLunch
// ── Entry logic (replace with your own conditions) ───────
longCondition = ta.crossover(ta.ema(close, 9), ta.ema(close, 21)) and tradeOK
shortCondition = ta.crossunder(ta.ema(close, 9), ta.ema(close, 21)) and tradeOK
if longCondition
strategy.entry("Long", strategy.long)
strategy.exit("Long Exit", "Long", profit=20, loss=10)
if shortCondition
strategy.entry("Short", strategy.short)
strategy.exit("Short Exit", "Short", profit=20, loss=10)
// Visual
bgcolor(not tradeOK ? color.new(color.gray, 92) : na, title="Outside Trade Window")
Settings
startHr/startMin— window open on the NY clock. Default 9:30.endHr/endMin— window close. Default 15:30.skipLunch— toggle the midday exclusion. Default off.lunchStartHr/lunchEndHr— lunch window bounds. Default 12:00-13:00.
How to use
- Paste into the Pine Editor and replace the EMA-crossover placeholder with your own entry conditions.
- All times are computed on the NY clock via
hour(time, "America/New_York"), so the filter is chart-timezone independent. - Gray shading marks bars where entries are blocked — verify the window visually before going live.
- For a single simple window, the session filter is shorter; use this version when you need minute precision or the lunch exclusion.
Frequently Asked Questions
How do I only trade during certain hours in Pine Script?
Convert bar time to minutes-of-day on your market’s clock — hour(time, "America/New_York") * 60 + minute(time, "America/New_York") — and compare it against start and end inputs. Gate every entry condition with the resulting boolean. This works on any chart timeframe and ignores the exchange’s native timezone.
How is this different from a session filter?
A session filter uses TradingView’s session-string format like 0930-1600 via time(), which is compact but limited to one continuous window. This hour-math version supports a midday exclusion window, odd minute boundaries, and any extra logic you want to bolt on — at the cost of a few more lines.
Related
Want a complete eval-ready strategy with time filters tuned?
This snippet is a building block. Our paid scripts are complete strategies — entries, tested trade windows, kill switch, and EOD flatten verified on 3 years of MES/NQ data. Instant delivery from $50.
View Plans — From $50