How Do You Draw Lines in Tradingview

On the TradingView platform we can draw trend lines to analyse price movements and interpret the chart. But how does an indicator or strategy script make trend lines? Let's find out.

IN THIS ARTICLE:

# Create trend lines with TradingView code: line.new()

Trend lines are a helpful tool to analyse price charts. Since version 4 of TradingView's Pine Script, our code can also make trend lines. To have an indicator or strategy draw lines on the chart we execute the line.new() function in our code (TradingView, n.d.; TradingView Docs, n.d.).

On each price bar that we execute that function, a new trend line appears. After the line is made our code can make several changes to it, like update its location and change the line colour. And we can also delete trend lines. But all of that requires that we first make a trend line. Let's see how.

# Default pattern of TradingView's line.new() function

To make a trend line with code we specify two chart locations. TradingView then draws a line between those points.

At the bare minimum, we execute line.new() with four values (TradingView, n.d.):

The first argument (x1) sets the x-axis (time) coordinate for the line's first point. The y1 argument specifies the y-axis (price) coordinate for that point. Combined, that makes up the chart location from where to draw the line.

With the x2 and y2 arguments we set the time and price coordinates for the line's second point. That specifies where the line should end.

Here's a basic example:

                          line.new(x1=              bar_index              [              10              ]              ,              y1=              close              [              10              ]              ,              x2=              bar_index              ,              y2=              close)                      

Here we make a trend line with all four required arguments filled in. For the start coordinates we use the bar number from 10 bars ago (bar_index[10]) and the close from that same bar (close[10]). The line ends on the current bar number (bar_index) and close (close).

The line.new() function has more arguments than those four. With those extra arguments we configure how the line should look.

Here's what the function's complete pattern looks like (TradingView, n.d.):

                          line.new(x1,              y1,              x2,              y2,              xloc,              extend,              color, style,              width)                      
Argument Description
x1 Sets the time coordinate of the line's first point (required). We can use a bar number (default) or time value. That latter applies when the xloc argument is set to xloc.bar_time.
y1 Specifies the price coordinate of the line's first point (required).
x2 Sets the time coordinate of the line's second point (required). This can be a bar number (default) or time value. To use that latter we set the xloc argument to xloc.bar_time.
y2 Defines the price coordinate of the line's second point (required).
xloc Specifies which kind of x-axis (time) coordinates the line uses (optional). With xloc.bar_index the line is positioned with bar numbers. That is also the default behaviour. With xloc.bar_time the line's x-axis coordinates use time values. That makes it possible to draw the line in the future.
extend Defines the trend line's extension (optional). The default is extend.none, which doesn't make the line continue past its first and second point. With extend.right the line continues past its second point. extend.left has it go on beyond its first point. And extend.both has the line continue forever in both directions.
color Sets the line's colour (optional). We can use a standard colour, hexadecimal colour, or transparent colour here.
style Specifies the trend line's style (optional). The default is solid (line.style_solid). Other options are dotted (line.style_dotted), dashed (line.style_dashed), pointing arrows (line.style_arrow_left, line.style_arrow_right, and line.style_arrow_both).
width Sets the trend line's width in pixels (optional). The default line thickness is 1 and higher values make the line thicker.

A trend line that uses bar numbers for its time coordinates can only be drawn on the current and previous bars (TradingView, n.d.). So if we want to draw a trend line at a future time and date, then we cannot use bar numbers.

Instead we have to use time values with that kind of line. To do so we set the xloc argument of line.new() to the xloc.bar_time value. To get the time values themselves we can use the bar's time variables (time and time_close).

After making the trend line, line.new() returns a reference to that line (TradingView, n.d.). It's a good idea to save that value in a variable. That way we can access the line later. Else we wouldn't be able to move the trend line, change its style, or delete the line.

# Quick examples: draw trend lines with line.new()

Using the line.new() function is easier than all the different arguments in the table above suggest. Let's take a look.

# Create a basic trend line in TradingView

Most often when we use line.new(), we draw a trend line that uses bar numbers (bar_index). Here's how that looks:

                          //@version=4              study(title=              "Quick example: line.new()"              ,              overlay=              true)              // On the last price bar, make a new trend line              if              (barstate.islast)              line.new(x1=              bar_index              [              35              ]              ,              y1=              close              [              35              ]              ,              x2=              bar_index              ,              y2=              close)                      

The if statement in this mini-indicator looks if the script processes the chart's last price bar. When it does, the barstate.islast variable returns true. This way we make a trend line only on one price bar.

Inside the if statement we execute the line.new() function. We give that function four values. The first two specify the line's start point. That point's x-axis coordinate is bar_index[35], the bar number from 35 bars ago. The accompanying y-axis coordinate is that bar's close (close[35]).

