Pine Script Session Time Filter

Restricts entries to a configurable trading session using TradingView's time() function — keeps a strategy out of thin overnight action and off-hours fills.

Pine Script v5 Free · TradingView free plan
Pine Script
//@version=5
strategy("Target Filled - Session Filter Example", overlay=true)

// --- Inputs ---------------------------------------------------
sessionWindow = input.session("0930-1600", "Trading Session",
     tooltip="TradingView session format. Default = NY RTH, 9:30-16:00 ET.")
sessionZone   = input.string("America/New_York", "Timezone")

// --- Session check --------------------------------------------------
withinSession = not na(time(timeframe.period, sessionWindow, sessionZone))

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

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: shade bars outside the session
bgcolor(not withinSession ? color.new(color.gray, 92) : na, title="Outside Session")

How to set your session string

TradingView session strings follow the format HHMM-HHMM in whatever timezone you pass alongside them. Common strings for a prop firm trader:

SessionStringTimezone
NY Regular Trading Hours (RTH)0930-1600America/New_York
CME Globex (full overnight)1800-1700America/New_York
CME futures, RTH only0830-1500America/Chicago
London session0800-1630Europe/London
Asian session (Tokyo)0900-1530Asia/Tokyo
NY morning only (first 2 hours)0930-1130America/New_York

To restrict to weekdays only, append a day list to the string — 0930-1600:23456 trades Monday through Friday only.

Why the not na(...) wrapper. time() returns na whenever the current bar falls outside the given session, so wrapping it in not na(...) turns that into a clean boolean gate — the idiomatic way to do session filtering in Pine.

Why prop firms care about overnight holds

Nearly every evaluation account explicitly bans holding a position once the session closes. The reasoning is simple: an overnight gap can produce a loss large enough to blow through a trailing drawdown limit in one move, with no bar-by-bar warning along the way.

Even on funded accounts where overnight holds are technically permitted, the risk profile shifts a lot outside RTH — liquidity drops, spreads widen, and a single headline can move price hundreds of ticks before there's any chance to react. A session filter is the cleanest way to guarantee a strategy never opens something that could still be live at the close.

Pair this with the EOD Flatten snippet for belt-and-suspenders coverage: the session filter stops new entries after the cutoff, and EOD flatten closes anything still open once the session ends.

Frequently Asked Questions

What is RTH in futures trading?
RTH stands for Regular Trading Hours — the main cash session for a market. For equity index futures like MES and MNQ, RTH runs 9:30 AM to 4:00 PM Eastern. Many prop firms and risk-conscious traders prefer strategies confined to RTH because liquidity peaks, spreads tighten, and price action gets more orderly during that window.
How do I set a custom session string in Pine Script?
Session strings follow the pattern HHMM-HHMM0930-1600 means NY RTH. Append a day-of-week list like :23456 to restrict to Monday through Friday. The separate timezone argument then tells time() which clock those hours are measured against, typically America/New_York for US markets.
Why do prop firms care about overnight holds?
Most evaluation accounts explicitly forbid holding a position past the session close. A gap at the open can blow through a trailing drawdown limit in a single bar, with no chance to react. Even where overnight holds are technically allowed on a funded account, liquidity thins out and spreads widen enough that the risk profile changes substantially outside RTH.

Get a Pine Script tuned to your prop firm.

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