Skip to content

weekofyear() - Pine Script Function

weekofyear() calculates the ISO week number for a given UNIX timestamp. By default the function interprets the timestamp in the chart’s exchange timezone, but you can override it with any UTC offset or IANA timezone string.

weekofyear(time, timezone) → series int
NameTypeDescription
timeseries intUNIX timestamp in milliseconds—typically the built-in time series.
timezoneseries string (optional)Target timezone ("UTC+1", "America/New_York", etc.). Defaults to syminfo.timezone.

series int — Calendar week number between 1 and 53.

  • Use time_tradingday when you want the week number of the trading session instead of the bar open (helpful on overnight markets).
  • Combine with year() to build composite keys (e.g., year(time) * 100 + weekofyear(time)).
  • Week definitions follow ISO rules: weeks start on Monday and the first week is the one containing the year’s first Thursday.
//@version=5
indicator("Week-of-year filter", overlay=true)
allowedWeeks = input.string("1,13,26,39,52", "Trade weeks")
currentWeek = weekofyear(time)
shouldTrade = str.contains("," + allowedWeeks + ",", "," + str.tostring(currentWeek) + ",")
bgcolor(shouldTrade ? color.new(color.green, 88) : color.new(color.red, 88),
title="Enabled weeks")