"Pine Script polyline.new: draw shapes, zigzags and curves"
How polyline.new works in Pine Script v6 with chart.point, closed and filled shapes, curved lines, and a working zigzag example.
polyline.new(points) draws one line through a list of points. Where line.new() joins two points, a polyline joins as many as you like, so a whole zigzag, a triangle or a smooth curve is a single drawing. It can be closed into a shape and filled. SuperCharting supports polyline.new in Pine Script v6.
The points: chart.point
A polyline takes an array<chart.point>. Each point is a bar and a price:
| Function | Point at |
|---|---|
chart.point.from_index(bar_index, price) | a bar number (default xloc.bar_index) |
chart.point.from_time(time, price) | a bar time (use xloc = xloc.bar_time on the polyline) |
chart.point.now(price) | the current bar |
A working example: a filled triangle
//@version=6
indicator("Triangle", overlay = true)
hi = ta.highest(high, 30)
lo = ta.lowest(low, 30)
if barstate.islast
pts = array.new<chart.point>()
pts.push(chart.point.from_index(bar_index - 28, lo))
pts.push(chart.point.from_index(bar_index - 20, hi))
pts.push(chart.point.from_index(bar_index - 12, lo))
polyline.new(pts, closed = true, line_color = color.purple,
fill_color = color.new(color.purple, 80), line_width = 2)
closed = true joins the last point back to the first, and fill_color colours the inside.
A zigzag through pivots
//@version=6
indicator("Pivot zigzag", overlay = true, max_polylines_count = 1)
len = input.int(5, "Pivot length")
var pts = array.new<chart.point>()
ph = ta.pivothigh(high, len, len)
pl = ta.pivotlow(low, len, len)
if not na(ph)
pts.push(chart.point.from_index(bar_index - len, ph))
if not na(pl)
pts.push(chart.point.from_index(bar_index - len, pl))
if pts.size() > 50
pts.shift()
var polyline zz = na
if barstate.islast and pts.size() > 1
polyline.delete(zz)
zz := polyline.new(pts, line_color = color.orange, line_width = 2)
The script collects pivot highs and lows as they are confirmed and draws one polyline through the last 50 of them on the newest bar.
The arguments
| Argument | What it does |
|---|---|
curved | true draws a smooth curve through the points instead of straight segments |
closed | true joins the last point to the first |
fill_color | colours the inside of a closed shape |
line_color, line_width, line_style | the outline (line.style_solid, _dashed, _dotted) |
xloc | xloc.bar_index (default) or xloc.bar_time, matching how the points were made |
force_overlay | draws on the price chart from a pane indicator, see the force_overlay guide |
How it behaves
- A polyline cannot be edited. There are no setters. To change one,
polyline.delete()it and create a new one, as the zigzag does. - Delete before you redraw. Creating a new polyline on every bar without deleting the old one stacks drawings until the script's
max_polylines_countis reached. - Points with
naare skipped.
How to try it
Open the Pine Editor at the bottom of a chart, paste an example and click Add to chart. The free Pine Script checker checks a script without an account.
Frequently asked questions
What is the difference between polyline and line in Pine Script?
line.new() joins two points and can be moved with setters. polyline.new() joins any number of points, can be curved, closed and filled, and is redrawn instead of edited.
Why is my polyline not filled?
The fill needs closed = true and a fill_color. An open polyline has no inside to fill.