Search code examples
linepine-scriptindicator

Pine Script Indicator - Sunday Open Horizontal Line


I'm stuck and need some help please... I'm trying to create a indicator which draws a horizontal line from the Sunday Daily Open candle for the upcoming week. I found the code below as an example, which works for simple line, but the line is too short; it only continues for a day - I would want it to plot through to the following Sunday open (if this isn't possible it's also fine to just extend the line, and delete all previous lines.. or I was thinking a continuous step line could be best?)

The chart below shows how I want it to look, whether step line or individual lines for each week. But I wouldn't want them all to extend to the end of the chart, would be too messy.

enter image description here


study(title="Sunday Open", shorttitle="Sunday Open", overlay=true)
openPrice = security(tickerid, 'D', open)
isSunday() => dayofweek(time('D')) == sunday ? 1 : 0
plot(isSunday() and openPrice ? openPrice:  na, title="Sunday Open",   style=linebr, linewidth=2, color=orange)

Solution

  • That is because you are plotting only when it is Sunday with isSunday() inside your plot().

    Instead, use a var to store the open price when it is Sunday, and update this variable the next Sunday.

    //@version=5
    indicator(title="Sunday Open", shorttitle="Sunday Open", overlay=true)
    var float open_price = na
    is_sunday = dayofweek == dayofweek.sunday
    is_sunday_first_bar = not is_sunday[1] and is_sunday
    open_price := is_sunday_first_bar ? open : open_price
    plot(open_price, title="Sunday Open", style=plot.style_linebr, linewidth=2, color=color.orange)