Year
Year returns the four-digit year of a bar on the chart, such as 2026. Year accepts a bracket offset such as Year[10] to read the year of an earlier bar.
Syntax
Year[N]How it works
Year returns the calendar year of the bar being processed, as a four-digit integer such as 2026. It reads the date attached to each bar, so it changes along the time axis: bars from 2024 return 2024, bars from 2025 return 2025, and the value increments at each turn of the year.
The optional index N selects a bar relative to the one being processed. Year[0], or plain Year, is the current bar. A positive N looks back: Year[10] is the year of the bar ten positions earlier. The chart must hold enough history for the requested offset, otherwise the value is undefined at the left edge. Year only works on time-based charts, where each bar carries a date.
Year is the per-bar year. Its siblings are CurrentYear, which reads the year of the clock at execution time and is identical on every bar, and OpenYear, which reads the year at the open of each bar. Pair Year with Month and Day when you need to test a full calendar date.
Examples
Example 1, Year of an earlier bar (Indicator)
// Retrieve the year of the bar exactly 10 periods before the current bar
myYear = Year[10]
RETURN myYearThe indicator plots the year of the bar ten positions back at each point, a stepped line that jumps at each year boundary shifted ten bars to the right.
Example 2, Scan by year (ProScreener)
// Match instruments whose latest bar falls in 2026
thisYear = Year = 2026
SCREENER[thisYear AND close > Average[50](close)] (Year AS "Year")The screener returns only instruments whose most recent bar is dated 2026, with the year shown alongside, useful when a chart's last bar may lag behind the calendar.
Example 3, Year-change entry gate (ProOrder)
// Allow entries only on the first bar of a new year
DEFPARAM CumulateOrders = false
newYear = Year > Year[1]
IF newYear AND close CROSSES OVER Average[20](close) THEN
BUY 1 CONTRACT AT MARKET
ENDIFThe condition Year > Year[1] is true on the first bar of a new calendar year, so the strategy uses it as an annual entry gate for seasonal ideas.
Common errors and gotchas
- Four-digit year. Year returns a full year such as 2026, not a two-digit form. Comparisons must use the whole number.
- Time-based charts only. Year needs bars that carry a date. On non-time charts the value is not meaningful.
- Per-bar, not the clock. Year reads each bar's date and changes along the axis. For the year of the moment the code runs, use
CurrentYear. - Offset beyond history.
Year[N]needsNbars of history. Near the left edge of the chart the value is undefined.
Related instructions
CurrentYear, year of the clock at execution time.OpenYear, year at the open of each bar.Month, month of each bar, pairs with Year for a fuller date.Day, day of the month of each bar.DayOfWeek, weekday of each bar, 1 for Monday to 7 for Sunday.Days, whole days elapsed since 1 January 1900 up to each bar.TIME, closing time of each bar as an HHMMSS integer.Timestamp, UNIX time of the close of each bar.
