pine script: how do you get the length of a series?

Viewed 1775

How can I get the number of days for a tickerid? For example, how long is the 'close' series in a simple default script? Or how can I get the first date availble for the current tickerid? If I had that I think that I can compute the number of days available.

1 Answers

This will show the information you requested in a label on the last bar.
If you set the timeframe to Daily, you'll get the number of (trading) days.

//@version=4
study("My Script", overlay=true)

var string firstDate = na

if barstate.isfirst
    firstDate := tostring(dayofmonth(time)) + "/" + tostring(month(time)) + "/" + tostring(year(time))
    
if barstate.islast
    label.new(bar_index, high, "First date = " + firstDate + "\nNumber of bars = " + tostring(bar_index+1), yloc=yloc.abovebar)
Related