Edgoro
Compile error · Types and qualifiers

Pine Script hline error: series float vs input float (fix)

Why hline() rejects a moving average or any calculated value in Pine Script ("input float is expected"), and how to draw a moving level with plot().

Pine Editor console · line 3
Cannot call "hline" with argument "price"="call "ta.sma" (series float)". An argument of "series float" type was used but a "input float" is expected.

Cause

hline() draws one fixed horizontal level, so its price must be known before the first bar: a number or an input. A moving average changes on every bar.

Broken code

TradingView reports the error on line 3.

Pine Script v6
//@version=6
indicator("SMA level", overlay = true)
hline(ta.sma(close, 20), "SMA 20")

Fixed code

The same script, corrected for Pine Script v6. Paste it into the Pine Editor and save.

Pine Script v6
//@version=6
indicator("SMA level", overlay = true)
plot(ta.sma(close, 20), "SMA 20", color.orange)
level = input.float(100.0, "Fixed level")
hline(level, "Fixed level", color.gray, hline.style_dashed)

Why it works

hline() only accepts const or input values for its price. plot() accepts a series, so it draws a line that follows the value bar by bar. Keep hline() for levels that don't move, such as 70 and 30 on an RSI pane or a price you type into the settings.

A flat line at the latest value

If you want a horizontal line at the current value of the average rather than a curve, draw it with line.new() and move it on the last bar:

var line lvl = line.new(na, na, na, na, extend = extend.both, color = color.orange)
avg = ta.sma(close, 20)
if barstate.islast
    lvl.set_xy1(bar_index - 1, avg)
    lvl.set_xy2(bar_index, avg)

Questions

Why can't hline() use a variable?

It can, as long as the variable is a constant or an input, for example level = input.float(70.0). What it can't use is a value calculated from price, because that value changes on every bar and hline() draws a single fixed level.

How do I draw a horizontal line at the last price in Pine Script?

Create a line with line.new() and update its coordinates on the last bar, with extend.both or extend.right. If the line may follow the bars, plot() is simpler.

The scripts on this page are minimal examples written to show the error and its fix, not trading systems. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.

Still getting an error?

Paste your script and the message into the free Pine Script error fixer: it explains what went wrong and returns a corrected v6 version. Or describe the indicator in plain English and let Edgoro write the code.