This tutorial will delve into the ta.linreg()
function, exploring its syntax, arguments, and application through practical examples.
Syntax of ta.linreg()
The syntax for the linear regression function in Pine Script is as follows:
ta.linreg(source, length, offset) → series float
This function returns a series of floating-point numbers that represent the values of the linear regression curve.
Arguments Explained
- source (series int/float): This is the data series on which the linear regression calculation will be performed. It can be any series of prices, such as closing prices, opening prices, or even a series of indicators.
- length (series int): This argument specifies the number of bars, or periods, over which the linear regression should be calculated. A larger length will consider more data points, making the line smoother and less reactive to recent price changes.
- offset (simple int): The offset argument allows you to shift the linear regression curve forward or backward. An offset of 0 means the curve is calculated with the most recent data point included. A positive offset moves the curve forward, meaning it is based on past data and excludes recent data points.
How It Works
The ta.linreg()
function computes the linear regression line by applying the least squares method to the specified source series over a defined length. The formula used is:
linreg = intercept + slope * (length - 1 - offset)
Where:
- intercept and slope are calculated using the least squares method on the source series.
- length is the number of periods over which the calculation is performed.
- offset adjusts the position of the regression line relative to the current bar.
Practical Example
Let’s apply the ta.linreg()
function to a simple script that plots the linear regression line of the closing prices over the last 20 bars:
//@version=5 indicator("My Linear Regression Line", overlay=true) source = close length = 20 offset = 0 regressionLine = ta.linreg(source, length, offset) plot(regressionLine, color=color.red)
Detailed Walkthrough
//@version=5
- This line specifies the version of Pine Script being used, which is version 5 in this case. Pine Script versions update over time, introducing new functionalities and syntax changes.
indicator("My Linear Regression Line", overlay=true)
- This line declares a new indicator script titled “My Linear Regression Line.”
- The
overlay=true
argument specifies that this indicator should be drawn directly on the main chart, overlaying the price action, rather than in a separate pane below the chart.
source = close
- This line defines a variable named
source
and assigns it the value ofclose
, which represents the closing prices of the bars on the chart. - The
source
variable is used as the input for the linear regression calculation, meaning the regression line will be based on the closing prices.
- This line defines a variable named
length = 20
- This line sets a variable named
length
to 20, indicating the number of bars to be included in the linear regression calculation. - A
length
of 20 means the regression line will be calculated using the last 20 bars of data from thesource
series.
- This line sets a variable named
offset = 0
- This line initializes an
offset
variable with a value of 0. - The
offset
is used to shift the linear regression line forward or backward in time. Anoffset
of 0 means there is no shift, and the line will be plotted based on the most recent data.
- This line initializes an
regressionLine = ta.linreg(source, length, offset)
- This line calculates the linear regression line using the
ta.linreg()
function from Pine Script’s built-in technical analysis library. - It passes the previously defined
source
,length
, andoffset
variables as arguments to the function. The result is a series of values representing the linear regression line, which is then stored in theregressionLine
variable.
- This line calculates the linear regression line using the
plot(regressionLine, color=color.red)
- This final line plots the calculated linear regression line on the chart.
- The
plot()
function is called with theregressionLine
series as its first argument, which tells Pine Script what data to draw on the chart. - The
color=color.red
argument specifies that the plotted line should be colored red, making it visually distinct on the chart.
Key Features and Takeaways
- Function Usability:
ta.linreg()
is versatile, allowing for the analysis of any series of data, not just price. - Syntax and Application: It requires three arguments: source, length, and offset, making it adaptable to various trading strategies.
- Analytical Power: By adjusting the length and offset, traders can fine-tune the sensitivity and positioning of the linear regression curve, tailoring it to their analysis needs.