How make a hline value dynamic? i.e not user defined

Viewed 279

I'm trying to code a dynamic hline in tradingview pinescript so that the hline value changes depending on price, volume, rsi etc etc. below is an example of a hline thats not dynamic:

userinput = input.int(10, "value)
hline(userinput, title = "title", color = color.new(#ffffff, 0), linestyle = hline.style_dotted, linewidth = 1)

What I'd like is for the hline to be dynamic instead of being user defined, for example:

dynamicvalue = math.avg(variable, anothervariable)
hline(dynamicvalue, title = "dynamic title", color = color.new(#ffffff, 0), linestyle = hline.style_dotted, linewidth = 1)
1 Answers

hline() cannot be dynamic.

You can either use lines or the following trick with the plot() function.

The following indicator will draw horizontal lines from pivot high and low levels. The trick is, using style=plot.style_circles in plot().

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

var float ph = na
var float pl = na

_ph = ta.pivothigh(n, n)
_pl = ta.pivotlow(n, n)

ph := _ph ? _ph : ph
pl := _pl ? _pl : pl

plot(ph, style=plot.style_circles, color=color.green, offset=-n)
plot(pl, style=plot.style_circles, color=color.red, offset=-n)

enter image description here

Related