Skip to main content

Pine Script Timestamps: Complete Guide for Traders

· 7 min read
Pineify Team
Pine Script and AI trading workflow research team

Look, if you've ever tried to build a trading strategy that needs to know what time it is, you've probably bumped into Pine Script timestamps. They can be a bit confusing at first, but once you get the hang of them, they're actually pretty useful.

So What Exactly Are These Timestamps?

Pine Script uses something called UNIX timestamps. Sounds fancy, but it's just a way of counting time. Basically, it counts the milliseconds since January 1, 1970 (yeah, that's when computers decided time began). The cool thing is that this number is the same no matter where you are in the world - no timezone headaches.

When you use the time() function in Pine Script, it gives you this timestamp for whatever bar you're looking at.

Getting Your Hands on Time Data

The Basic Time Functions

There are a few functions that'll get you the time info you need:

  • time() - gives you when the current bar opened
  • time_close() - when it closed
  • timenow() - what time it is right now when your script runs

You can also break down dates into pieces using functions like year(), month(), dayofmonth() - pretty self-explanatory stuff.

Making Timestamps Human-Readable

The Best Pine Script Generator

Nobody wants to read "1641024000000" and figure out what date that is. That's where str.format_time() comes in handy. It converts those ugly numbers into something like "Jan 1, 2022" or whatever format you want.

The Easy Way: Using Pineify Instead

Here's the thing - writing all this timestamp code can get pretty messy, especially if you're not a programmer. I've seen people spend hours trying to get their time conditions right.

Pineify | Best Pine Script Editor

That's where Pineify comes in. Instead of wrestling with code, you can just click and drag to build your time-based rules. Want to check if it's the first day of the month? Just select it from a dropdown. Need to combine that with some technical indicators? Drag them together. It's like building with Lego blocks instead of writing assembly code.

Pineify | Best Pine Script Generator

Website: Pineify

Check out what Pineify can do.

What You Can Actually Do With This Stuff

Time-Based Triggers

Want to mark the first trading day of each month? Use the month() function to detect when the month changes. Or maybe you want your strategy to only trade during certain hours - input.time() lets you pick times right from the chart.

Working Across Different Timeframes

The security() function is pretty neat - it lets you grab data from different timeframes. So you could be looking at 5-minute bars but check what the daily chart is doing.

A Few Things to Keep in Mind

  • Pay attention to timezones. The exchange timezone affects how those calendar functions work.
  • When using input.time(), add confirm = true so you can click directly on the chart to pick your time. Way easier than typing it in.

Wrapping Up

Timestamps in Pine Script aren't rocket science, but they can trip you up if you don't understand the basics. Whether you want to code it yourself or use a tool like Pineify to build it visually, getting comfortable with time-based conditions will make your trading strategies way more flexible.

What is a UNIX timestamp in Pine Script?

A UNIX timestamp in Pine Script is the number of milliseconds elapsed since January 1, 1970. The time() function returns this value for each bar, making it timezone-independent and consistent across all markets.

How do I use the time() function in Pine Script?

The time() function returns the UNIX timestamp of when the current bar opened. You can compare it against a specific date or use it with calendar functions like year(), month(), and dayofmonth() to build time-based trading conditions.

What is the difference between time() and timenow() in Pine Script?

time() returns the timestamp of the current bar's open, while timenow() returns the real-time current timestamp when the script executes. Use time() for bar-level logic and timenow() for live real-time checks.

How do I filter trades to specific hours in Pine Script?

Use input.time() to let users select a time range directly from the chart, or use the hour() and minute() functions to check whether the current bar falls within a desired trading window.

How do I convert a Pine Script timestamp to a readable date?

Use str.format_time(timestamp, format, timezone) to convert a UNIX millisecond timestamp into a human-readable string such as "Jan 1, 2022". This is useful for labels, tooltips, and debugging.

Can I access timestamps from a different timeframe in Pine Script?

Yes. Use the request.security() function to pull bar data including timestamps from a higher or lower timeframe, allowing you to align multi-timeframe logic in a single script.