How does a chart's time zone setting affect TradingView scripts?
How does a chart’s time zone setting affect TradingView scripts?
Section titled “How does a chart’s time zone setting affect TradingView scripts?”TL;DR
Built-in date/time variables use the instrument’s exchange time zone, so script logic can appear “offset” if the chart uses a different zone.
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("First bar of day", overlay=true)
firstBar = dayofmonth != dayofmonth[1]bgcolor(firstBar ? color.new(color.teal, 80) : na)Why It Matters
Section titled “Why It Matters”Scripts that trigger “at the open” or inside a session rely on exchange time. If you change the chart time zone to something else (e.g., local time) the highlighted bars may look late or early—even though the logic still fires at the correct exchange timestamp. Understanding the distinction prevents false debugging hunts.
What You’ll Learn
Section titled “What You’ll Learn”- How exchange time interacts with date/time variables
- Detecting session membership regardless of chart display time
- Communicating the time zone difference to users
- Overriding the chart display while keeping logic intact
Quick Reference
Section titled “Quick Reference”| Call | Purpose |
|---|---|
dayofmonth | Returns the day number in exchange time. |
time(resolution, session) | Checks whether a bar falls inside a session (exchange time). |
timenow | Current exchange time (UTC timestamp). |
input.timezone | (Optional) Allow users to compare exchange time against their own zone. |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Detect day boundaries or sessions
Use exchange-time variables and functions.isFirstBar = dayofmonth != dayofmonth[1]inSession = not na(time(timeframe.period, "0930-1600")) -
Colour or signal based on those checks
Colour backgrounds or emit alerts without worrying about the chart’s visible time zone.bgcolor(isFirstBar ? color.new(color.teal, 85) : na)bgcolor(inSession ? color.new(color.orange, 85) : na) -
Optional: display both exchange and chart time
Help users reconcile what they see by printing both times.label.new(bar_index, high,"Exchange: " + str.format("{0,time,HH:mm}", time) +"\nChart TZ: " + str.format("{0,time,HH:mm}", timenow))
Example Playbook
Section titled “Example Playbook”//@version=5indicator("Exchange vs chart time helper", overlay=true)
isFirstBar = dayofmonth != dayofmonth[1]inSession = not na(time(timeframe.period, "0930-1600"))
bgcolor(isFirstBar ? color.new(color.teal, 85) : inSession ? color.new(color.orange, 85) : na)
if isFirstBar or inSession and barstate.islast label.new( bar_index, high, str.format("Exchange: {0,time,HH:mm}\nChart: {1,time,HH:mm}", time, timenow), style=label.style_label_left, textcolor=color.white, bgcolor=color.new(color.gray, 70) )- The session logic stays anchored to exchange time even if you change the chart display zone.
- The label provides context by showing both exchange time (script logic) and local chart time.
- Colour cues make it obvious which bars the logic considers “day start” or “inside the session.”
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”- Always document in your script description that exchange time drives the logic.
- If you must trigger logic using a local time zone, convert with
input.timezoneandtime(timeframe.period, session, timezone). - For multi-exchange instruments, verify which exchange TradingView associates with the symbol (see the symbol info dialog).
- When comparing
timevalues, usehour,minute, ortime(timeframe, session)helpers rather than assuming uniform bar spacing.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”My background highlights are shifted when I change the chart time zone—did I break something?
Section titled “My background highlights are shifted when I change the chart time zone—did I break something?”No. The logic still uses exchange time; only the chart display changed. Verify by switching back to the exchange zone—your highlights should align again.
How can users compare exchange time with their local zone?
Section titled “How can users compare exchange time with their local zone?”Print both times (as in the example playbook) or expose an input.timezone so they can supply their preferred zone and see the offset.
Key Takeaways
Section titled “Key Takeaways”- Pine’s date/time helpers use the instrument’s exchange time—not the chart display zone.
- Changing the chart’s time zone only affects what you see in the footer, not the logic itself.
- Colouring or signalling based on exchange time is reliable—just communicate it to end users.
- Provide optional labels or settings if you want to bridge the gap between exchange and local times.
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.