Skip to content
Docs
Open Portal
Copy manually

Your browser did not allow direct clipboard access. Select and copy the text below.

Add a trading-session filter

Limit new entry signals to an explicit time window, without preventing your exit condition from running outside it.

Build signalsLesson 3 of 4Course map

Builds on: Combine conditions

You will allow new entry signals only on candles that open between 09:00 and 17:00 UTC. Exit signals will remain eligible outside that window.

This is a time-filter exercise on your 1-minute BITSTAMP:BTCUSD chart, not a recommended trading schedule. Keep old practice alerts paused and Enable signal alerts off.

Click Copy on the complete script below.

Complete indicator with an entry-session filterPine v6
TradingView → Pine Editor → My EMA practice Pine Script
//@version=6
indicator("Pine practice: sessions", overlay = true)
int length = input.int(20, "Signal EMA length", minval = 1)
int trendLength = input.int(100, "Trend EMA length", minval = 1)
bool useTrendFilter = input.bool(false, "Use trend filter")
bool useSessionFilter = input.bool(false, "Use entry session filter")
string entrySession = input.session("0900-1700", "Entry session")
string sessionTimezone = input.string("Etc/UTC", "Session time zone", options = ["Etc/UTC", "America/New_York", "Asia/Singapore"])
string practiceLabel = input.string("Practice A", "Practice label")
bool enableSignalAlerts = input.bool(false, "Enable signal alerts")
float ema = ta.ema(close, length)
float trendEma = ta.ema(close, trendLength)
bool crossedUp = ta.crossover(close, ema)
bool crossedDown = ta.crossunder(close, ema)
bool trendAllowed = not useTrendFilter or close > trendEma
// This checks the candle's opening time in the selected zone, on all seven days.
bool inEntrySession = not na(time(timeframe.period, entrySession, sessionTimezone))
bool sessionAllowed = not useSessionFilter or inEntrySession
bool entrySignal = crossedUp and barstate.isconfirmed and trendAllowed and sessionAllowed
// Exit signals remain eligible outside the entry window.
bool exitSignal = crossedDown and barstate.isconfirmed
string entryMessage = practiceLabel + " | " + syminfo.ticker + " | long entry signal"
string exitMessage = practiceLabel + " | " + syminfo.ticker + " | long exit signal"
plot(ema, title = "Signal EMA", color = color.blue, linewidth = 2)
plot(useTrendFilter ? trendEma : na, title = "Trend EMA", color = color.orange, linewidth = 2)
bgcolor(useSessionFilter and not inEntrySession ? color.new(color.gray, 88) : na, title = "Outside entry session")
plotshape(entrySignal, title = "Long entry signal", style = shape.triangleup,
location = location.belowbar, color = color.green, size = size.small, text = "Entry")
plotshape(exitSignal, title = "Long exit signal", style = shape.triangledown,
location = location.abovebar, color = color.orange, size = size.small, text = "Exit")
var table preview = table.new(position.top_right, 1, 3, border_width = 1)
if barstate.islast
table.cell(preview, 0, 0, "Message previews", bgcolor = color.blue, text_color = color.white)
table.cell(preview, 0, 1, entryMessage, bgcolor = color.white, text_color = color.black)
table.cell(preview, 0, 2, exitMessage, bgcolor = color.white, text_color = color.black)
if enableSignalAlerts
if entrySignal
alert(entryMessage, alert.freq_once_per_bar_close)
else if exitSignal
alert(exitMessage, alert.freq_once_per_bar_close)
1

Replace and save your practice script

Open Pine, click inside My EMA practice, select all, and paste. Save with Cmd+S / Ctrl+S or the cloud-shaped control, then update the chart if asked.

What you should see

The chart label reads Pine practice: sessions. The familiar Entry and Exit markers are still available.

TradingView right-hand toolbar with the Pine Editor icon highlighted
Enlarge screenshot
Click Pine in the far-right toolbar. This opens the code editor; it does not create a trade.

Click Pine in the far-right toolbar. This opens the code editor; it does not create a trade.

TradingView right-hand toolbar with the Pine Editor icon highlighted
2

