Skip to content

time_close() - Pine Script Function

time_close() mirrors time() but returns the expected close timestamp for matching bars. This is helpful when you want to mark the end of a session or schedule actions at close.

On realtime bars for tick- or price-based charts (Renko, Line Break, etc.), the close time is unknown until the bar finishes, so the function returns na until the bar is confirmed.

time_close(timeframe, session, timezone, bars_back) → series int

Same as time():

NameTypeDescription
timeframeseries string (optional)Target timeframe (empty string inherits the chart).
sessionseries string (optional)Session filter such as "1200-1300" or "0930-1600:America/New_York".
timezoneseries string (optional)Override timezone without editing the session string.
bars_backseries int (optional)Look-back offset used primarily for higher-timeframe calculations.

series int — Bar-close UNIX time in milliseconds, or na when outside session or on unpredictable realtime bars.

  • Comparing time_close() against timenow lets you estimate time remaining in the current session.
  • For reliable alerts near the session close, combine time_close() with time() and boolean guards to avoid duplicate triggers.
//@version=5
indicator("Session close countdown", overlay=false)
sessionInput = input.session("1300-1700", "Active window", confirm=true)
tzInput = input.string(syminfo.timezone, "Timezone")
sessionClose = time_close("", sessionInput + ":" + tzInput)
secondsLeft = math.max(0, (sessionClose - timenow) / 1000)
plot(secondsLeft, "Seconds until close", color=color.orange)
hline(0, "Close", color=color.gray)