Pine now does polyline drawings

Oct 25, 2023

Polyline drawing objects allow you to display irregular shapes using an array of point coordinates. Points are joined using curved or straight lines. The shape can be closed or open, and you can control its line and fill attributes.

You create polylines by first assembling an array of point coordinates using the new chart.point objects, where x-coordinates can be expressed as either a time or bar index, and y-coordinates are price. Once you have an array of points, you can use it to create a polyline using polyline.new().

Let’s look at the first example where we put all this together. This script uses interactive inputs to define five points on the chart and creates a polyline from them:

//@version=5
indicator("Polylines example", overlay = true)

//@variable If `true`, uses curved lines to connect the points.
bool curvedInput = input.bool(false, "Curved polyline")
//@variable If `true`, connects the polyline's first and last points.
bool closedInput = input.bool(true,  "Closed polyline")
//@variable The color of the space filled by the polyline.
color fillcolor = input.color(color.new(color.blue, 90), "Fill Color")

// Time and price inputs for the polyline's points.
p1x = input.time(0,  "p1", confirm = true, inline = "p1")
p1y = input.price(0, "  ", confirm = true, inline = "p1")
p2x = input.time(0,  "p2", confirm = true, inline = "p2")
p2y = input.price(0, "  ", confirm = true, inline = "p2")
p3x = input.time(0,  "p3", confirm = true, inline = "p3")
p3y = input.price(0, "  ", confirm = true, inline = "p3")
p4x = input.time(0,  "p4", confirm = true, inline = "p4")
p4y = input.price(0, "  ", confirm = true, inline = "p4")
p5x = input.time(0,  "p5", confirm = true, inline = "p5")
p5y = input.price(0, "  ", confirm = true, inline = "p5")

// Draw only once on the last historical bar, so the script runs faster.
if barstate.islastconfirmedhistory
    //@variable An array of `chart.point` objects for the polyline.
    var points = array.new<chart.point>()
    // Add the user-defined points to the `points` array.
    points.push(chart.point.from_time(p1x, p1y))
    points.push(chart.point.from_time(p2x, p2y))
    points.push(chart.point.from_time(p3x, p3y)) 
    points.push(chart.point.from_time(p4x, p4y))
    points.push(chart.point.from_time(p5x, p5y))
    // Create a new polyline from the `points` array.
    polyline.new(points, curved = curvedInput, closed = closedInput, fill_color = fillcolor, xloc = xloc.bar_time) 
    // Display the name of each point on the chart.
    for [pointNo, point] in points
        label.new(point, text = "p" + str.tostring(pointNo + 1), xloc = xloc.bar_time, color = na, textcolor = chart.fg_color)

Two new sets of functions help you with chart points and polylines.

Create chart points using:

To handle polylines, use the following functions:

  • polyline.new(): Creates a new polyline instance and displays it on the chart, sequentially connecting all the points in the points array with line segments.
  • polyline.delete(): Deletes the specified polyline object.
  • polyline.all: Returns an array containing all the current polyline instances created by the script.

The new feature comes with a max_polylines_count parameter for the indicator() and strategy() declaration statements. The default value is 50 and its maximum, 100.

Our new User Manual section will help you get started with polylines.

These recent publications from our Community Scripts are further examples of scripts using polylines:

Volume Profile with a few polylines indicator by Fikira

Zig-Zag Volume Profile indicator by KioseffTrading

RSI Radar Multi Time Frame indicator by LonesomeTheBlue

MA Sabres [LuxAlgo] indicator by LuxAlgo

To stay up to date on new Pine Script® features, keep an eye on the User Manual’s Release notes. The PineCoders account also broadcasts updates from its Squawk Box on Telegram, its Twitter account, and from the Pine Script® Q&A public chat on TradingView.

We hope you find this highly-requested feature as useful as we think it’ll be, and please do keep sending us your feedback and suggestions, so we can make the platform the best it can be. We build TradingView for you, and we’re always keen to hear your thoughts.

Look first Then leap

TradingView is built for you, so make sure you're getting the most of our awesome features
Launch Chart