HomePine Script Snippets › Session Time Filter

Pine Script Session Time Filter

Restricts entries to a configurable trading session using TradingView's time() function. Prevents off-hours fills and overnight holds that violate prop firm rules.

//@version=5
strategy("Session Filter — RTH Only", overlay=true)

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

// ── Session check ────────────────────────────────────────
inSession = not na(time(timeframe.period, sessionInput, timezone))

// ── Entry logic (replace with your own conditions) ───────
longCondition  = ta.crossover(ta.ema(close, 9), ta.ema(close, 21)) and inSession
shortCondition = ta.crossunder(ta.ema(close, 9), ta.ema(close, 21)) and inSession

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

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

// Visual: shade outside-session bars
bgcolor(not inSession ? color.new(color.gray, 92) : na, title="Outside Session")

How to set your session string

TradingView session strings follow the format HHMM-HHMM in the timezone you specify. Common session strings for prop firm traders:

SessionStringTimezone
NY Regular Trading Hours (RTH)0930-1600America/New_York
CME Globex Futures (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 :23456 to the session string — for example, 0930-1600:23456 trades Monday through Friday only.

The time() function returns na when the current bar falls outside the specified session. The not na(...) check converts this into a clean boolean. This is Pine Script's idiomatic way to handle session filtering.

Why prop firms care about overnight holds

Nearly every prop firm evaluation account explicitly prohibits holding positions when the session closes. The reason is straightforward: overnight gaps can produce large unexpected losses in a single bar that blow through the firm's trailing drawdown limit without warning.

Even on funded PA accounts where overnight holds are technically permitted by some firms, the risk profile is dramatically different from RTH trading. Liquidity drops sharply, bid-ask spreads widen, and news events can move prices several hundred ticks before you can react. The session filter is the simplest way to ensure your strategy never opens a position that could still be live when the session ends.

Pair this snippet with the EOD Flatten snippet for belt-and-suspenders protection: the session filter prevents new entries after a cutoff, and the EOD flatten closes any positions that are still open at that time.

Frequently Asked Questions

What is RTH in futures trading?

RTH stands for Regular Trading Hours — the primary cash session for a given market. For equity index futures like MES and MNQ, RTH is 9:30 AM to 4:00 PM Eastern Time. Many prop firms and risk managers prefer strategies that only trade during RTH because liquidity is highest, spreads are tightest, and price action is most predictable.

How do I set a custom session string in Pine Script?

TradingView session strings use the format HHMM-HHMM. For example, 0930-1600 is NY RTH. You can also specify days: 0930-1600:23456 restricts to Monday through Friday. The timezone parameter then tells Pine Script which timezone to interpret those hours in — use America/New_York for US markets.

Why do prop firms care about overnight holds?

Most prop firm evaluation accounts explicitly prohibit holding positions overnight. Overnight gaps can produce large unexpected losses that breach the firm's trailing drawdown in a single move. Even on funded accounts where overnight holds are permitted, the risk profile changes dramatically outside RTH because liquidity drops and gaps are more common.

Related

Want a complete strategy with session filtering pre-wired?

Our scripts include the session filter, kill switch, bar-close entries, and EOD flatten — tested on 3 years of MES/NQ data and ready for TradersPost automation. Instant delivery from $50.

View Plans — From $50