Returns the opening time of the current bar or period, or na outside the trading session you give.
time(timeframe, session) is the standard way to test whether a bar falls inside a time window. It returns a UNIX timestamp in milliseconds when the bar qualifies and na when it does not, so not na(time(timeframe.period, "0930-1600")) is true during those hours. The one-argument form time("D") is the usual way to read a higher-timeframe period start.
With a timeframe longer than the chart's, the result is the opening time of the higher-timeframe period that contains the bar, so every bar of one day returns the same value for "D". Compare it with its previous value to catch the first bar of that period.
Syntax
time(timeframe: series string, session: series string (optional), bars_back: series int (optional), timeframe_bars_back: series int (optional)) -> series int
time(timeframe: series string, session: series string, timezone: series string, bars_back: series int (optional), timeframe_bars_back: series int (optional)) -> series intArguments
| Name | Type | Default | Description |
|---|---|---|---|
| timeframe | series string | required | The timeframe of the period to look up, such as "D", "W", "60" or timeframe.period for the chart's own. A timeframe shorter than the chart's is treated as the chart's. |
| session | series string | every bar qualifies | The session as text, such as "0930-1600" or "0930-1600:23456". When left out, or empty, every bar qualifies and the call only changes the timeframe. |
| bars_back | series int | 0 | How many bars back to look for the time; negative values look into the future where the exchange calendar allows it. |
| timeframe_bars_back | series int | 0 | Counts back in bars of the given timeframe instead of chart bars. |
| timezone | series string | required | The time zone the session is read in. Accepts the same names as the timezone argument of timestamp(). |
Returns
series int: The UNIX timestamp in milliseconds of the bar or period start, or na when the bar is outside the session.
Remarks
- A session string is HHMM-HHMM in the exchange time zone; add a colon and day digits to limit the days, as in "0930-1600:23456" (1 is Sunday, 7 is Saturday). Separate several ranges with commas. A range whose end is before its start runs overnight.
- The bar's open time decides membership, so a 5-minute bar opening at 15:55 is in "0930-1600" and the bar opening at 16:00 is not.
- On Sidechair: bars_back and timeframe_bars_back are accepted and ignored. With timeframe left empty, the chart's timeframe is used.
- Not the same as the time variable, which is the plain open time of every bar.
Examples
//@version=6
indicator("Regular session", overlay = true)
inSession = not na(time(timeframe.period, "0930-1600"))
bgcolor(inSession ? color.new(color.green, 92) : color.new(color.gray, 92))
plot(inSession ? close : na, "Close in session", color.green)//@version=6
indicator("Day open", overlay = true)
dayStart = time("D")
newDay = dayStart != dayStart[1]
plotshape(newDay, "Day open", shape.triangleup, location.belowbar, color.blue, size = size.tiny)See also
Runs on Sidechair