3

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

e2e4
  • 3,428
  • 6
  • 18
  • 30
Max Cheung
  • 176
  • 3
  • 13

3 Answers3

4

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 
Anthony Raimondo
  • 1,621
  • 2
  • 24
  • 40
2

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)
e2e4
  • 3,428
  • 6
  • 18
  • 30
  • I wish to grab the day low value of certain date say 3 Jan 2019. is it the best way? I dont need intraday value. Anyway to simplify the code so that I dont need to input 4 Jan 2019? – Max Cheung Jul 24 '20 at 11:19
  • Edited the answer. Now script grab the low value if the date is reached. – e2e4 Jul 24 '20 at 14:11
0

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

Alfa Bondi
  • 13
  • 2