Home » Chart Information » Understanding the last_bar_time Function in Pine Script

Understanding the last_bar_time Function in Pine Script

Photo of author
Published on

This article will explore the last_bar_time variable in detail, including its usage, implications for indicator repainting, and practical examples to solidify your understanding.

What is last_bar_time?

last_bar_time is a built-in variable in Pine Script that holds the time of the last bar on the chart in UNIX format. Specifically, it returns the number of milliseconds that have elapsed since 00:00:00 UTC on 1 January 1970, up to the opening time of the last bar displayed on the chart.


The last_bar_time variable is a series integer (series int). This means that its value can vary from one bar to the next, reflecting the time of the last bar as the chart updates.


It’s important to be aware that utilizing last_bar_time in your scripts can lead to indicator repainting. Repainting refers to the phenomenon where the indicator’s value changes after the bar has closed, which can be misleading in real-time analysis. This happens because last_bar_time updates with each new bar, potentially altering the behavior of scripts that depend on its value.

Practical Example

Let’s demonstrate the use of last_bar_time with a simple script that highlights the opening time of the last bar on the chart:

indicator("Last Bar Time Example", overlay=true)
// Rename the variable for uniqueness
lastBarOpeningTime = last_bar_time

// Convert the UNIX time to a readable format
timeString = tostring(lastBarOpeningTime)

// Plot the time on the chart
label.new(bar_index, high, text=timeString, style=label.style_labeldown, color=color.red, size=size.large)

In this example, lastBarOpeningTime is used to store the value of last_bar_time. The script then converts this UNIX timestamp into a string (timeString) and displays it on the chart using a label.

Line-by-Line Walkthrough

  1. Indicator Declaration: We start by declaring the version of Pine Script (@version=5) and defining our script’s name and properties with indicator(). The overlay=true parameter ensures our script’s output appears directly on the chart.
  2. Variable Renaming: We assign last_bar_time to lastBarOpeningTime for uniqueness. This practice is helpful in complex scripts where variable names may clash.
  3. Conversion to String: The UNIX timestamp is converted into a string format using tostring(). This allows us to display it as text on the chart.
  4. Label Creation: Finally, we use label.new() to create a label that shows the time of the last bar. The label is placed at the last bar’s index and above its high price, styled to point downwards, colored red, and sized large for visibility.

Key Features and Takeaways

  • Functionality: last_bar_time provides the UNIX timestamp of the last bar’s opening time, offering precise timing information for scripting purposes.
  • Type: It is a series int, meaning its value can change with each new bar, reflecting real-time updates on the chart.
  • Repainting Concerns: Using last_bar_time can lead to indicator repainting, which is crucial to consider when developing real-time analysis tools.
  • Application: This variable is essential for scripts that need to perform actions based on the exact time of the last bar, such as time-based filtering or event marking.

In conclusion, last_bar_time is a valuable variable in Pine Script for accessing time-related data of chart bars. However, it’s essential to use it judiciously to avoid the pitfalls of indicator repainting. By understanding and applying last_bar_time correctly, you can enhance your Pine Script indicators and strategies with precise timing functionalities.

Leave a Comment