time_close() - Pine Script Function
time_close()
Section titled “time_close()”Overview
Section titled “Overview”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.
Syntax
Section titled “Syntax”time_close(timeframe, session, timezone, bars_back) → series intParameters
Section titled “Parameters”Same as time():
| Name | Type | Description |
|---|---|---|
timeframe | series string (optional) | Target timeframe (empty string inherits the chart). |
session | series string (optional) | Session filter such as "1200-1300" or "0930-1600:America/New_York". |
timezone | series string (optional) | Override timezone without editing the session string. |
bars_back | series int (optional) | Look-back offset used primarily for higher-timeframe calculations. |
Returns
Section titled “Returns”series int — Bar-close UNIX time in milliseconds, or na when outside session or on unpredictable realtime bars.
Remarks
Section titled “Remarks”- Comparing
time_close()againsttimenowlets you estimate time remaining in the current session. - For reliable alerts near the session close, combine
time_close()withtime()and boolean guards to avoid duplicate triggers.
Example
Section titled “Example”//@version=5indicator("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)