Look if TradingView script runs on weekly time frame with Pine Script
Look if TradingView script runs on weekly time frame with Pine Script
Section titled “Look if TradingView script runs on weekly time frame with Pine Script”TL;DR
timeframe.isweeklyreturnstruewhenever the active chart resolution is expressed in weeks (1W, 2W, …). Use it to branch logic or show chart-aware labels.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 5-10 min |
| Category | Time & Sessions |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5indicator("Weekly detector", overlay=true)
isWeekly = timeframe.isweekly
if barstate.islast txt = isWeekly ? "You're on a weekly chart" : "Not a weekly chart" label.new(bar_index, close, txt + "\nPeriod: " + timeframe.period, style=label.style_label_left, textcolor=color.white, bgcolor=color.new(isWeekly ? color.green : color.orange, 70))Why It Matters
Section titled “Why It Matters”Some strategies only make sense on weekly data, while others should disable themselves to avoid misleading signals. Detecting weekly charts lets you adjust defaults, run heavier calculations, or warn the user when they’re on an unsupported resolution.
What You’ll Learn
Section titled “What You’ll Learn”- How
timeframe.isweeklybehaves across chart resolutions. - Combine the weekly flag with other time-frame helpers and multipliers.
- Present chart-aware labels, alerts, or parameter changes when weekly mode is active.
- Best practices to avoid unnecessary checks each bar.
Quick Reference
Section titled “Quick Reference”| Helper | Purpose |
|---|---|
timeframe.isweekly | true for weekly-based bars (1W, 2W, etc.). |
timeframe.period | Human-readable period string ("W", "2W"). |
timeframe.multiplier | Numeric multiplier for the current period (1 for 1W, 4 for 4W). |
barstate.islastconfirmedhistory | Runs logic once on the last historical bar—useful for labels. |
request.security(symbol, timeframe, expression) | Fetch higher/lower time frame data when weekly is detected. |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Check the weekly flag
Evaluatetimeframe.isweeklyonce per bar and cache the result.isWeekly = timeframe.isweekly -
Branch behaviour
Use the flag to enable or skip logic, choose settings, or show notices.emaLen = isWeekly ? 13 : 55weeklyOnlySignal = isWeekly and ta.crossover(close, ta.ema(close, emaLen)) -
(Optional) Fetch helper data
Pair withtimeframe.periodortimeframe.multiplierfor labels, or load additional series viarequest.security.weeklyMultiplier = timeframe.multiplier
Example Playbook
Section titled “Example Playbook”//@version=5indicator("Weekly-aware trend filter", overlay=true, max_labels_count=500)
fastLen = input.int(13, "EMA length (weekly)")slowLen = input.int(55, "EMA length (non-weekly)")
isWeekly = timeframe.isweeklyema = ta.ema(close, isWeekly ? fastLen : slowLen)plot(ema, "Adaptive EMA", color=color.new(color.blue, 0))
trendUp = close > ema
bgcolor(isWeekly ? color.new(color.green, 88) : na, title="Weekly backdrop")
if barstate.islast label.new(bar_index, close, text="Weekly: " + (isWeekly ? "YES" : "NO") + "\nPeriod: " + timeframe.period + "\nMultiplier: " + str.tostring(timeframe.multiplier), style=label.style_label_left, textcolor=color.white, bgcolor=color.new(isWeekly ? color.green : color.gray, 70))- EMA length adapts automatically, so the same script stays responsive on weekly and non-weekly charts.
- Background colouring highlights when the chart runs in weekly mode.
- A summary label confirms the active period and multiplier for quick debugging.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”- Weekly flags encompass “multi-week” resolutions (
2W,4W). Usetimeframe.multiplierto differentiate them. - Combine with
timeframe.isdwmto catch daily/weekly/monthly groupings in one conditional when needed. - If you only want to run heavy logic once per new week, pair
timeframe.isweeklywithta.change(time("W")). - When disabling trades, ensure you also cancel pending orders to avoid unexpected fills.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”timeframe.isweekly is always false
Section titled “timeframe.isweekly is always false”Double-check the chart resolution; custom ranges (e.g., 10 range) are not weekly data. Switch to a weekly preset like 1W or 2W.
Can I check higher-than-weekly charts?
Section titled “Can I check higher-than-weekly charts?”Use the other helpers: timeframe.ismonthly, timeframe.isdaily, etc. You can also compare timeframe.multiplier against thresholds (timeframe.isweekly and timeframe.multiplier >= 4).
Does the chart type matter?
Section titled “Does the chart type matter?”No. The flag is based entirely on bar timing, so Renko, Line Break, and Heikin Ashi still honour the weekly interval behind the scenes.
Key Takeaways
Section titled “Key Takeaways”timeframe.isweeklytells you when the chart uses a weekly-based resolution.- Use the flag to enable/disable logic, adjust parameters, or show user-facing guidance.
- Pair with
timeframe.period/timeframe.multiplierfor richer context or to differentiate multi-week settings. - Keep weekly-specific work lightweight by running infrequent tasks on
barstate.islastorta.change(time("W")).
Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse PineScript Examples for ready-to-run templates
- Connect to Live Trading when you are ready to automate
Adapted from tradingcode.net, optimised for Algo Trade Analytics users.