Concept 48 / 52·Time & Sessions·Session timing
‹ Swing Highs & LowsTrailing Stop ›

Time-of-Day Windows in Pine Script

session window · trading session filter · session hours · time filter · intraday session

A daily window on the exchange clock that tells a rule whether the bar is in the chosen part of the session, so the rule acts there and nowhere else.

Reads best on: Any market with a defined session on 1 to 30-minute charts: index futures, US and European stocks and FX by session. On daily charts the window has nothing to filter.
On this page: the Pine v6 script, checked, drawn on real NVDA bars. Backtest it free, costs charged, every trade on the chart.
Backtest results: Time-of-day strategy: SPY, QQQ and more since 2016, costs charged, every trade listed.
01

Definition

A time-of-day window is a fixed stretch of the trading day, defined by a start and an end on the exchange's clock, inside which a rule is allowed to act. It belongs to the session timing family: tools that add the clock to a chart without adding a price calculation. The window itself makes no prediction. It tints the bars that fall inside it, marks where it opens and closes, and gives every other tool a yes-or-no input: whether this bar is in the part of the day the rule was built for.

The window matters because intraday behaviour is not uniform. Volume and range are highest in the first hour and the last hour and lowest around midday, a shape that holds across equities and index futures. Currencies move with the sessions: the Asian session is slow, the London open brings range, the London and New York overlap brings the most volume. A rule that works at the open can lose all day in the lunch lull. The window is how a trader keeps a signal where it was measured and out of the hours where it is noise.

There is no inventor and no standard setting; the window is whatever part of the day the rule needs. The common ones are the cash session itself, the first hour, the last hour, the London morning and the London and New York overlap. The implementations here take the window as a session input in exchange time, 0930-1600 by default, and evaluate it through the symbol's own time zone, never GMT. The strategy version enters at one clock time and exits at another, one trade per day, which turns a window into a testable, timed position.

On the chart, expect the window to be shaded, a circle under the first bar inside it and another over the first bar after it closes, and no Long or Short marks: the indicator is a building block, not an entry signal. Expect the shading to follow the exchange through daylight saving changes without editing. Expect a window on a 1-hour chart to be coarse, since a bar belongs to the window by its open time. And expect a window that crosses midnight, such as 2200-0200, to behave correctly, because the session function understands it.

02

Reading the chart

The window is chosen from the market's clock, so the work is in knowing the exchange's hours and which part of them the rule needs.

  1. Find the exchange's session times. US equities trade 09:30 to 16:00 New York time; other markets have their own hours. The chart's exchange time zone is the clock the window must use.
  2. Decide which part of the day the rule was built for. A breakout rule belongs near the open; a mean-reversion rule often belongs in the quieter middle; a closing rule belongs in the last hour.
  3. Type the window as start and end in exchange time, for example 0930-1030 for the first hour. If the window crosses midnight, write it in the same form; the session function handles the wrap.
  4. Check the chart timeframe divides the window. On a 5-minute chart a 60-minute window is 12 bars; on a 1-hour chart a 30-minute window is one bar or none, depending on the bar's open time.
  5. Read the tint. Bars inside the window are shaded and the first bar in and the first bar after are circled, so a glance shows which signals fall inside the allowed hours.
  6. Combine it with the signal. The window is a condition, so ask for the signal to be allowed only inside it, or use the strategy version to test a plain timed entry and exit.
03

The maths

The window is a clock test on each bar's open time, evaluated in the exchange's time zone.