The other two values set the line's end point. These are the current bar number (bar_index) for the time axis coordinate. And the current bar's close (close) for its price axis coordinate. With the line's two points defined, TradingView will now draw a line between them.

Here's how that trend line looks on the chart:

Example of a basic trend line in TradingView Pine

# Draw a trend line with a future time coordinate

By default, the lines that line.new() makes use bar numbers for their time coordinates.

But we can also set time coordinates with TradingView time values. That makes it possible to draw a line at a future location. That future location is some chart area to the right of the current bar.

Here's how we make a trend line with time values:

                          //@version=4              study(title=              "Quick example: line.new()"              ,              overlay=              true)              // On the chart's last bar, draw a line that extends              // one day in the future              if              (barstate.islast)              line.new(x1=              time[35              ]              ,              y1=              close              [              35              ]              ,              x2=              time              +              86400000              ,              y2=              close              *              0.99              ,              xloc=              xloc.bar_time)                      

Here we again only make a trend line on the chart's last price bar (barstate.islast). On that bar we execute the line.new() function.

There are three things we do with that function. First we set the line's starting point with the x1 and y1 arguments. That location is the opening time from 35 bars ago (time[35]). The price coordinate is the close of that bar (close[35]).

The second thing we do is define the line's second point (x2, y2). For that point's time coordinate we take the open time of the current bar (time) and increase it with 86,400,000.

TradingView's time values use milliseconds. So to calculate a future time coordinate we increase with a certain amount of milliseconds. That 86.4 million is how many milliseconds a day (24 hours) has.

We set the price coordinate of the line's second point to the current close (close) multiplied with 0.99. That gives a one percent lower price value.

For the third and final thing we set the xloc argument of the line.new() function to xloc.bar_time. That tells TradingView the line should use time values for its x-axis coordinates rather than the default bar numbers.

Here's how the trend line drawn into the future looks like:

Example of a TradingView trend line drawn in the future

# Configure a trend line with line.new() arguments

The previous examples only used the minimum arguments that line.new() needs. But as we can tell from the table above, line.new() has more arguments. Those optional arguments make it possible to configure how the line should look.

Here's a quick example:

                          //@version=4              study(title=              "Quick example: line.new()"              ,              overlay=              true)              // Create a formatted trend line on the chart's last bar              if              (barstate.islast)              line.new(x1=              bar_index              [              20              ]              ,              y1=              high              [              20              ]              ,              x2=              bar_index              ,              y2=              low              ,              color=              color.lime              ,              width=              10              ,              style=              line.style_dashed              ,              extend=              extend.both)                      

This indicator again only makes a trend line on the chart's last price bar. On that bar we execute line.new() with several arguments.

The first four specify the line's location. We set its first point to the bar number and high from 20 bars ago (bar_index[20], high[20]). Its second point is the current bar's low (bar_index, low).

The other arguments specify how the line looks. With color set to color.lime our line becomes lime green. With width we increase the line's size to 10 pixels. Then we use style to set the line's style to a dashed line (line.style_dashed). Finally we extend the trend line in both directions (extend=extend.both).

Here's how that formatted line looks on the chart:

Example of a formatted TradingView trend line

# How to change existing trend lines line.new() made?

Often drawing a trend line with the line.new() function is just the first step. Once the line is made our script has to move, change, or delete the line. But how to do so?

There are two requirements to change an existing trend line:

  • We need to store the line reference that line.new() returns in a variable.
  • And then use that variable with any of TradingView's line.* functions to modify the line.

Let's take a closer look at both points.

# Put the value line.new() returns in a variable

After the line.new() function draws a trend line on the chart, it returns a so-called line reference (TradingView, n.d.). That value is pretty important, because it's the only connection that our code has with the trend line on the chart.

Any operation we want to do with that trend line, requires that we provide the line reference. Else TradingView doesn't know what line we want to modify.

Here's an example of how that works in practice:

                          // Draw a new trend line              myLine              =              line.new(x1=              bar_index              [              10              ]              ,              y1=              high              [              10              ]              ,              x2=              bar_index              ,              y2=              high)              // Change the line's colour to red              line.set_color(id=myLine,              color=              color.red)                      

This snippet draws a trend line between the high of 10 bars ago and the current bar high. That's just like the earlier trend lines we made above.

But this time we also store the value that line.new() returns in the myLine variable. That make it possible to use that variable to access the line.

An example of that is the line.set_color() function call. This function can change the colour of a trend line. But for that it needs to know which line to change. Here we give that information through the myLine variable. That way we set the line we just made to red (color.red).

It's not required to store the line reference that line.new() returns in a variable. But 95% of the time you want to do that. Because that makes it possible to access the line later.

We cannot get a line reference in any other way. So if you don't store line.new()'s returned value in a variable, there's no way to change, move, or delete the trend line later.

# Modify a trend line with specialised TradingView functions

