timeframe namespace
timeframe
Section titled “timeframe”Overview
Section titled “Overview”The timeframe namespace describes the chart’s resolution and offers utilities for converting between timeframe strings, seconds, and timeframe categories (intraday, daily, weekly, etc.). These helpers are essential when building multi-timeframe logic that reacts differently on higher resolutions.
Timeframe strings follow TradingView’s format (e.g., "1", "15", "1H", "1D", "3M"). Use input.timeframe to gather user input in that format, then process it with `timeframe.*` utilities.
Common helpers
Section titled “Common helpers”| Function | Purpose |
|---|---|
timeframe.period | Returns the script’s current timeframe string. |
timeframe.multiplier | Number of base units in the current timeframe (e.g., 5 for 5m). |
timeframe.change(tf) | true on the first bar of each new tf period. |
timeframe.isintraday, timeframe.isdaily, timeframe.isweekly, timeframe.ismonthly, timeframe.isseconds, timeframe.isticks | Boolean flags describing the current chart. |
timeframe.in_seconds(tf) | Convert a timeframe string into seconds. |
timeframe.from_seconds(seconds) | Produce the closest matching timeframe string for a number of seconds. |
timeframe.main_period | Underlying base period for composite ranges (e.g., 1D for a chart built from multiple days). |
Remarks
Section titled “Remarks”timeframe.change()is handy for drawing separators or resetting accumulators whenever a higher timeframe begins (daily on a 5-minute chart, etc.).- For custom resampling, multiply
timeframe.in_seconds()by a factor to create strings withtimeframe.from_seconds()before feeding them torequest.security(). - Category checks (e.g.,
timeframe.isseconds) help you guard code that only works on certain chart types, preventing runtime errors.
Example
Section titled “Example”//@version=5indicator("Timeframe utilities", overlay=true)
isNewDay = timeframe.change("1D")isHigherTF = timeframe.in_seconds("1D") >= timeframe.in_seconds()
plotshape(isNewDay, style=shape.labelup, text="New day", color=color.new(color.orange, 0), textcolor=color.white, location=location.belowbar)
bgcolor(isHigherTF ? color.new(color.blue, 92) : na, title="Chart timeframe ≤ daily")