longshortratio
longshortratio

Reputation: 23

Get the date of a specific bar on TradingView Pine Script

I'm writing an indicator where I need to "anchor" it to a certain date of interest. Basically an anchored VWAP where I'm trying to automate finding easy areas of interest to "anchor" the indicator to.

Basically, I'm trying to get the Highest and Lowest value over a lookback period (say 365 in this example, and trying to "access" the date of that bar, so I can initialise t (time) as starting in that bar.

I can do this with individual inputs but unsure how to do it with accessing time/date information from previous bars. Thanks!

h1 = highest(high, 365)
time(h1) (?)  *this is wrong* 
start = t == time 

Upvotes: 2

Views: 8751

Answers (1)

PineCoders-LucF
PineCoders-LucF

Reputation: 8789

You'll need highestbars() for that, which returns the offset of the highest high. It returns a negative value, so we need to change its sign:

//@version=4
study("")
// Get bar index of highest high.
highIndex = -highestbars(high, 365)
// Get time at highest high.
t = time[highIndex]
plot(highIndex, "Index of highest high")
// Plot day of the month of highest high's bar.
plot(dayofmonth(t), "Day of the month", color.red)

Upvotes: 4

Related Questions