Date and time/probuilder · probacktest · proorder · proscreener

Days

Days returns the whole number of days elapsed since 1 January 1900 up to a bar. Days accepts a bracket offset such as Days[1] and helps measure date differences.

Syntax

probuilder
Days[N]

How it works

Days returns a single monotonic integer: the count of calendar days from the reference date of 1 January 1900 up to the date of the bar being processed. Because it collapses the full date into one number that only ever increases, it is the natural tool for measuring the span between two bars. The difference Days[0] - Days[N] is the number of calendar days that separate the current bar from the bar N positions back.

The count includes every calendar day, weekends and holidays alike, so it measures elapsed time rather than trading sessions. This distinguishes it from a plain bar count, which skips the days on which the market is closed.

The optional index N selects a bar relative to the one being processed. Days[0], or plain Days, is the current bar. A positive N looks back: Days[1] is the previous bar. The chart must hold enough history for the requested offset, otherwise the value is undefined at the left edge.

Examples

Example 1, Days elapsed at the current bar (Indicator)

probuilder
// Number of days since 1 January 1900 up to the current bar
myDays = Days[0]
RETURN myDays

The indicator plots a slowly rising line, incrementing by the number of calendar days between successive bars. Gaps over weekends and holidays show as larger steps.

Example 2, Scan for a fresh calendar day (ProScreener)

probuilder
// Match instruments whose latest bar is on a different calendar day from the previous bar
newDay = Days > Days[1]
SCREENER[newDay AND close > Average[20](close)] (close AS "Close")

On an intraday chart Days > Days[1] is true only on the first bar of a new calendar day, so the screener isolates the session's opening bar.

Example 3, Days-in-trade filter (ProOrder)

probuilder
// Close the position once it has been held for at least five calendar days
DEFPARAM CumulateOrders = false
IF NOT LONGONMARKET AND close CROSSES OVER Average[20](close) THEN
    BUY 1 CONTRACT AT MARKET
    entryDays = Days
ENDIF
IF LONGONMARKET AND (Days - entryDays) >= 5 THEN
    SELL AT MARKET
ENDIF

The strategy stores the Days value at entry, then exits once five calendar days have passed, counting weekends and holidays because Days measures elapsed calendar time.

Common errors and gotchas

  • Reference date is 1 January 1900. The absolute number is only meaningful as a difference. Subtract two Days values to get a span in days.
  • Calendar days, not trading days. Days counts every calendar day, so a Monday bar following a Friday bar differs by 3, not 1. It is not a bar count.
  • Not a formatted date. Days is a running count, not a YYYYMMDD date. For a readable date use Today or combine Day, Month and Year.
  • Offset beyond history. Days[N] needs N bars of history. Near the left edge of the chart the value is undefined.
  • Day, day of the month of each bar, 1 to 31.
  • DayOfWeek, weekday of each bar, 1 for Monday to 7 for Sunday.
  • Month, month of each bar.
  • Year, four-digit year of each bar.
  • Timestamp, UNIX time of the close of each bar, an alternative monotonic clock.
  • TIME, closing time of each bar as an HHMMSS integer.
  • Yesterday, date of the day before a bar, in YYYYMMDD form.
  • Today, date of the current bar in YYYYMMDD form.