Docs

ITMScript

Time and sessions

The calendar fields and session facts a study can read, how a total restarts each session, week or month, and how to read monthly, quarterly and yearly bars.

Every bar has a place in time. A study can read where it is, and it can start a running total again when a new period begins. This page covers the calendar fields, the session facts, the period words, session and weekly VWAP, and the monthly clocks.

Calendar fields

A calendar field is a value the chart knows about each bar. Write its name where you would write close.

FieldReads
timeThe label of the bar, in milliseconds since 1970.
yearThe year of the bar's date.
monthThe month of the bar's date, 1 to 12.
dayofmonthThe day of the month, 1 to 31.
dayofweekThe day of the week. 1 is Sunday and 7 is Saturday.
hourThe hour on the exchange's wall clock, 0 to 23.
minuteThe minute on the exchange's wall clock, 0 to 59.

On a bar finer than a day, the date is the date at the exchange. On a bar of a day or longer, it is the date of the bar's label. On a 24/7 calendar every date and every clock reading is in UTC. The hour and minute of a daily or coarser bar are missing, because the bar spans the whole day.

The fields follow the chart's own bars. They do not read ahead and they do not depend on where the script starts. A name you bind yourself, such as hour = 3, is read before the field of the same name.

itm
//@itmscript 2
study("Wednesday closes", overlay=true)
midweek = dayofweek == 4
plot(midweek ? close : na, "Wednesday close", color=color.orange, width=2, style=circles)

Session facts

Five more fields say where the bar sits in the trading day. Four are conditions and one is a count.

FieldReads
session.ismarketTrue while the regular session is open.
session.ispremarketTrue in the five and a half hours before the open.
session.ispostmarketTrue in the four hours after the close.
session.isfirstbarTrue on the bar that holds the open.
session.islastbarTrue on the bar that holds the close.
bars_since_openThe number of bars since the open, 0 on the first bar. Missing outside the regular session.

A condition is not a number. Use it to choose a value, as in session.ismarket ? volume : na.

itm
//@itmscript 2
study("Morning volume", overlay=false)
morning = session.ismarket and hour < 11
plot(morning ? volume : na, "Morning volume", color=color.blue, width=1, style=histogram)
itm
//@itmscript 2
study("Opening bars", overlay=true)
opening = session.ismarket and bars_since_open < 3
plot(opening ? high : na, "Opening high", color=color.green, width=1, style=circles)
plot(opening ? low : na, "Opening low", color=color.red, width=1, style=circles)

On a daily or coarser bar the session facts describe the day. A bar on a trading day is the session. session.ismarket, session.isfirstbar and session.islastbar are true, bars_since_open is 0, and the pre-market and post-market facts are false. A bar on a closed day has all five facts false and bars_since_open missing.

On a 24/7 calendar the session is the UTC day. session.ismarket is always true and the pre-market and post-market facts are always false. bars_since_open counts from 00:00 UTC.

Early closes and holidays follow the exchange calendar that comes with the chart. A chart without it assumes weekdays from 09:30 to 16:00 in New York and no holidays.

Restart on a period

A scan can start again when a new period begins. Add reset= and one of six words.

WordA new period starts on the first bar of
sessioneach trading day of the exchange calendar.
dayeach date.
weekeach week, which starts on Monday.
montheach calendar month.
quartereach calendar quarter.
yeareach calendar year.

The word is written bare. If your script binds a name of the same word, the name is read as a series instead. A reset may also be a condition, as before. A condition and a word together are refused.

itm
//@itmscript 2
study("Volume since the open", overlay=false)
add(acc, x) => nz(acc, 0) + x
sinceOpen = scan(volume, na, add, reset=session)
sinceMonday = scan(volume, na, add, reset=week)
plot(sinceOpen, "Since the open", color=color.blue, width=2, style=line)
plot(sinceMonday, "Since Monday", color=color.gray, width=1, style=line)

