Pine Script v6 time filters

Pine Script Sessions and Time: Time Zones, Dates and Weekdays

A Pine Script session filter tests not na(time(timeframe.period, session, timezone)). Use a session string for recurring hours, timestamp() for absolute date boundaries, and dayofweek(time, timezone) for the calendar weekday of a bar open. Keep the time zone explicit: a Sunday evening bar can belong to a Monday overnight session, and a fixed UTC offset will not follow daylight saving time.

Build a Pine time filter

Session, timestamp and weekday answer different questions

Use each value for one purpose. The time variable is the bar opening timestamp in milliseconds; it is not a formatted local time. Changing the chart display time zone does not change Pine calculations. Calendar variables such as dayofweek use the exchange time zone, while the function form lets you choose another zone.

QuestionPine value or functionBoundary to specify
Does this bar open inside recurring hours?time(timeframe.period, session, timezone)Session hours, day digits and time zone
Is this bar inside a dated interval?time compared with timestamp()Start inclusive, end exclusive
What weekday is the local bar open?dayofweek(time, timezone)Calendar day in the chosen zone
What trading day does the exchange session use?time_tradingdayRead its calendar date in UTC; it is not a custom session identifier

A complete overnight session filter

This diagnostic indicator uses 20:00 to 06:00 New York time for sessions ending Monday through Friday. The date range starts at local midnight on March 8, 2026 and stops at local midnight on March 10. Use ordinary time-based intraday candles with bars aligned to the window, such as 5-minute bars. The default window is fixed for a reproducible check; change the two timestamp dates to inspect another period.

  • The day suffix uses 1 for Sunday through 7 for Saturday. Omitting it allows all seven days. input.session supplies the time selection here; the code appends the weekday policy.
  • This is a bar-opening filter. It does not place orders, close positions at 06:00 or guarantee that a strategy fill happens inside the window.
Complete Pine v6 indicator: inspect the three 0/1 outputs in the Data Windowpinescript
//@version=6
indicator("Overnight session and date filter", overlay=true)

string zone = "America/New_York"
string hours = input.session("2000-0600", "Overnight hours")
string sessionSpec = hours + ":23456"
int rangeStart = timestamp(zone, 2026, 3, 8, 0, 0)
int rangeEnd = timestamp(zone, 2026, 3, 10, 0, 0)

if not timeframe.isintraday
    runtime.error("Use a time-based intraday chart")

bool inSession = not na(time(timeframe.period, sessionSpec, zone))
bool inDates = time >= rangeStart and time < rangeEnd
bool accepted = inSession and inDates
int localWeekday = dayofweek(time, zone)

bgcolor(accepted ? color.new(color.blue, 85) : na)
plot(inSession ? 1 : 0, "In recurring session", display=display.data_window)
plot(inDates ? 1 : 0, "In date range", display=display.data_window)
plot(accepted ? 1 : 0, "Accepted bar", display=display.data_window)
plot(localWeekday, "New York weekday: Sunday = 1", display=display.data_window)

Expected results across midnight and the date cutoff

These are expected outputs for available, aligned bars with the default settings, not a claim that every symbol supplies these bars. The Monday overnight session begins on Sunday evening. Adding a second condition that rejects calendar Sundays would remove its first four hours. The date cutoff is independent: it can cut a valid recurring session in half.

Bar opens in New YorkLocal weekdayIn session / in datesAccepted
Sunday, March 8, 2026 at 19:5510 / 10
Sunday, March 8, 2026 at 20:0011 / 11
Monday, March 9, 2026 at 05:5521 / 11
Monday, March 9, 2026 at 06:0020 / 10
Monday, March 9, 2026 at 23:5521 / 11
Tuesday, March 10, 2026 at 00:0031 / 00

Daylight saving time changes elapsed hours

America/New_York follows regional clock changes; UTC-5 stays fixed. The script below calculates two 09:30 local timestamps around the March 2026 change and two midnight-to-midnight durations. Its arithmetic is elapsed hours = (end timestamp - start timestamp) / 3,600,000. A local calendar day can therefore contain 23 or 25 elapsed hours. Do not add 86,400,000 milliseconds when the requirement is the next local midnight.

Constructed local time or intervalExpected UTC time or duration
March 6, 2026 at 09:30 New York14:30 UTC
March 9, 2026 at 09:30 New York13:30 UTC
March 9, 2026 at 09:30 in fixed UTC-514:30 UTC, one hour later
March 8 midnight to March 9 midnight in New York23 hours
November 1 midnight to November 2 midnight in New York25 hours
Complete Pine v6 indicator: fixed timestamp checks independent of the chart symbolpinescript
//@version=6
indicator("New York DST timestamp checks")