int={1start≤clock(t)<end0otherwise\mathrm{in}_t = \begin{cases} 1 & \mathrm{start} \le \mathrm{clock}(t) < \mathrm{end} \\ 0 & \text{otherwise} \end{cases}
opent={1int=1∧int−1=00otherwise\mathrm{open}_t = \begin{cases} 1 & \mathrm{in}_t = 1 \land \mathrm{in}_{t-1} = 0 \\ 0 & \text{otherwise} \end{cases}
closet={1int=0∧int−1=10otherwise\mathrm{close}_t = \begin{cases} 1 & \mathrm{in}_t = 0 \land \mathrm{in}_{t-1} = 1 \\ 0 & \text{otherwise} \end{cases}
filteredt=signalt×int\mathrm{filtered}_t = \mathrm{signal}_t \times \mathrm{in}_t
Rˉh=1nh∑i∈Bh(Hi−Li)\bar{R}_h = \frac{1}{n_h} \sum_{i \in B_h} (H_i - L_i)
clock(t)\mathrm{clock}(t)the open time of bar t in the exchange's time zone
start,end\mathrm{start}, \mathrm{end}the window's boundaries on the same clock
intin_t1 while bar t is inside the window
opent,closet\mathrm{open}_t, \mathrm{close}_tthe window's first bar and the first bar after it
signalt\mathrm{signal}_tany other rule's output on bar t
filteredt\mathrm{filtered}_tthat rule allowed only inside the window
RˉhR̄_hthe average bar range for the bars that open in hour h
BhB_hthe set of bars that open in hour h
nhn_hthe number of those bars
Hi,LiH_i, L_ihigh and low of bar i

The test uses the bar's open time. A 15-minute bar that opens at 09:45 belongs to a 0930-1000 window even though it closes at 10:00, and a bar opening at 10:00 does not.

The exchange clock is the symbol's own time zone, so the same window string means different absolute times for a New York stock and a London share, and each follows its own daylight saving.

A window that crosses midnight is written the same way, for example 2200-0200, and the session function evaluates it correctly across the day boundary.

The strategy version does not use a session string. It compares the bar's hour and minute with an entry time and an exit time. It enters on the first confirmed bar that opens at or after the entry time and closes on the first bar that opens at or after the exit time.

04

Inputs that matter

InputTypicalWhat changing it does
Window (exchange time)0930-1600Narrower windows keep a rule to the hours it was measured in; wider ones admit more signals and more of the midday noise. Type it in the exchange's clock.
Entry hour and minute (strategy)09:30 exchange timeWhen the timed position opens. The entry fires on the first confirmed bar that opens at or after this time and fills on the next bar, so choose a chart timeframe that divides the entry time.
Exit hour and minute (strategy)15:55 exchange timeWhen the position is closed, on the first bar that opens at or after this time. 15:55 needs 1 or 5-minute bars on a cash-session chart; on 15-minute bars set 15:45.
Long (off = short)longThe direction of the timed trade. Off enters short at the entry time instead.
05

In practice

  • As a filter on any rule: the window allows an EMA cross, an RSI turn or a structure break only while the bar is inside it, so the rule trades the hours it was built for and ignores the rest.
  • As a session map for FX: shade the London morning or the London and New York overlap to see at a glance where range and volume live, and where a breakout rule has any chance.
  • As a lunch-lull guard: block the hours around midday for a breakout rule, since narrow-range bars there produce breaks that fail more often than at the open or the close.
  • As a timed-trade test: the strategy version enters at one clock time and exits at another every day, which measures whether a market has a drift in a given part of the session before any signal is added.
  • As a closing-hour tool: a window over the last hour isolates the period where day traders exit and institutions rebalance, which behaves differently from the rest of the session.
  • As a building block for opening ranges: the same window logic, applied to the first minutes of the day, is how the opening range breakout builds its levels.
06

Where it goes wrong

  • Writing the window in GMT or local time. The implementations evaluate it in the exchange's time zone, so a converted window is wrong for part of the year; type exchange-clock times.
  • Using a window smaller than the bar. A 30-minute window on a 1-hour chart catches at most one bar by its open time; choose a chart timeframe that divides the window.
  • Reading the marks as signals. The indicator's circles show where the window opens and closes; they are not entries, and there is no Long or Short mark until a signal is combined with the window.
  • Assuming a timed entry works on its own. A plain long-at-open, exit-at-close test is a baseline; measure it before building a rule on it.
07

Compared with

Opening Range Breakout. The opening range uses a time window to build a price level and then trades the break of it. A time-of-day window is only the clock test; it produces no level and no signal, and it is the input the opening range starts from.

Day-of-Week Effects. A day-of-week filter switches whole days on or off; a time-of-day window switches hours inside each day. They stack: a rule can be allowed only on Tuesday to Thursday and only in the first hour.

VWAP. Session VWAP resets at the session open and measures average price through the day; it is a price level built from a session. A time window is the session boundary itself, with no price calculation attached.

Multi-Timeframe Trend Filter. A higher-timeframe trend filter gates signals by the direction of a slower chart; a time window gates them by the clock. Both are conditions on another signal rather than signals themselves, and they are often used together.

08

Build it in Quant

Type one of these in the Quant desk, or click it. A request that fits a checked template is assembled on the spot without a credit; anything beyond the blanks goes to the copilot, which edits on top of the checked script.

Backtest Time-of-Day Windows as a strategy
Costs and slippage included; every version stays selectable.
09

Questions

  • Which time zone does the window use?

    The exchange's, through the symbol's own time zone setting. A window of 0930-1600 on a US stock is New York time, and on a London share it is London time. The implementations never use GMT, and the window follows the exchange through daylight saving without editing. Type the times as the exchange's clock reads them, and check the symbol's time zone for markets that trade around the clock.

  • What does the indicator draw?

    A shaded background over the bars inside the window, then a circle under the first bar inside it and a circle over the first bar after it has closed. Alerts on those two events are optional. It draws no Long or Short marks, because it is a building block: combine it with a signal to get entries, or use the strategy version for a timed trade.

  • How does the strategy version work?

    It enters at an entry time and exits at an exit time, on the exchange clock, once per day. The entry fires on the first confirmed bar at or after the entry time and before the exit time; the exit closes everything on the first bar at or after the exit time. The default is long from 09:30 to 15:55; a switch makes it short. There is no stop.

  • Why does my 09:35 entry fill at 10:00 on 15-minute bars?

    Because the strategy compares each bar's open time with the entry time and fires on the first confirmed bar at or after it. On 15-minute bars the first bar at or after 09:35 opens at 09:45, is confirmed when it closes at 10:00, and the order fills on the next bar's open. The exit works the same way, so a 15:55 exit needs 1 or 5-minute bars on a cash-session chart. Use a timeframe that divides both times.

  • Can a window cross midnight?

    Yes. Write it in the same form, for example 2200-0200, and the session function evaluates it across the day boundary. That is how an Asian-session window is defined on a New York-time chart. Check where the platform's daily boundary falls for the symbol, because tools that reset daily, such as the opening range, use that boundary.

  • How do I use the window with another indicator?

    Ask for the signal to be allowed only inside the window, for example an EMA cross that counts only between 09:30 and 11:00. The window's yes-or-no output becomes a condition on the other script's entries, so the marks and the alerts appear only in the allowed hours, and a backtest measures the rule in those hours alone.