Search code examples
pine-scriptpine-script-v5

How to specify a custom last bar for Pine Script indicator?


I want to find a way to tell the indicator that the last bar is at for example this date 2024-01-01 16:00:00 and plots the indicator. In other words I want to set the end time for indicator. And ignore data beyond that date for calculations.

Right now the current bar is always the last bar.


Solution

  • You can use a timestamp to define your end time and do all your calculations and plots when time < end time.

    Here is an example:

    //@version=5
    indicator("My script", overlay=true)
    
    end_time = timestamp("Jan 01 2024 16:00:00")
    can_plot = time < end_time
    val = can_plot ? close : na
    
    plot(val, color=color.white, linewidth=3)
    

    enter image description here

    If you want to keep the last value of your calculation, use a var variable.