'How to bypass "if" operator restriction and draw "hline"

trying to draw (and switch between) three lines above and below my base value. But I can't get it to work because "if" operator unable to use "line". Tried to reference "hline" in "plot" - also failed. I would appreciate your help on how to solve this restriction. https://i.stack.imgur.com/3k5qk.png

//@version=5
indicator("Lines", overlay=true)
base = input.float(title="Base Value", defval=0.76,  step=0.0001)
lineChoice = input.string(title="Buy/Sell", defval = "Buy", options = ["Buy", "Sell", "Buy&Sell"])

if (lineChoice == "Buy")
    hline(base, title='Base', color=color.aqua, linestyle=hline.style_solid, linewidth=2)
    hline(base * 1.03, title='3%', color=color.aqua, linestyle=hline.style_dotted, linewidth=2)
    hline(base * 1.02, title='2%', color=color.aqua, linestyle=hline.style_dashed, linewidth=2)
    hline(base * 1.01, title='1%', color=color.aqua, linestyle=hline.style_dashed, linewidth=2)
    hline(base * 1.004, title='0,4%', color=color.aqua, linestyle=hline.style_dashed, linewidth=2)
else if (lineChoice =="Sell")    
    hline(base, title='Base', color=color.purple, linestyle=hline.style_solid, linewidth=2)
    hline(base * 0.996, title='-0.4%', color=color.purple, linestyle=hline.style_dotted, linewidth=2)
    hline(base * 0.99, title='-1%', color=color.purple, linestyle=hline.style_dashed, linewidth=2)
    hline(base * 0.98, title='-2%', color=color.purple, linestyle=hline.style_dashed, linewidth=2)
    hline(base * 0.97, title='-3%', color=color.purple, linestyle=hline.style_dashed, linewidth=2)
else if (lineChoice == "Buy&Sell")
    hline(base * 1.03, title='3%', color=color.aqua, linestyle=hline.style_dotted, linewidth=2)
    hline(base * 1.02, title='2%', color=color.aqua, linestyle=hline.style_dashed, linewidth=2)
    hline(base * 1.01, title='1%', color=color.aqua, linestyle=hline.style_dashed, linewidth=2)
    hline(base * 1.004, title='0,4%', color=color.aqua, linestyle=hline.style_dashed, linewidth=2)
    hline(base, title='Base', color=color.red, linestyle=hline.style_solid, linewidth=2)
    hline(base * 0.996, title='-0.4%', color=color.purple, linestyle=hline.style_dotted, linewidth=2)
    hline(base * 0.99, title='-1%', color=color.purple, linestyle=hline.style_dashed, linewidth=2)
    hline(base * 0.98, title='-2%', color=color.purple, linestyle=hline.style_dashed, linewidth=2)
    hline(base * 0.97, title='-3%', color=color.purple, linestyle=hline.style_dashed, linewidth=2)
else
    na
    
plot (base)

Thank you!!!



Solution 1:[1]

I was hoping to get an answer here but had to find a solution myself. Instead of "hline" we need to use "line.new" and that solves everything.

// © via15

//@version=5
indicator("Lines", overlay=true)
base = input.float(title="Base Value", defval=0.7162,  step=0.0001)
lineChoice = input.string(title="Buy/Sell", defval = "Buy", options = ["Buy", "Sell", "Buy&Sell"])

if (lineChoice == "Buy")
    a = line.new(x1=bar_index, y1=base, x2=bar_index[1], y2=base, extend=extend.both, color=color.aqua, style=line.style_solid, width=1)
    b = line.new(x1=bar_index, y1=base * 1.004, x2=bar_index[1], y2=base * 1.004, extend=extend.both, color=color.aqua, style=line.style_dotted, width=1)
    c = line.new(x1=bar_index, y1=base * 1.01, x2=bar_index[1], y2=base * 1.01, extend=extend.both, color=color.aqua, style=line.style_dashed, width=1)
    d = line.new(x1=bar_index, y1=base * 1.02, x2=bar_index[1], y2=base * 1.02, extend=extend.both, color=color.aqua, style=line.style_dashed, width=1)
    e = line.new(x1=bar_index, y1=base * 1.03, x2=bar_index[1], y2=base * 1.03, extend=extend.both, color=color.aqua, style=line.style_dashed, width=1)
else
    na
if (lineChoice == "Sell")
    f = line.new(x1=bar_index, y1=base, x2=bar_index[1], y2=base, extend=extend.both, color=color.purple, style=line.style_solid, width=1)
    g = line.new(x1=bar_index, y1=base * 0.996, x2=bar_index[1], y2=base * 0.996, extend=extend.both, color=color.purple, style=line.style_dotted, width=1)
    h = line.new(x1=bar_index, y1=base * 0.99, x2=bar_index[1], y2=base * 0.99, extend=extend.both, color=color.purple, style=line.style_dashed, width=1)
    i = line.new(x1=bar_index, y1=base * 0.98, x2=bar_index[1], y2=base * 0.98, extend=extend.both, color=color.purple, style=line.style_dashed, width=1)
    j = line.new(x1=bar_index, y1=base * 0.97, x2=bar_index[1], y2=base * 0.97, extend=extend.both, color=color.purple, style=line.style_dashed, width=1)
else
    na
if (lineChoice == "Buy&Sell")
    a = line.new(x1=bar_index, y1=base, x2=bar_index[1], y2=base, extend=extend.both, color=color.aqua, style=line.style_solid, width=1)
    b = line.new(x1=bar_index, y1=base * 1.004, x2=bar_index[1], y2=base * 1.004, extend=extend.both, color=color.aqua, style=line.style_dotted, width=1)
    c = line.new(x1=bar_index, y1=base * 1.01, x2=bar_index[1], y2=base * 1.01, extend=extend.both, color=color.aqua, style=line.style_dashed, width=1)
    d = line.new(x1=bar_index, y1=base * 1.02, x2=bar_index[1], y2=base * 1.02, extend=extend.both, color=color.aqua, style=line.style_dashed, width=1)
    e = line.new(x1=bar_index, y1=base * 1.03, x2=bar_index[1], y2=base * 1.03, extend=extend.both, color=color.aqua, style=line.style_dashed, width=1)
    g = line.new(x1=bar_index, y1=base * 0.996, x2=bar_index[1], y2=base * 0.996, extend=extend.both, color=color.purple, style=line.style_dotted, width=1)
    h = line.new(x1=bar_index, y1=base * 0.99, x2=bar_index[1], y2=base * 0.99, extend=extend.both, color=color.purple, style=line.style_dashed, width=1)
    i = line.new(x1=bar_index, y1=base * 0.98, x2=bar_index[1], y2=base * 0.98, extend=extend.both, color=color.purple, style=line.style_dashed, width=1)
    j = line.new(x1=bar_index, y1=base * 0.97, x2=bar_index[1], y2=base * 0.97, extend=extend.both, color=color.purple, style=line.style_dashed, width=1)
else
    na

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 via15