Pine Script is a powerful scripting language developed by TradingView. One of its unique features is the line
function. In this blog post, we will delve deep into understanding how to use this function in your trading strategies.
Understanding the Line Function
line
is a keyword used to explicitly declare the “line” type of a variable or a parameter. It’s an essential tool that traders use to draw lines on charts dynamically, based on logic specified in the Pine Script.
Let’s break down the initial provided example to understand each line of the code.
//@version=5 indicator("line") // Empty `line1` line ID. var line line1 = na // `line` type is unnecessary because `line.new()` returns "line" type. var line2 = line.new(na, na, na, na) line3 = line.new(bar_index - 1, high, bar_index, high, extend = extend.right)
//@version=5
This specifies the Pine Script version used in the code. As of the time of writing, the latest version is 5.
indicator("line")
This line declares a new indicator with the title “line”.
var line line1 = na
This line initializes a new variable of type line
called line1
. It’s assigned the na
value, which stands for “not a number”. This indicates that the line doesn’t have any points or coordinates yet.
var line2 = line.new(na, na, na, na)
Here, we create a new line object without specifying any coordinates. The line.new()
function returns a “line” type, so specifying line
before line2
is not necessary.
line3 = line.new(bar_index - 1, high, bar_index, high, extend = extend.right)
This line creates a new line object, line3
, with specified coordinates. It draws a line at the high price of the previous bar (bar_index - 1, high
) to the high price of the current bar (bar_index, high
). The extend = extend.right
parameter means that the line will be extended to the right indefinitely.
A Unique Use Case: Drawing a Trendline
Now, let’s consider a unique use case. We’ll use the line
function to draw a trendline between two recent swing highs on a chart.
//@version=5 indicator("Trendline", overlay = true) var line myLine = na // Identifying swing highs swingHigh = ta.highest(high, 5) <= high//@version=5 indicator("Trendline", overlay = true) var line myLine = na // Identifying swing highs swingHigh = ta.highest(high, 5) <= high if swingHigh if na(myLine) line.new(bar_index -5 , high, bar_index, high) else line.set_xy1(myLine, na , high) line.set_xy2(myLine, bar_index, high) if swingHigh if na(myLine) line.new(bar_index -5 , high, bar_index, high) else line.set_xy1(myLine, na , high) line.set_xy2(myLine, bar_index, high)
Here’s a breakdown of what the code does:
indicator("Trendline", overlay = true)
We declare a new indicator titled “Trendline” and set overlay = true
to draw directly on the price chart.
var line myLine = na
An empty line
variable myLine
is created for future use.
swingHigh = ta.highest(high, 5) == high
This line of code identifies swing highs on the chart. The ta.highest(high, 5)
function returns the highest value of the “high” series over the past 5 bars.
if swingHigh
The if
statement checks if a swing high has been identified.
line.new(bar_index -5, high, bar_index, high)
When a swing high is found and myLine
is na
(meaning no line has been drawn yet), we create a new line at the current bar’s swing high.
line.set_xy1(myLine, na, high) and line.set_xy2(myLine, bar_index, high)
When another swing high is found, we update the line to extend from the previous swing high to the current one. line.set_xy1
sets the initial point of the line, and line.set_xy2
sets the final point of the line.
Key Takeaway
The line
function in Pine Script is an instrumental tool that allows traders to draw and manipulate lines on charts dynamically. Understanding how to use it effectively can greatly enhance your strategy development process in TradingView.
Conclusion
This blog has shown you how to declare and use the line
function in Pine Script, using a unique example of drawing a trendline between swing highs. Now, you should be well-equipped to incorporate dynamic line drawing into your own trading strategies. Happy coding