Élodie Petit
Élodie Petit

Reputation: 5914

hline doesn't work as expected in PineScript

How come the code below works:

//@version=3
study("My Script", overlay=true)
price = (ticker == "EURUSD" ? 1.1600 : 1.1500)
hline(1.1500, title='Pi', color=blue, linestyle=dotted, linewidth=2)
plot(price)

But this doesn't:

//@version=3
study("My Script", overlay=true)
price = (ticker == "EURUSD" ? 1.1600 : 1.1500)
hline(price, title='Pi', color=blue, linestyle=dotted, linewidth=2)
plot(price)

Only difference is using a variable for the hline parameter.

Upvotes: 2

Views: 2866

Answers (2)

Nique
Nique

Reputation: 583

Workaround

line hLine = line.new(
      x1     = bar_index-1, 
      y1     = price,
      x2     = bar_index,
      y2     = price,
      xloc   = xloc.bar_index,
      color  = color.blue,
      extend = extend.both,
      style  = line.style_dotted,
      width  = 2)
line.delete(hLine[1])

Upvotes: 1

vitruvius
vitruvius

Reputation: 21294

Well, the idea of hline() is to have a horizontal line at a fixed price. If you use a variable for the price parameter, then it is not "fixed" anymore. Because a variable can have a different value during run-time. Hence, you cannot use variables.

Upvotes: 4

Related Questions