Search code examples
pine-scriptpine-script-v5tradingview-api

How to get get Previous Day 11:00 AM Open Price


I am new to pine script kindly please advice me a code snip , How to get get Previous Day 11:00 AM Open Price

I would like to write a strategy based on this price level, below code is not working properly for each day

t = timestamp(year(timenow), month(timenow), dayofmonth(timenow)-1, 11, 0)  // 1st of November 2017
l = float(na)

var oprice = 0
if time >= t and na(l[1])
    l := open
    oprice = l
    l
else
    l := l[1]
    l

plot(l)

Solution

  • If the bar exists, you can easily do that by using the hour and minute built-in variables.

    //@version=5
    indicator("My script", overlay=true)
    
    is_11_am = (hour == 11) and (minute == 0)
    
    bgcolor(is_11_am ? color.new(color.blue, 85) : na)
    
    var float open_11_am_today = na
    var float open_11_am_yesterday = na
    
    if (is_11_am)
        open_11_am_yesterday := open_11_am_today
        open_11_am_today := open
    
    plot(open_11_am_yesterday, color=color.red)
    plot(open_11_am_today, color=color.green)
    

    enter image description here