Date and time/probuilder · probacktest · proorder · proscreener

DayOfWeek

DayOfWeek returns the weekday of a bar, 1 for Monday to 7 for Sunday. DayOfWeek accepts a bracket offset such as DayOfWeek[1] to read an earlier bar.

Syntax

probuilder
DayOfWeek[N]

How it works

DayOfWeek returns the weekday of the bar being processed, as an integer from 1 to 7. Monday is 1, Tuesday 2, and so on through to Sunday, which is 7. It reads the calendar date attached to each bar, so it changes along the time axis: on a daily chart the value walks 1, 2, 3, 4, 5 across a trading week and skips the days on which the market is closed.

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

DayOfWeek is the per-bar weekday. Its siblings are CurrentDayOfWeek, which reads the weekday of the clock at execution time, and OpenDayOfWeek, which reads the weekday at the open of each bar. Because most markets do not trade every calendar day, weekend values rarely appear on a chart of a single instrument.

Examples

Example 1, Monday flag (Indicator)

probuilder
// Flag bars that fall on a Monday
myDay = DayOfWeek[0]
IF myDay = 1 THEN
    isMonday = 1
ELSE
    isMonday = 0
ENDIF
RETURN isMonday

The indicator plots 1 on every Monday bar and 0 on the rest of the week, a quick way to see where each trading week begins.

Example 2, Scan for Friday bars (ProScreener)

probuilder
// Match instruments whose latest bar is a Friday
isFriday = DayOfWeek = 5
SCREENER[isFriday AND close > Average[20](close)] (DayOfWeek AS "Weekday")

The screener returns only instruments whose most recent bar falls on a Friday, with the weekday number shown for reference.

Example 3, Avoid Monday entries (ProOrder)

probuilder
// Skip new entries on Mondays, take them on other weekdays
DEFPARAM CumulateOrders = false
notMonday = DayOfWeek <> 1
IF notMonday AND close CROSSES OVER Average[20](close) THEN
    BUY 1 CONTRACT AT MARKET
ENDIF

The strategy declines fresh entries on Monday bars and takes signals on the rest of the week, a common filter for systems that treat the first day of the week as noisy.

Common errors and gotchas

  • Monday is 1, Sunday is 7. The numbering runs 1 to 7 from Monday. A test written as if Sunday were 0 will be off by one.
  • Weekend bars are rare. On most instruments the market is closed at weekends, so values 6 and 7 seldom appear. Do not rely on them being present.
  • Per-bar, not the clock. DayOfWeek reads each bar's date. For the weekday of the moment the code runs, use CurrentDayOfWeek.
  • Offset beyond history. DayOfWeek[N] needs N bars of history. Near the left edge of the chart the value is undefined.
  • CurrentDayOfWeek, weekday of the clock at execution time.
  • OpenDayOfWeek, weekday at the open of each bar.
  • Day, day of the month of each bar, 1 to 31.
  • Month, month of each bar.
  • Year, four-digit year of each bar.
  • Days, whole days elapsed since 1 January 1900 up to each bar.
  • TIME, closing time of each bar as an HHMMSS integer.
  • Today, date of the current bar in YYYYMMDD form.