Skip to content

line namespace

The line namespace exposes functions to draw geometric lines on the price chart. Each call returns a handle you can reuse to move the endpoints, restyle the stroke, extend in either direction, or delete the line when it is no longer needed.

Create line objects once with var, then update their coordinates on subsequent bars. This prevents leaking objects and keeps only a single handle per line alive.

FunctionPurpose
line.new(x1, y1, x2, y2, xloc, extend, color, style, width)Create a line between two bar/price coordinates.
line.set_xy1(id, x, y) / line.set_xy2(id, x, y)Move the first or second anchor point.
line.set_color(id, color) / line.set_width(id, width)Restyle an existing line.
line.set_style(id, style)Switch to solid, dashed, dotted, or arrow styles.
line.set_extend(id, extend)Extend the line left, right, both, or neither.
line.delete(id)Remove the line from the chart.
//@version=5
indicator("Dynamic channel via line namespace", overlay=true, max_lines_count=2)
length = input.int(50, "Lookback", minval=2)
upper = ta.highest(high, length)
lower = ta.lowest(low, length)
var line upperLine = na
var line lowerLine = na
if barstate.islast
if na(upperLine)
upperLine := line.new(bar_index - length + 1, upper,
bar_index, upper,
extend = extend.right,
color = color.new(color.red, 0),
width = 2)
lowerLine := line.new(bar_index - length + 1, lower,
bar_index, lower,
extend = extend.right,
color = color.new(color.green, 0),
width = 2)
else
line.set_xy1(upperLine, bar_index - length + 1, ta.highest(high, length))
line.set_xy2(upperLine, bar_index, upper)
line.set_xy1(lowerLine, bar_index - length + 1, ta.lowest(low, length))
line.set_xy2(lowerLine, bar_index, lower)