Date and time/probuilder · probacktest · proorder · proscreener

OpenYear

OpenYear returns the year in which the current bar's trading session opened, as a four-digit integer. Useful for annual filters in ProBuilder scripts.

Syntax

probuilder
OpenYear

The keyword takes no parameters. A history index in square brackets reads earlier bars, so OpenYear[1] gives the opening year of the previous bar.

How it works

Each bar records the date and time at which it started forming. OpenYear extracts the year component of that opening moment and returns it as a four-digit integer. The value is not encoded inside a larger date number, so it can be compared directly against literals such as 2020 without any decoding.

The keyword belongs to the family of open-based date components, alongside OpenMonth, OpenDay, and OpenTime, all of which describe when the bar opened. The close-based counterpart is Year, which reads the year of the bar date, and CurrentYear, which reads the year of the wall clock rather than of any bar.

Typical uses include restricting a backtest to recent history, splitting statistics by calendar year, and detecting the first bar of a new year by comparing the value against the previous bar.

Examples

Example 1, Plotting the opening year of each bar (Indicator)

probuilder
// Store the year in which the current bar's session opened
yearOpened = OpenYear
RETURN yearOpened

The indicator returns the opening year of every bar, which makes year boundaries visible as steps in the plotted line.

Example 2, Limiting a strategy to recent years (ProBacktest)

probuilder
// Only evaluate entries on bars that opened in 2020 or later
IF OpenYear >= 2020 THEN
  IF close CROSSES OVER Average[100](close) THEN
    BUY 1 SHARES AT MARKET
  ENDIF
ENDIF

The year filter excludes older history from the backtest even when a longer data range is loaded, keeping results focused on the chosen period.

Example 3, Detecting the first bar of a new year (ProScreener)

probuilder
// Match instruments printing their first bar of a new calendar year
newYear = OpenYear > OpenYear[1]
SCREENER[newYear] ("New year")

Comparing the opening year of the current bar with that of the previous bar isolates the single bar on which the year changes.

Common errors and gotchas

  • Comparing against full dates. OpenYear returns 2023, not 20230101. Comparing it against YYYYMMDD literals silently evaluates to false. Full-date comparisons belong to OpenDate or Today.
  • Open versus close year. On bars that span midnight of 31 December, the opening year and the closing year differ. Year follows the bar date while OpenYear follows the open, so the two are not interchangeable around year boundaries.
  • Wall clock versus bar. CurrentYear reads the computer clock and is the same on every bar during a run. OpenYear varies bar by bar. Using CurrentYear in a historical filter makes the whole history pass or fail at once.
  • CurrentYear, year of the current wall-clock date.
  • Year, year of the bar N periods before the current one.
  • OpenMonth, month in which the bar opened.
  • OpenDay, day of the month on which the bar opened.
  • OpenDate, full opening date of the bar in YYYYMMDD format.
  • OpenTime, opening time of the bar as an HHMMSS integer.
  • Today, date of the current bar in YYYYMMDD format.