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.

Pine Script v6 Free · TradingView free plan
Pine Script
//@version=6
strategy("Target Filled - Time of Day Filter Example", overlay=true, calc_on_every_tick=false)

// --- Inputs ---------------------------------------------------
openHour   = input.int(9,  "Start Hour (ET)", minval=0, maxval=23)
openMin    = input.int(30, "Start Minute", minval=0, maxval=59)
closeHour  = input.int(15, "End Hour (ET)", minval=0, maxval=23)
closeMin   = input.int(30, "End Minute", minval=0, maxval=59)
skipMidday = input.bool(false, "Exclude Lunch Window")
lunchStart = input.int(12, "Lunch Start Hour (ET)", minval=0, maxval=23)
lunchEnd   = input.int(13, "Lunch End Hour (ET)", minval=0, maxval=23)

// --- Minutes-of-day on the NY clock ------------------------------
nyMinuteOfDay = hour(time, "America/New_York") * 60 + minute(time, "America/New_York")
openMins      = openHour * 60 + openMin
closeMins     = closeHour * 60 + closeMin
inWindow      = nyMinuteOfDay >= openMins and nyMinuteOfDay < closeMins
inLunch       = skipMidday and nyMinuteOfDay >= lunchStart * 60 and nyMinuteOfDay < lunchEnd * 60
canTrade      = inWindow and not inLunch

// --- Sample signal — replace with your own entry logic ---------
fastMA = ta.ema(close, 9)
slowMA = ta.ema(close, 21)
canLong  = ta.crossover(fastMA, slowMA)  and canTrade
canShort = ta.crossunder(fastMA, slowMA) and canTrade

if canLong
    strategy.entry("Long", strategy.long)
    strategy.exit("Long Exit", "Long", profit=20, loss=10)

if canShort
    strategy.entry("Short", strategy.short)
    strategy.exit("Short Exit", "Short", profit=20, loss=10)

// Visual
bgcolor(not canTrade ? color.new(color.gray, 92) : na, title="Outside Trade Window")

Settings

InputDefaultPurpose
openHour / openMin9:30Window open, on the NY clock.
closeHour / closeMin15:30Window close.
skipMiddayoffToggles the lunch exclusion window.
lunchStart / lunchEnd12:00-13:00Bounds of the excluded lunch window.

How to use

  • Paste into the Pine Editor and swap the EMA-crossover placeholder for your own entry conditions.
  • All times run through hour(time, "America/New_York"), so the filter is independent of the chart's own timezone setting.
  • Gray shading marks bars where entries are blocked — check it visually against the window before trusting it live.
  • For a single simple window without minute precision, the session filter snippet is shorter.

Frequently Asked Questions

How do I only trade during certain hours in Pine Script?
Convert the bar time into minutes-of-day on the 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 whatever timezone the chart itself is set to.
How is this different from a session filter?
A session filter leans on TradingView's compact 0930-1600-style string through time(), which is short but limited to one continuous window. This hour-math version adds a midday exclusion, odd minute boundaries, and room for any extra logic — at the cost of a few more lines of code.

Get a Pine Script tuned to your prop firm.

Invite-only on TradingView within 24 hours. From $19/mo, cancel anytime.