Set the entry window explicitly

Open the settings gear for Pine practice: sessions, then select Inputs.

Keep Signal EMA length at 20. Uncheck Use trend filter so you can inspect the time filter on its own.

Check Use entry session filter. Set Entry session to 09:00–17:00 and Session time zone to Etc/UTC. Keep Enable signal alerts unchecked, then click Ok.

What you should see

The entry-session filter is on for 09:00–17:00 in Etc/UTC. Signal alerts are still off.

Session filter settings show a 09:00 to 17:00 entry window in Etc/UTC, with signal alerts off.
Enlarge screenshot
Enable the entry-session filter (1), set 09:00–17:00 (2), and choose Etc/UTC (3). Leave signal alerts unchecked.

Enable the entry-session filter (1), set 09:00–17:00 (2), and choose Etc/UTC (3). Leave signal alerts unchecked.

Session filter settings show a 09:00 to 17:00 entry window in Etc/UTC, with signal alerts off.
3

Compare inside and outside the window

Look through the chart for gray areas and compare their markers with the unshaded areas. An upward cross inside the window is eligible. A downward-cross Exit is eligible on either side of the window.

If your visible chart is entirely inside or outside the window, scroll through earlier candles to find its boundary.

What you should see

Gray background marks candles outside the entry window. They cannot produce Entry signals in this version; Exit signals can still appear there.

A gray outside-session area still contains an Exit marker; an Entry marker appears inside the white session area.
Enlarge screenshot
Gray means outside the entry window. The Exit marker remains eligible there (1); this Entry marker is inside the allowed window (2).

Gray means outside the entry window. The Exit marker remains eligible there (1); this Entry marker is inside the allowed window (2).

A gray outside-session area still contains an Exit marker; an Entry marker appears inside the white session area.

No entry inside the window? The time filter does not generate entries by itself. The confirmed upward cross must still occur. Exit outside the window? That is expected: this is an entry window, not a switch that stops all signals.

The code supplies an explicit timezone to its time check:

The session check and entry conditionExcerpt from the complete script.
Read in your existing script Pine Script
// This checks the candle's opening time in the selected zone, on all seven days.
bool inEntrySession = not na(time(timeframe.period, entrySession, sessionTimezone))
bool sessionAllowed = not useSessionFilter or inEntrySession
bool entrySignal = crossedUp and barstate.isconfirmed and trendAllowed and sessionAllowed
// Exit signals remain eligible outside the entry window.
bool exitSignal = crossedDown and barstate.isconfirmed
  • Etc/UTC means the hours are interpreted in UTC.
  • America/New_York interprets the hours in that region, including daylight-saving changes.
  • Asia/Singapore interprets them in Singapore time.

Changing the chart’s displayed timezone does not change this input or Pine’s session calculation. Use the explicit Session time zone field to change the rule. TradingView’s time guide

The example checks the candle’s opening time. On the 1-minute chart, a candle opening at 16:59 UTC is inside this window; one opening at 17:00 UTC is outside it. The signal itself still waits for the eligible candle to close. Keep session boundaries aligned with your chosen candle interval. This example has no weekday restriction, so its time window applies on all seven days. TradingView’s session guide

Nothing in this script says “close a position at 17:00.” Its exit rule still requires a confirmed downward cross. It also does not know whether the broker’s market is open or whether a broker position exists.

To express a different rule, you would need to change the exit condition deliberately. Do not put the session filter around both entry and exit calls merely to shorten the code; that would prevent these exit signals outside the window.

Question: an Entry condition occurs on a candle opening at 17:00 UTC. Will this version mark it as Entry?

Show the answer

No. The 09:00–17:00 entry window has ended. The upward-cross condition alone is not enough.

Question: an Exit condition occurs at 18:00 UTC. Is it blocked by the entry window?

Show the answer

No. The exit condition is evaluated separately and does not include the session filter.

Try: turn Use entry session filter off, compare the same earlier candles, then turn it back on. Leave Enable signal alerts off. The next lesson uses these conditions to compare two ways of configuring indicator alerts.