session never restarts on a daily or coarser chart, and never on a 24/7 calendar, because there is no session to tell apart. The other five words work on every chart. A bar as long as the period, or longer, starts a period of its own. reset=day on a daily chart restarts on every bar.

A reset reads back to the start of the period. A longer period asks for more history. A reset by year can need as many as 366 daily bars.

Session and weekly VWAP

ta.vwap takes the same six words. Without one, it weights every bar loaded and never restarts. With reset=session it starts again at each open. With reset=week it starts again on Monday.

itm
//@itmscript 2
study("Anchored VWAP", overlay=true)
sessionVwap = ta.vwap(hlc3, reset=session)
weekVwap = ta.vwap(hlc3, reset=week)
plot(sessionVwap, "Session VWAP", color=color.orange, width=2, style=line)
plot(weekVwap, "Weekly VWAP", color=color.blue, width=1, style=line)

The VWAP bands recipe draws bands around it. On the first bar of a period the VWAP equals that bar's hlc3, because it holds nothing else yet.

Totals by period

Three more operators take the same reset option. Each starts again on the first bar of the new period.

OperatorWhat restarts
ta.cumThe running total.
ta.cumretThe base the return is measured from. The next value that is not missing becomes the new base.
ta.drawdownThe peak, the worst drawdown and the duration.
itm
//@itmscript 2
study("Month to date", overlay=false)
volumeToDate = ta.cum(volume, reset=month)
returnToDate = ta.cumret(close, reset=month)
plot(volumeToDate, "Volume this month", color=color.blue, width=1, style=area)
plot(returnToDate, "Return this month", color=color.green, width=2, style=line)
itm
//@itmscript 2
study("Drawdown this year", overlay=false)
plot(ta.drawdown(close, reset=year), "Drawdown", color=color.red, width=2, style=area)

A word that is not one of the six is refused, and the message lists them.

itm
//@itmscript 2
study("Bad reset", overlay=false)
plot(ta.cum(volume, reset=fortnight), "Total", color=color.blue)

Month clocks

A study can read monthly, quarterly and yearly bars. The clocks are 1mo, 1q and 1y. Three months is 1q and twelve months is 1y, so 3mo and 1q name the same clock. The token 1m is one minute, whatever its case. A month is always 1mo.

Write one in place, as with any other clock.

itm
//@itmscript 2
study("Monthly close", overlay=true)
plot(close@1mo, "Last month close", color=color.purple, width=2, style=stepline)

Or declare the series once and read its fields.

itm
//@itmscript 2
study("Quarterly range", overlay=false)
q = series(timeframe="1q")
plot(q.high - q.low, "Quarter range", color=color.gray, width=2, style=stepline)
plot(ta.sma(q.close, 4), "Four quarter average", color=color.blue, width=2, style=line)

A monthly bar is built from the daily bars of one calendar month. It closes at the end of the month's last session. Until then, close@1mo shows the last month that has finished. The bar that is still forming is never read, as with every other coarser clock. The other data page explains when a coarser value becomes visible.

A window counts bars of its own series. ta.sma(q.close, 4) is the average of four quarters. A clock that names a calendar or a session, such as @XNYS/1mo, is not part of the language and is refused.

Drawn ahead

A plot drawn ahead with offset= puts its last values after the last bar. Those places are the chart's next bars on its own clock. On an intraday chart they run to the end of the session and then into the next session. On a daily chart they are the next trading days. The outputs page covers offset=.

What the calendar does not do

The calendar says where a bar sits. It does not turn a stated duration into a bar count. A duration such as 20d is refused in an expression. Write the window as a number of bars, or read a series whose bar is the unit, such as a daily series.

itm
//@itmscript 2
study("Twenty days", overlay=true)
plot(ta.sma(close, 20d), "Average", color=color.blue)

A calendar field is not a field of a declared series. d.hour is refused. A calendar field always describes the chart's own bar.