Sessions and intervals

Two groups of names sit close together here, and they behave differently.

The interval values describe the chart your script is running on. Every one of them is simple: the chart’s interval does not change while a script runs. Read one wherever you like and the answer is the same on every bar.

The session values describe where the current bar falls. Both are series: they change as your script moves through the bars.

The chart’s interval

Three values describe the interval itself.

NameTypeHolds
interval.intervalsimple stringThe chart’s interval
interval.multipliersimple intThe interval’s multiplier
interval.timeframe_minutessimple intThe interval in minutes

Seven booleans answer what kind of interval it is.

NameTypeTrue when the interval is
interval.isintradaysimple boolIntraday
interval.isminutessimple boolIn minutes
interval.ishourlysimple boolHourly
interval.isdailysimple boolDaily
interval.isweeklysimple boolWeekly
interval.ismonthlysimple boolMonthly
interval.iseodsimple boolEnd of day

Because these are simple rather than series, a test against one of them gives the same answer on every bar of the run.

interval.isintraday

The session

NameTypeHolds
session.dateseries stringThe session’s date
session.isfirstseries boolWhether this is the first bar of the session

session.isfirst marks the first bar of a session. Other parts of the language lean on it. The cumulative delta function reads it to decide when to restart, seeding its running total from zero on a session’s first bar rather than carrying the previous bar’s total forward. Anything of yours that has to reset once a session is the same shape of problem.

The bar’s time values

NameTypeHolds
timeseries intThe bar’s time
time_closeseries intThe bar’s closing time
timenowseries intThe current time

Seven more int series name the parts of a date and a time on the current bar.

NameType
yearseries int
monthseries int
dayofmonthseries int
dayofweekseries int
hourseries int
minuteseries int
secondseries int

Each of those seven names also exists as a function, which is the subject of the next section. The variable reads the current bar. The function reads whatever time value you hand it.

Taking a time apart

Eight functions take a time and a timezone and return an int series. Both arguments are required. Seven of them share their names with the variables above; weekofyear has no variable form.

year(series int time, series string timezone) → series int
month(series int time, series string timezone) → series int
weekofyear(series int time, series string timezone) → series int
dayofmonth(series int time, series string timezone) → series int
dayofweek(series int time, series string timezone) → series int
hour(series int time, series string timezone) → series int
minute(series int time, series string timezone) → series int
second(series int time, series string timezone) → series int

The time argument takes an int in any form, so the bar’s own time is accepted, and so is a constant. The timezone argument takes a string in any form. The chart instrument’s timezone is available as syminfo.timezone, described in Instrument properties.

hour(time, syminfo.timezone)

Building a time value

timestamp goes the other way, assembling a time from its parts. It has two forms. Give it series arguments and you get an int series back. Give it simple arguments and you get a simple int back.

timestamp(series int year, series int month, series int day, series int hour, series int minute, series int second) → series int
timestamp(simple int year, simple int month, simple int day, simple int hour, simple int minute, simple int second) → simple int

In both forms the year, the month and the day are required. The hour, the minute and the second may be left out.

A script that plots the bar’s hour and minute

indicator("Bar hour and minute")
plot(hour)
plot(minute)

Both plot calls sit at the top level of the script. Chart-output functions have to be called there: a call placed inside a branch, inside a loop or inside a function of your own is rejected.

Ready to put this into practice?
Try GoCharting Premium
Unlock advanced orderflow, market profile, options desk, and real-time data — everything you just read about, live in your charts.
Upgrade