Search code examples
pine-script

Get date of a candle


I'm trying to get the date of the last candle, but at the moment I get only the current date.

What I'm looking for is this data:

Year => 2022
Month => December
Day => 24

At this moment I get this values using:

year(timenow)
month(timenow)
dayofmonth(timenow)

But what I would like is getting this value based on last daily candle. If I use Bareplay, using timenow, the value doesn't change.


Solution

  • You should use the time function:

    The time function returns the UNIX time of the current bar for the specified timeframe and session or NaN if the time point is out of session.

    See: https://www.tradingview.com/pine-script-reference/v5/#fun_time

    time(timeframe.period) will return you the time of the actual bar, not the time now. Then you can use year( ), month( ) and dayofthemonth( ) to retrieve your values.