See if TradingView uses hourly time frame with Pine Script
See if TradingView uses hourly time frame with Pine Script
Section titled “See if TradingView uses hourly time frame with Pine Script”TL;DR
Pine Script doesn’t have a direct “is hourly” flag, but you can combinetimeframe.isminutesandtimeframe.multiplier >= 60to detect hour-based charts.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 10-15 min |
| Category | Time & Sessions |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5indicator("Hourly detector", overlay=true)
isHourlyTF() => timeframe.isminutes and timeframe.multiplier >= 60 and timeframe.multiplier < 1440
isHourly = isHourlyTF()
bgcolor(isHourly ? color.new(color.orange, 85) : na, title="Hourly background")plotchar(isHourly, "Hourly flag", "H", location=location.top)Why It Matters
Section titled “Why It Matters”Strategies often treat 1H–12H charts differently from lower minute resolutions or higher daily ones. Detecting hourly resolutions lets you tweak indicator lengths, throttle alerts, or block trades that were optimised for intraday flows.
What You’ll Learn
Section titled “What You’ll Learn”- Combine Pine’s time-frame helpers to detect 60-minute-and-above charts that are still intraday.
- Apply the helper to change visuals, inputs, or strategy behaviour.
- Guard the logic against range charts and custom aggregations.
- Use the same flag inside
request.security()calls when pulling multi-time-frame data.
Quick Reference
Section titled “Quick Reference”| Helper | Purpose |
|---|---|
timeframe.isminutes | true for any minute-based resolution, including hourly and custom minute counts. |
timeframe.multiplier | Number of minutes represented by each bar (e.g., 1, 60, 240, 1440). |
isHourlyTF() (custom) | Returns true when the bar multiplier is between 60 minutes and one day. |
bgcolor, label.new | Optional ways to surface the detection on chart. |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Define the helper
A single function keeps the check reusable and readable.isHourlyTF() =>timeframe.isminutes and timeframe.multiplier >= 60 and timeframe.multiplier < 1440 -
React to the flag
Use the boolean to toggle visuals, change inputs, or enforce guardrails.emaLen = isHourlyTF() ? 34 : 55 -
Integrate with strategy logic
Skip trades, adjust risk, or fetch higher-time-frame data only when hourly mode is active.if isHourlyTF()strategy.entry("HourlyLong", strategy.long)
Example Playbook
Section titled “Example Playbook”//@version=5indicator("Hourly-aware parameter switch", overlay=true, max_labels_count=500)
isHourlyTF() => timeframe.isminutes and timeframe.multiplier >= 60 and timeframe.multiplier < 1440
fastLenHourly = input.int(21, "Fast EMA (hourly)")fastLenNonHour = input.int(34, "Fast EMA (non-hourly)")slowLenHourly = input.int(55, "Slow EMA (hourly)")slowLenNonHour = input.int(89, "Slow EMA (non-hourly)")
emaFast = ta.ema(close, isHourlyTF() ? fastLenHourly : fastLenNonHour)emaSlow = ta.ema(close, isHourlyTF() ? slowLenHourly : slowLenNonHour)
plot(emaFast, "Adaptive fast EMA", color=color.new(color.teal, 0))plot(emaSlow, "Adaptive slow EMA", color=color.new(color.purple, 0))
bgcolor(isHourlyTF() ? color.new(color.orange, 88) : na, title="Hourly highlight")
if barstate.islast label.new(bar_index, close, text = "Resolution: " + timeframe.period + "\nHourly? " + (isHourlyTF() ? "Yes" : "No") + "\nFast EMA: " + str.tostring(isHourlyTF() ? fastLenHourly : fastLenNonHour), style=label.style_label_left, textcolor=color.white, bgcolor=color.new(isHourlyTF() ? color.orange : color.gray, 70))- The helper abstracts the resolution test so it can be reused across the script.
- EMA parameters adapt instantly when switching between hourly and non-hourly charts.
- Background colour and label provide immediate confirmation of the detected mode.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”- Range charts always use 1-minute data under the hood—
timeframe.multiplierreports1, so the helper correctly returnsfalse. - If you want to include multi-hour charts (like 4H, 8H, 12H), keep the
>= 60logic, but adjust the upper bound if you consider 24H part of the hourly family. - Pair the helper with
request.security()to pull daily data when the chart is hourly: use the flag to decide which higher frame to request. - Cache the result in a
varor evaluate once per bar if you call it repeatedly inside loops.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”The helper returns false on a 2H chart
Section titled “The helper returns false on a 2H chart”Ensure you didn’t set an upper bound of exactly 60. Use >= 60 to include all hourly multiples such as 120, 180, or 240 minutes.
Does it work on Renko or Heikin Ashi?
Section titled “Does it work on Renko or Heikin Ashi?”Yes—those charts still rely on the underlying minute aggregation. The helper reads the data interval, not the visual chart type.
I only care about exactly 1H
Section titled “I only care about exactly 1H”Replace the >= 60 check with timeframe.multiplier == 60 to match a single hour.
Key Takeaways
Section titled “Key Takeaways”- Pine doesn’t expose
timeframe.ishourly, but the combination oftimeframe.isminutesandtimeframe.multipliermakes the check trivial. - Wrap the logic in a helper function so you can use it across entries, exits, and visual cues.
- Hourly detection keeps parameter presets and guards aligned with the chart resolution your script supports.
- Adjust the multiplier bounds if you consider multi-hour bars part of the same family.
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.
-
So for Heikin Ashi, Renko, Line Break, Kagi, and Point & Figure charts, the function returns true or false based on the chart’s underlying data.
-
On Range charts the function always returns false. TradingView makes those charts from 1-minute data, no matter how many ranges we configure. So they can’t use hourly data.
-
IsHourlyTF() tells if the script runs on an hourly chart. But we can also pass this function to the request.security() function. When we do, we find out if that background data function calculates with hourly data.
-
So for Heikin Ashi, Renko, Line Break, Kagi, and Point & Figure charts, the function returns true or false based on the chart’s underlying data.
-
On Range charts the function always returns false. TradingView makes those charts from 1-minute data, no matter how many ranges we configure. So they can’t use hourly data.
-
With two Pine Script variables we see if the script uses an hourly time frame.
-
The timeframe.isminutes variable says if the time frame uses minute data. This is needed since TradingView makes hourly charts from minute data.
-
And the timeframe.multiplier variable has to be 60 or higher. That’s how we differentiate between minute time frames and hourly ones.
Ready to implement this in your own strategies?
- Open AI Editor: Get AI assistance while coding
- See More Examples: Browse complete strategy examples
- Connect to Trading: Turn your PineScript into live trades via Alpaca
This tutorial is based on content from TradingCode.net, adapted for Algo Trade Analytics users.
This tutorial is based on content from tradingcode.net, adapted for Algo Trade Analytics users.
Example Playbook
Section titled “Example Playbook”//@version=5 indicator(title="Hourly time frame", overlay=true) // IsHourlyTF() returns 'true' if the script runs on data with an // hourly time frame (>= 60 minutes and below 1 day). Returns 'false' // otherwise. // <div class="callout callout-warning">
**Note** works on all chart types, including price-based ones.
</div>
IsHourlyTF() => timeframe.isminutes and timeframe.multiplier >= 60 // On the last historical bar, show if the chart uses an // hourly time frame or not if barstate.islastconfirmedhistory labelText = if IsHourlyTF() "This chart uses an\nhourly time frame!" else "This chart's is below 1 hour\nor 1 day and higher" label.new(bar_index + 2, hl2, style=label.style_label_lower_left, color=#D0F0C0, size=size.large, text=labelText)Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse Strategy 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.