LinearReg (Function)
The LinearReg function calculates the slope and angle of a linear regression line and allows you identify the price where the projected line crosses a future (or past) bar position.
Syntax
LinearReg(Price, Length, TgtBar, oLRSlope, oLRAngle, oLRIntercept, oLRValue)
Returns (Integer)
The oLRSlope, oLRAngle, oLRIntercept, and oLRValueRaw output parameters return the slope, angle, intercept, and regression value. The LinearReg function itself returns a value of 1.
Parameters
Name |
Type |
Description |
Price |
Numeric |
Specifies which bar value (price, function, or formula) to use for the calculating the regression line. |
Length |
Numeric |
Sets the number of bars to consider. |
TgtBar |
Numeric |
Sets a target bar position in the future (or past). Use a negative integer for a future bar, a positive integer for a previous bar, and zero for the current bar. |
oLRSlope |
Numeric |
Outputs the slope of the linear regression line. |
oLRAngle |
Numeric |
Outputs the angle of the linear regression line in degrees. |
oLRIntercept |
Numeric |
Outputs the value at which the linear regression line crosses the current bar position. |
oLRValueRaw |
Numeric |
Outputs the regression value where the linear regression line crosses the TgtPos bar position. |
Remarks
Linear Regression is a concept also known as the "least squares method" or "best fit." Linear Regression attempts to fit a straight line between a range of bar values in such a way that distance between each data point and the line is minimized.
The equation of a regression line is:
y = mx + b
In the equation m refers to the slope of the regression line, b refers to the constant intercept of the y-axis, x is the independent variable, and y is the dependent variable.
The input Price can be hard coded with a bar attribute such as Close, Open, High, Low, and Volume or a numeric series type input. It can also be replaced with a valid EasyLanguage expression. For example: Close + Open, or Average(RSI(Close,14),14).
See Multiple Output Function for more information on using output parameters to return values.
Example
Assigns to Value2 the slope of a linear regression line of the Close, Value3 the angle, Value4 the current bar intercept value, and Value5 the regression value 5 bars into the future. Value1 is assigned a value of 1:
Vars: oLRSlope(0), oLRAngle(0), oLRIntercept(0), oLRValueRaw(0);
{… add EL statements here to assign High values to array elements... }
Value1 = LinearReg (Close, 20, -5, oLRSlope, oLRAngle, oLRIntercept, oLRValueRaw);
Value2 = oLRSlope;
Value3 = oLRAngle;
Value4 = oLRIntercept;
Value5 = oLRValueRaw;