The trend lines that the line.new() function makes cannot be modified by hand (TradingView Docs, n.d.). Instead we use specialised functions that modify something of a trend line.

What all those functions have in common is that they require a line reference. Without such a thing, they don't know which line to change. This shows how useful it is to store the value that line.new() returns in a variable.

Let's see what functions there are. To change a line's visual appearance, our code uses:

  • line.set_color() changes the trend line's colour.
  • line.set_width() sets the trend line's size (thickness).
  • line.set_style() defines the line style (such as dotted, dashed, or solid).
  • line.set_extend() extends the trend line in one or both directions.

To move a trend line we update its coordinates with one of these functions:

  • line.set_xy1() and line.set_xy2() set the line's price and time coordinates of its first and second point, respectively.
  • line.set_x1() and line.set_x2() set the time coordinates of the line's first and second point.
  • line.set_y1() and line.set_y2() set the price coordinates of the line's first and second point.

And to retrieve a line's coordinates, we use:

  • line.get_x1() and line.get_x2() return the bar number (or time) coordinates of the line's first and second point.
  • line.get_y1() and line.get_y2() return the price coordinate of the line's first and second point.

And to remove a line from the chart, we use:

  • line.delete() deletes a trend line from the chart.

# Set line appearance with line.new() or custom functions?

So there are several functions that can change a line's appearance. But we can also set a line's visual style with the arguments of the line.new() function. So what's the difference?

In general, if you want to set your line's appearance once, it's easier to use the line.new() function. That requires less code typing. But if you want to format a line based on some condition, then you have to use a specialised function.

For example, let's say a trend line should be red all the time. In that case it's more convenient to set the color argument of the line.new() function to color.red. That requires less code than a separate line.set_color() statement.

But if the line should be red only when the bar trades under the moving average, then we combine an if statement with the line.set_color() function to set the line's colour conditionally.

# Example indicator: highs and lows lines with line.new()

Now let's see how we use the line.new() function in a complete script. The indicator below makes two trend lines. One line shows the 20-bar highest high; the other depicts the lowest low over the same time period.

We of course make those lines with line.new(). But once they're on the chart, we update them whenever a new 20-bar highest high or lowest low happens.

This is the indicator's full code:

                          //@version=4              study(title=              "Basic trend line example"              ,              overlay=              true)              // Make both trend lines just once                              var                highLine                =                line.new(x1=                bar_index                [                10                ]                ,                y1=                close                [                10                ]                ,                                            x2=                bar_index                ,                y2=                close                ,                color=                color.green                ,                                            extend=                extend.both)                                            var                lowLine                =                line.new(x1=                bar_index                [                10                ]                ,                y1=                close                [                10                ]                ,                                            x2=                bar_index                ,                y2=                close                ,                color=                color.red                ,                                            extend=                extend.both)                            // Look for new 20-bar highs and lows              newHigh              =              (high              ==              highest(high              ,              20)) newLow              =              (low              ==              lowest(low              ,              20))              // Update the lines when there's a new high or low              if              (newHigh)              line.set_xy1(id=highLine,              x=              bar_index              [              10              ]              ,              y=              high)              line.set_xy2(id=highLine,              x=              bar_index              ,              y=              high)              if              (newLow)              line.set_xy1(id=lowLine,              x=              bar_index              [              10              ]              ,              y=              low)              line.set_xy2(id=lowLine,              x=              bar_index              ,              y=              low)          

Let's go through the code together. First we define the indicator's settings with TradingView's study() function. Then we make two trend lines:

                          // Make both trend lines just once              var              highLine              =              line.new(x1=              bar_index              [              10              ]              ,              y1=              close              [              10              ]              ,              x2=              bar_index              ,              y2=              close              ,              color=              color.green              ,              extend=              extend.both)              var              lowLine              =              line.new(x1=              bar_index              [              10              ]              ,              y1=              close              [              10              ]              ,              x2=              bar_index              ,              y2=              close              ,              color=              color.red              ,              extend=              extend.both)                      

Here we execute line.new() twice. The first line starts at the close 10 bars ago (bar_index[10], close[10]). The line ends on the current bar number (bar_index) and current close (close). This line shows in green (color.green) and extends in both directions (extend.both). We store the line reference that line.new() returns in the highLine variable. Later we'll use that variable to access and modify the trend line.

The second line.new() function call makes another trend line. That line gets the same coordinates as the previous line. Those just act as temporary placeholders; later code will move both trend lines to their actual location. This second line appears in red (color.red) and extends in both directions (extend.both). We put the line reference in the lowLine variable.

Note that both lines have the var keyword before them. Statements with that keyword only execute once. So these two line.new() calls will only make two trend lines in total, and not two trend lines on each and every bar. This is one way to prevent that the chart gets cluttered with trend lines.

