TradingCharts

Time

All times inside ChartScript are expressed as epoch milliseconds, UTC — a 64-bit integer. Chart display formats are a presentation concern; the script works with raw numbers.

Built-in time series

Name Meaning
time The bar's open time (ms UTC).
time_close The bar's close time (ms UTC).
timenow Wall-clock time (ms UTC) — the executor's current moment.
last_bar_time Open time of the chart's last loaded bar.

Historical access works as expected: time[1], time_close[5].

time(timeframe, session?, timezone?)

Returns the start time of the bar's bucket at the requested timeframe. Reset points: ta.change(time("D")) != 0 is true exactly once per calendar day.

isNewDay   = ta.change(time("D")) != 0
isNewWeek  = ta.change(time("W")) != 0
isNewMonth = ta.change(time("M")) != 0

if isNewDay
    log.info("New session: " + str.format_time(time, "yyyy-MM-dd"))

With a session argument, the result is na outside the session window:

inRth = not na(time("", "0915-1530"))

With a timezone argument, the session is interpreted in that zone:

inLondon = not na(time("", "0800-1700", "Europe/London"))

time_close(timeframe, session?, timezone?)

Mirror of time(timeframe, …) but returns the bucket's close time.

daysLeft = (time_close("D") - timenow) / (60 * 1000)   // minutes until EOD

timestamp() — build a time

timestamp(year, month, day, hour?, minute?, second?)        // IST
timestamp("tz", year, month, day, hour, minute, second)     // explicit timezone
timestamp("01 Jan 2020 09:15 +0530")                        // free-form string

The string form is handy for input defaults:

startTime = input.time(timestamp("01 Jan 2020 00:00 +0530"), "Start")

Decomposition

Function Returns
year(t?) int (defaults to the current bar)
month(t?) int (1–12)
dayofmonth(t?) int (1–31)
dayofweek(t?) int (1=Sunday … 7=Saturday); members dayofweek.sunday/.monday/…
hour(t?) int (0–23)
minute(t?) int (0–59)
second(t?) int (0–59)
weekofyear(t?) ISO week number

An optional second argument lets you request a specific timezone:

hourIST = hour(time, "Asia/Kolkata")   // same as plain hour(time): IST is the default

Formatting

See Strings: str.format_time(epochMs, format, timezone?).

str.format_time(time, "yyyy-MM-dd HH:mm:ss", "Asia/Kolkata")

Caveats

  • time on a historical bar is immutable. time on the live forming bar updates on every tick (until the bar closes).
  • timenow ticks faster than time during the live pass — it is the wall clock, not the bar's clock.

See also