string zone = "America/New_York"
int beforeChange = timestamp(zone, 2026, 3, 6, 9, 30)
int afterChange = timestamp(zone, 2026, 3, 9, 9, 30)
int fixedOffset = timestamp("UTC-5", 2026, 3, 9, 9, 30)
float springHours = (timestamp(zone, 2026, 3, 9, 0, 0) - timestamp(zone, 2026, 3, 8, 0, 0)) / 3600000.0
float fallHours = (timestamp(zone, 2026, 11, 2, 0, 0) - timestamp(zone, 2026, 11, 1, 0, 0)) / 3600000.0

var table result = table.new(position.top_right, 2, 5)
if barstate.islast
    table.cell(result, 0, 0, "March 6 open (UTC)")
    table.cell(result, 1, 0, str.format_time(beforeChange, "HH:mm", "UTC"))
    table.cell(result, 0, 1, "March 9 open (UTC)")
    table.cell(result, 1, 1, str.format_time(afterChange, "HH:mm", "UTC"))
    table.cell(result, 0, 2, "Fixed UTC-5 open (UTC)")
    table.cell(result, 1, 2, str.format_time(fixedOffset, "HH:mm", "UTC"))
    table.cell(result, 0, 3, "Spring day hours")
    table.cell(result, 1, 3, str.tostring(springHours))
    table.cell(result, 0, 4, "Fall day hours")
    table.cell(result, 1, 4, str.tostring(fallHours))

Keep calendar dates separate from trading dates

dayofweek(time, zone) describes the bar open, so it correctly reports Sunday for the first row accepted by the overnight example. For the exchange-defined trading date, time_tradingday represents midnight UTC for the session ending day on daily and lower timeframes. Read that date with UTC calendar functions. It follows the symbol session, not the custom 20:00 to 06:00 window above.

  • For a Monday-only calendar rule, compare the local weekday with dayofweek.monday. For a Monday overnight session, use the session day suffix instead.
  • Numeric timestamp arguments without a zone use the exchange zone. The date-string overload defaults to UTC when its string has no zone. Explicit zones avoid mixing these defaults.
  • When constructing a boundary from the current bar date in another zone, extract year, month and dayofmonth using that same zone before passing them to timestamp().

Missing bars and partial bars need separate checks

A custom window filters data that exists. It does not create overnight bars on a regular-hours-only equity chart, restore a holiday, or infer prices during a feed gap. If the entire requested window is absent, there is no bar on which Pine can report an accepted value. Choose a symbol and session setting that contain the period you want to test.

  • time() and time_close() build session-specific bar representations. A non-na time_close() result alone does not prove the whole chart candle lies inside your window; an hourly chart bar can outlast the session representation.
  • For split hours, a comma separates windows, as in 0900-1130,1300-1600:23456. Test the excluded lunch interval as well as both openings.
  • Use time-based candles for these checks. Open realtime bars on price-based charts can have an unknown closing timestamp. The intraday guard is not a full chart-type validator.
  • A simple false-to-true session test can miss a new session when every available chart bar qualifies and the intervening closed hours contain no bars. Use an explicit session or trading-date boundary for reset logic.
SymptomLikely mechanism to inspectNext check
No blue barsDate range is outside loaded history, or no bars exist during the windowInspect In date range and available bar times separately
Filter shifts by one hourFixed UTC offset was used for a regional clockCompare an IANA zone across the DST boundary
A bar extends beyond session endThe bar opens inside the window but closes laterUse aligned smaller bars and compare the actual chart close
No event at exactly 20:00No bar exists at that minuteDetect the first available qualifying bar instead of timestamp equality

Give a coding agent the boundary cases

Supply the original script, symbol, chart timeframe, data-session setting, IANA zone and the expected rows above. Ask the Pine Script AI Coding Agent to preserve the indicator or strategy logic while adding separate session and date conditions. Inspect the generated source, run syntax validation, then compare the Data Window and DST table in TradingView. Compilation cannot establish the intended session behavior.

Where Pineify fits

Pine Script AI Coding Agent

Use a minimal script and explicit expected timestamps to request a session-filter change. Review the generated Pine code and test the midnight, DST and missing-data cases on your chart.

Build a Pine time filter

Frequently asked questions

Educational Pine Script examples, not trading signals or investment advice. Expected outputs depend on chart data and settings. Syntax validation does not verify chart behavior or trading results.

Sources and verification

Published by Pineify. Pineify is an independent product and is not affiliated with TradingView. Sources support the technical explanations; examples do not establish trading performance.