In this tutorial, we’ll focus on one function from this powerful scripting language: the line.new
function. This function provides a unique way to draw lines directly onto a chart in your script.
What is line.new?
The line.new
function is used to create a new line object in Pine Script. This line object is displayed directly on a TradingView chart, allowing you to visually illustrate a particular aspect of your indicator or strategy.
Here is the general syntax for this function:
line.new(x1, y1, x2, y2, xloc, extend, color, style, width) → series line
Line.new Arguments
Let’s break down each argument in this function to understand what they do.
x1 and x2
The x1
and x2
arguments represent the bar index (if xloc
is set to xloc.bar_index
) or the bar’s UNIX time (if xloc
is set to xloc.bar_time
) of the first and second point of the line respectively.
y1 and y2
y1
and y2
denote the price of the first and second points of the line respectively.
xloc
The xloc
argument can take two values: xloc.bar_index
and xloc.bar_time
. This argument decides whether the x-values of the line’s points are based on the bar’s index or the bar’s UNIX time.
extend
The extend
argument controls how the line is drawn beyond the two points provided. extend.none
draws a segment between the two points, extend.right
and extend.left
draw rays extending from the first or second point, and extend.both
draws a straight line through both points.
color
This argument allows you to specify the color of the line.
style
With the style
argument, you can choose the line’s style, with options such as solid, dotted, dashed, and different types of arrows.
width
Finally, the width
argument lets you set the line’s width in pixels.
Example Use Case
Let’s take a look at a practical example to understand how these arguments come together:
//@version=5 indicator("line.new Example") var line1 = line.new(0, low, bar_index, high, extend=extend.right) var line2 = line.new(time, open, time + 60 * 60 * 24, close, xloc=xloc.bar_time, style=line.style_dashed) line.set_x2(line1, 0) line.set_xloc(line1, time, time + 60 * 60 * 24, xloc.bar_time) line.set_color(line2, color.green) line.set_width(line2, 5)

In this example, two lines are created. The first line line1
is drawn from the bar’s low to high, extending to the right. The second line line2
is drawn from the current bar’s open to the bar’s close 24 hours later, in dashed style.
Then we modify the line1
‘s second x-coordinate to be the same as the first (essentially creating a vertical line) and change its x location to be based on time.
Finally, the color and width of line2
are changed to green and 5 pixels respectively using the line.set_color
and line.set_width
functions.
Key Takeaway
The line.new
function in Pine Script is a powerful tool for creating customizable line objects on your charts. By understanding each argument in the function, you can create dynamic and visually informative representations of your indicators or strategies.
Conclusion
Pine Script provides a rich set of functions to build custom indicators and strategies. The line.new
function is a particularly useful part of this toolset, giving you the ability to draw and customize lines on your charts. With a bit of practice, you’ll be able to incorporate this functionality into your own scripts, enhancing both their usability and their visual appeal. Happy coding!