Search code examples
pine-scriptpine-script-v5

How to plot horizontal ray when plotting a line?


I am wondering how to draw a horizontal ray (not a horizontal line) when using pinescript for plotting? For example, the following code draw lines for me

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

price1 = input.price(0, "Price 1", confirm=true)

price2 = price1 * 1.05
price3 = price1 * 0.95

plot(price1, color=color.yellow)
plot(price2, color=color.green)
plot(price3, color=color.red)

How can I change it in a way that plot Horizontal Rays instead of Horizontal lines?


Solution

  • You need an input.time() together with input.price(). The trick is, they need to have the same inline value in order for them to work together.

    //@version=5
    indicator("My script", overlay=true)
    
    price1_val = input.price(0, "Price 1", inline="price", confirm=true)
    price1_time = input.time(0, "Time", inline="price", confirm=true)
    
    var ray_drawn = false
    
    if (not ray_drawn)
        line.new(price1_time, price1_val, price1_time + 1, price1_val, color=color.yellow, extend=extend.right, xloc=xloc.bar_time)
        ray_drawn := true
    
    price2 = price1_val * 1.05
    price3 = price1_val * 0.95
    
    plot(price2, color=color.green)
    plot(price3, color=color.red)
    

    enter image description here