Next we look if new 20-bar highs and lows happened:

                          // Look for new 20-bar highs and lows              newHigh              =              (high              ==              highest(high              ,              20)) newLow              =              (low              ==              lowest(low              ,              20))                      

Here we compare if the current bar's high (high) equals (==) the 20-bar highest high, which we get with the highest() function. When that's the case, the newHigh variable we make here is true (and false otherwise).

Similarly, we then look if the bar's low (low) is (==) the 20-bar lowest low returned by the lowest() function. In that scenario the newLow variable holds true (and is false otherwise).

Knowing if a new 20-bar extreme happened is the first step. Next we got to update the trend lines to those values:

                          // Update the lines when there's a new high or low              if              (newHigh)              line.set_xy1(id=highLine,              x=              bar_index              [              10              ]              ,              y=              high)              line.set_xy2(id=highLine,              x=              bar_index              ,              y=              high)              if              (newLow)              line.set_xy1(id=lowLine,              x=              bar_index              [              10              ]              ,              y=              low)              line.set_xy2(id=lowLine,              x=              bar_index              ,              y=              low)                      

The first if statement looks if a new 20-bar high happened. When it did, newHigh is true. In that scenario we got to update the highest high line to the current 20-bar high.

For that we first call the line.set_xy1() function to update the line's first point. The new coordinates are the bar number of 10 bars ago (bar_index[10]) and the current bar's high (high). We use the highLine variable to identify which line to change. Then we execute the line.set_xy2() function to modify the line's second point. That location we update to the current bar's high (bar_index, high).

The second if statement evaluates the newLow variable. When that one is true, the current bar hit a new 20-bar low. So we move the lowest low trend line (lowLine) to that new low.

We update that line's first point with line.set_xy1(). That new location is the bar number from 10 bars ago and the current bar's low (low). Then line.set_xy2() updates the line's second point. Those new coordinates are the current bar number and low.

Note that we earlier extended both trend lines to the left and right. That makes them cross the entire chart. So even though we update the line's location to the current bar and 10 bars ago, the lines cross much more bars than those.

Here's how those trend lines look on the chart:

Example of extended high and low trend lines in TradingView

On this USD/CAD chart all of the recent price action is captured between the highest high and lowest low lines. This is also luck because the trend lines won't always look as nice as they do here.

# Features of TradingView's line.new() function

The line.new() function has a couple of features worth pointing out:

  • Trend lines can be drawn on the chart's instrument or inside a chart panel where the script plots data. Which of these two depends on the overlay argument of the study() and strategy() functions.
  • By default, trend lines use bar numbers for their time coordinates. But when they use time values, we can draw trend lines in the future as well. To use time values with a line:
    • Set the xloc argument of the line.new() function to xloc.bar_time, or
    • use the line.set_xloc() function to switch an existing line to using time values.
  • The lines we make with line.new() cannot be modified by hand. We have to use code for that.
  • A single script instance can make around 50 to 55 drawings on the chart. When our code tries to make more, TradingView automatically removes the oldest drawings. Details are in the maximum number of TradingView drawings.
  • Trend lines don't always persist on the chart. Those we make during an intra-bar script calculation are automatically removed with the next script calculation. That way not every real-time price update creates a new line (which would clutter the chart quickly). See intra-bar drawings are removed automatically for details.
  • Each line.new() function call creates at most one trend line per price bar. If you want to make multiple trend lines on a single bar, then your code has use the line.new() function more than once.
  • The trend lines that line.new() makes remain on the chart for as long as the script that made them is active. When we disable or remove that script, the lines are gone.
  • While we can load data from other instruments with TradingView's security() function, we cannot draw trend lines on the data series that function returns (TradingView Docs, n.d.).
  • The lines that a script makes with the line.new() function can only be modified by that indicator or strategy. Another script cannot change, relocate, or delete those trend lines.
  • Code cannot retrieve or fetch trend lines from the chart. The only way to get access to a trend line is by storing the reference that the line.new() function returns in a variable. Without that we cannot change, move, or delete the line.

# Summary

TradingView indicators and strategies draw trend lines with the line.new() function. That function has four required arguments. x1 and y1 define the coordinates of the line's first point. x2 and y2 specify the line's second point. The function's other arguments set the line's visual appearance.

After line.new() made a trend line, it returns a reference to that line. That value is pretty important. The reason why is that we cannot manually modify the lines that line.new() made. The only way to change, move, or delete a trend line is with specialised TradingView functions. And all those functions require a line reference so that they know which line to modify.

The line.new() function can make one trend line per price bar. To make several lines on a single bar we use the function more than once in our code. The total number of drawings (including trend lines) a script can make also has a maximum. That limit is around 50 drawings.

Published .

« All TradingView trend lines articles

How Do You Draw Lines in Tradingview

Source: https://kodify.net/tradingview/lines/new/

0 Response to "How Do You Draw Lines in Tradingview"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel