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.
//@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:
| Session | String | Timezone |
|---|---|---|
| NY Regular Trading Hours (RTH) | 0930-1600 | America/New_York |
| CME Globex (full overnight) | 1800-1700 | America/New_York |
| CME futures, RTH only | 0830-1500 | America/Chicago |
| London session | 0800-1630 | Europe/London |
| Asian session (Tokyo) | 0900-1530 | Asia/Tokyo |
| NY morning only (first 2 hours) | 0930-1130 | America/New_York |
To restrict to weekdays only, append a day list to the string — 0930-1600:23456 trades Monday through Friday only.
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?
How do I set a custom session string in Pine Script?
HHMM-HHMM — 0930-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.