Search code examples
pine-scriptpine-script-v5

What is the best way to plot a HoD line using this code


I have this script, but is there way to have the HoD line, straight / horizontal, stead of the wavey following a new intraday high? For example, if at new HoD is $105.55, a straight line will be horizontal at $105.55, then if there is a new HoD is $105.76, the straight line moves to $105.76?

indicator("My script", overlay=true)

var float high_price = na
var int high_time = na

is_new_day = ta.change(time("D"))
bgcolor(is_new_day ? color.new(color.blue, 85) : na)

if (is_new_day)
    high_price := high
    high_time := time
else
    if (high > high_price)
        high_price := high
        high_time := time

plot(high_price)````

Thanks

I tried adding plot(high_price[1]) But that did not work

Solution

  • You want to use the line.new function. This will draw a line starting at the first bar of a new day until the last bar on your chart. It will adjust to the high of the day throughout the day.

    //@version=5
    indicator("My script", overlay=true)
    
    var float high_price = na
    var int high_time = na
    var line hodLine = na
    
    is_new_day = ta.change(time("D"))
    bgcolor(is_new_day ? color.new(color.blue, 85) : na)
    
    if (is_new_day)
        high_price := high
        high_time := time
    else if (high > high_price)
        high_price := high
    
    if barstate.islast
        hodLine := line.new(high_time, high_price, chart.right_visible_bar_time, high_price, xloc = xloc.bar_time)