0

I need to get a price (in my case the first opening price) after a specifig date. I've got the timestamp, let's say I need the first opening price after 15.05.2019. The function is called on the last bar on the chart, so I really need to get a historic price back and not while processing every bar.

I can't find a possibility to do that within a function. It seems a quite simple task, but I am stuck for hours now...

I have tried ta.valuwehen and also a condition, but as I am on the last bar already, it will always show me the closing price of the current bar. E.g.:

var testopen = 0.0

    testopen := ta.valuewhen(time > first_buy_timestamp_start, open, 0)

and

  var testopen = 0.0

    if time > as first_buy_timestamp_start and testopen == 0.0
        testopen := close
TSC
  • 1

1 Answers1

0

Use time with your timestamp() to figure out your "start" candle. After that bar, store the open price in a var variable.

//@version=5
indicator("My script", overlay=true)

start_date = time == timestamp(2022, 10, 01, 03, 15, 00)

is_first_bar_after_start_date = start_date[1] and not start_date
bgcolor(start_date ? color.blue : na)

var float open_price_after_start_date = na

open_price_after_start_date := is_first_bar_after_start_date ? open : open_price_after_start_date
plot(open_price_after_start_date, color=color.white)

enter image description here

You can use below function to get the first open price after your timestamp regardless of the timeframe.

f_get_timestamp_open_price(t) =>
    start_date = time >= t
    first_bar = not start_date[1] and start_date
    ta.valuewhen(first_bar, open, 0)

t = timestamp(2022, 10, 02, 03, 10, 30)
test = f_get_timestamp_open_price(t)
plot(test, color=color.green)
vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • Thank you vitruvius, but unfortunately, I can't implement this solution. 1. It's not working within a function, it is working by processing bar by bar and can't be processed at the las bar backward. 2. I guess it's not working on every interval. I don't get any result on the 1d chart for example. – TSC Nov 13 '22 at 23:59
  • Please see my edit. Added a function for you. – vitruvius Nov 14 '22 at 07:15
  • Thank you, it is a great function, but I can't use it in my case. I am running a function on the last bar. If I invoke your function within my function, I will get always the price of the last bar on chart, and not of the bar I am looking for (from the timestamp provided). – TSC Nov 15 '22 at 10:43