weekofyear() - Pine Script Function
weekofyear()
Section titled “weekofyear()”Overview
Section titled “Overview”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.
Syntax
Section titled “Syntax”weekofyear(time, timezone) → series intParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
time | series int | UNIX timestamp in milliseconds—typically the built-in time series. |
timezone | series string (optional) | Target timezone ("UTC+1", "America/New_York", etc.). Defaults to syminfo.timezone. |
Returns
Section titled “Returns”series int — Calendar week number between 1 and 53.
Remarks
Section titled “Remarks”- Use
time_tradingdaywhen 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.
Example
Section titled “Example”//@version=5indicator("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")