Max Cheung
Max Cheung

Reputation: 186

How to get bar index on specific date

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

Upvotes: 4

Views: 10172

Answers (3)

Anthony Raimondo
Anthony Raimondo

Reputation: 1721

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 

Upvotes: 6

Alfa Bondi
Alfa Bondi

Reputation: 13

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

Upvotes: 0

e2e4
e2e4

Reputation: 3818

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)

Upvotes: 3

Related Questions