TradingCharts

Lines and Boxes

Lines, boxes, and polylines are drawing objects: you create them with .new(), hold the returned reference, and later modify or delete it.

line.new()

line.new(x1, y1, x2, y2, xloc?, extend?, color?, style?, width?)
  • x1/x2 default to bar_index coordinates; set xloc = xloc.bar_time to use epoch-ms times instead.
  • extend is one of extend.none (default), extend.left, extend.right, extend.both.
  • style is line.style_solid (default), line.style_dashed, line.style_dotted, line.style_arrow_left, line.style_arrow_right, line.style_arrow_both.
if ta.crossover(close, ta.sma(close, 20))
    line.new(bar_index, close, bar_index + 20, close + ta.atr(14) * 2,
             color=color.lime, style=line.style_dashed, extend=extend.right)

Updating a line

line.set_xy1(l, bar_index, close)
line.set_xy2(l, bar_index + 50, close)
line.set_color(l, color.red)
line.set_width(l, 2)
line.set_style(l, line.style_dotted)
line.set_extend(l, extend.right)

Deleting

line.delete(l)

A deleted reference becomes na.

box.new()

box.new(left, top, right, bottom, border_color?, border_width?, border_style?,
        bgcolor?, text?, text_color?, text_size?, text_halign?, text_valign?,
        xloc?, extend?)
if volume > ta.sma(volume, 20) * 2
    box.new(bar_index - 1, high + 2, bar_index + 1, low - 2,
            border_color=color.yellow, bgcolor=color.new(color.yellow, 85),
            text="Volume spike", text_color=color.yellow, text_size=size.tiny,
            text_halign=text.align_center, text_valign=text.align_top)

Updates mirror lines: box.set_lefttop, box.set_rightbottom, box.set_bgcolor, box.set_border_color, box.set_border_width, box.set_border_style, box.set_text, box.set_text_color, box.set_extend, box.delete.

polyline.new()

A polyline is a connected sequence of segments:

points = array.new<chart.point>()
for i = 0 to 20
    points.push(chart.point.new(bar_index[i], high[i]))
polyline.new(points, color=color.lime, line_width=2, line_style=line.style_solid, curved=false)
  • curved = true fits a Bezier curve through the points instead of straight segments.
  • closed = true connects the last point back to the first.
  • Caps on polylines are configured via max_polylines_count on the declaration (default and maximum 100).

linefill.new()

Fills a region between two line.new() drawings:

up = line.new(bar_index - 20, high[20], bar_index, high, color=color.lime)
dn = line.new(bar_index - 20, low[20],  bar_index, low,  color=color.red)
linefill.new(up, dn, color=color.new(color.yellow, 92))

Z-order among drawings

Drawings sit on top of plots in order of declaration: the last new() call renders on top. Reordering is done by deleting and re-creating.

Reference lifetimes

  • A drawing persists until you delete it or the chart is destroyed.
  • The script's declaration caps (max_lines_count, max_boxes_count, …) govern how many can coexist; exceeding the cap deletes the oldest.
  • Store references in var or array<line>/array<box> to survive across bars.

See also