How to get bar index on specific date

Viewed 3611

I am new to pine script. I want to compare the prices on 2 specific date.

But how would I get the bar_index on a particular date?

Thanks in advance

3 Answers

timestamp function would return UNIX time of specified date and time.

If time in ms is more the Jan 3, grab the low. For intraday specify minutes in the timestamp function.

//@version=4
study("low on specific date")

specificDate = time >= timestamp(2019, 1, 3, 00, 00)

var float lowOnDate= na
if specificDate and not specificDate[1]
    lowOnDate := low
plot(lowOnDate)

Get bar index at specific date across all time frames.

  //@version=5

  anchorTime = input.time(timestamp("20 May 2022 15:00 -0500"), "Date")

  anchorBarIndex = (time - anchorTime) / (1000 * timeframe.in_seconds(timeframe.period))

  anchorBarsBack = bar_index - anchorBarIndex 

Let's say you want to get candle data in 1H timeframe on June 30th 3AM GMT+8

x1 = (time - timestamp("GMT+8",2021,06,30,03,00,00))/3600000

candleHigh = high[x1]

If you're in different TF just convert it to milisecond

Related