Library "LinearRegression"
Calculates a variety of linear regression and deviation types, with optional emphasis weighting. Additionally, multiple of slope and Pearson’s R calculations.
calcSlope(_src, _len, _condition)
Calculates the slope of a linear regression over the specified length.
Parameters:
_src (float): (float) The source data.
_len (int): (int) The length of the lookback period for the linear regression.
_condition (bool): (bool) Flag to enable calculation. Set to true to calculate on every bar; otherwise, set to barstate.islast for efficiency.
Returns: (float) The slope of the linear regression.
calcReg(_src, _len, _condition)
Calculates a basic linear regression, returning y1, y2, slope, and average.
Parameters:
_src (float): (float) The source data series.
_len (int): (int) The length of the lookback period.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: (float[]) An array of 4 values: [y1, y2, slope, average].
calcRegStandard(_src, _len, _emphasis, _condition)
Calculates an Standard linear regression with optional emphasis.
Parameters:
_src (float): (series float) The source data series.
_len (int): (int) The length of the lookback period.
_emphasis (float): (float) The emphasis factor: 0 for equal weight; >0 emphasizes recent bars; <0 emphasizes older bars.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: (float[]) [y1, y2, slope, weighted_average].
calcRegRidge(_src, _len, lambda, _emphasis, _condition)
Calculates a ridge regression with optional emphasis.
Parameters:
_src (float): (float) The source data series.
_len (int): (int) The length of the lookback period.
lambda (float): (float) The ridge regularization parameter.
_emphasis (float): (float) The emphasis factor: 0 for equal weight; >0 emphasizes recent bars; <0 emphasizes older bars.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: (float[]) [intercept, y2, slope, average].
calcRegLasso(_src, _len, lambda, _emphasis, _condition)
Calculates a Lasso regression with optional emphasis.
Parameters:
_src (float): (float) The source data series.
_len (int): (int) The length of the lookback period.
lambda (float): (float) The Lasso regularization parameter.
_emphasis (float): (float) The emphasis factor: 0 for equal weight; >0 emphasizes recent bars; <0 emphasizes older bars.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: (float[]) [intercept, (intercept + slope*(_len-1)), slope, average].
calcElasticNetLinReg(_src, _len, lambda1, lambda2, _emphasis, _condition)
Calculates an Elastic Net regression with optional emphasis.
Parameters:
_src (float): (float) The source data series.
_len (int): (int) The length of the lookback period.
lambda1 (float): (float) L1 regularization parameter (Lasso).
lambda2 (float): (float) L2 regularization parameter (Ridge).
_emphasis (float): (float) Emphasis factor: 0 for equal weight; >0 emphasizes recent bars; <0 emphasizes older bars.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: (float[]) [intercept, (intercept + beta*(_len-1)), beta, average].
calcRegHuber(_src, _len, delta, iterations, _emphasis, _condition)
Calculates a Huber regression using Iteratively Reweighted Least Squares (IRLS).
Parameters:
_src (float): (float) The source data series.
_len (int): (int) The length of the lookback period.
delta (float): (float) Huber threshold parameter.
iterations (int): (int) Number of IRLS iterations.
_emphasis (float): (float) Emphasis factor: 0 for equal weight; >0 emphasizes recent bars; <0 emphasizes older bars.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: (float[]) [intercept, (intercept + slope*(_len-1)), slope, average].
calcRegLAD(_src, _len, iterations, _emphasis, _condition)
Calculates a Least Absolute Deviations (LAD) regression via IRLS.
Parameters:
_src (float): (float) The source data series.
_len (int): (int) The length of the lookback period.
iterations (int): (int) Number of IRLS iterations for LAD.
_emphasis (float): (float) Emphasis factor: 0 for equal weight; >0 emphasizes recent bars; <0 emphasizes older bars.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: (float[]) [intercept, (intercept + slope*(_len-1)), slope, average].
calcRegBayesian(_src, _len, priorMean, priorSpan, sigma, _emphasis, _condition)
Calculates a Bayesian linear regression with optional emphasis.
Parameters:
_src (float): (float) The source data series.
_len (int): (int) The length of the lookback period.
priorMean (float): (float) The prior mean for the slope.
priorSpan (float): (float) The prior variance (or span) for the slope.
sigma (float): (float) The assumed standard deviation of residuals.
_emphasis (float): (float) Emphasis factor: 0 for equal weight; >0 emphasizes recent bars; <0 emphasizes older bars.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: (float[]) [intercept, (intercept + bayesSlope*(_len-1)), bayesSlope, average].
calcRFromLinReg(_src, _len, _slope, _average, _y1, _condition)
Calculates the Pearson correlation coefficient (R) based on linear regression parameters.
Parameters:
_src (float): (float) The source data.
_len (int): (int) The length of the lookback period.
_slope (float): (float) The slope of the linear regression.
_average (float): (float) The average value of the source data series.
_y1 (float): (float) The starting point (y-intercept of the oldest bar) for the linear regression.
_condition (bool): (bool) Flag to enable calculation. Set to true to calculate on every bar; otherwise, set to barstate.islast for efficiency.
Returns: (float) The Pearson correlation coefficient (R) adjusted for the direction of the slope.
calcRFromSource(_src, _len, _condition)
Calculates the correlation coefficient (R) using a specified length and source data.
Parameters:
_src (float): (float) The source data.
_len (int): (int) The length of the lookback period.
_condition (bool): (bool) Flag to enable calculation. Set to true to calculate on every bar; otherwise, set to barstate.islast for efficiency.
Returns: (float) The correlation coefficient (R).
calcSlopeLengthZero(_src, _len, _minLen, _step, _condition)
Identifies the length at which the slope is flattest (closest to zero).
Parameters:
_src (float): (float) The source data.
_len (int): (int) The maximum lookback length to consider (minimum of 2).
_minLen (int): (int) The minimum length to start from (cannot exceed the max length).
_step (int): (int) The increment step for lengths.
_condition (bool): (bool) Flag to enable calculation. Set to true to calculate on every bar; otherwise, set to barstate.islast.
Returns: (int) The length at which the slope is flattest.
calcSlopeLengthHighest(_src, _len, _minLen, _step, _condition)
Identifies the length at which the slope is highest.
Parameters:
_src (float): (float) The source data.
_len (int): (int) The maximum lookback length (minimum of 2).
_minLen (int): (int) The minimum length to start from.
_step (int): (int) The step for incrementing lengths.
_condition (bool): (bool) Flag to enable calculation. Set to true to calculate on every bar; otherwise, set to barstate.islast.
Returns: (int) The length at which the slope is highest.
calcSlopeLengthLowest(_src, _len, _minLen, _step, _condition)
Identifies the length at which the slope is lowest.
Parameters:
_src (float): (float) The source data.
_len (int): (int) The maximum lookback length (minimum of 2).
_minLen (int): (int) The minimum length to start from.
_step (int): (int) The step for incrementing lengths.
_condition (bool): (bool) Flag to enable calculation. Set to true to calculate on every bar; otherwise, set to barstate.islast.
Returns: (int) The length at which the slope is lowest.
calcSlopeLengthAbsolute(_src, _len, _minLen, _step, _condition)
Identifies the length at which the absolute slope value is highest.
Parameters:
_src (float): (float) The source data.
_len (int): (int) The maximum lookback length (minimum of 2).
_minLen (int): (int) The minimum length to start from.
_step (int): (int) The step for incrementing lengths.
_condition (bool): (bool) Flag to enable calculation. Set to true to calculate on every bar; otherwise, set to barstate.islast.
Returns: (int) The length at which the absolute slope value is highest.
calcRLengthZero(_src, _len, _minLen, _step, _condition)
Identifies the length with the lowest absolute R value.
Parameters:
_src (float): (float) The source data.
_len (int): (int) The maximum lookback length (minimum of 2).
_minLen (int): (int) The minimum length to start from.
_step (int): (int) The step for incrementing lengths.
_condition (bool): (bool) Flag to enable calculation. Set to true to calculate on every bar; otherwise, set to barstate.islast.
Returns: (int) The length with the lowest absolute R value.
calcRLengthHighest(_src, _len, _minLen, _step, _condition)
Identifies the length with the highest R value.
Parameters:
_src (float): (float) The source data.
_len (int): (int) The maximum lookback length (minimum of 2).
_minLen (int): (int) The minimum length to start from.
_step (int): (int) The step for incrementing lengths.
_condition (bool): (bool) Flag to enable calculation. Set to true to calculate on every bar; otherwise, set to barstate.islast.
Returns: (int) The length with the highest R value.
calcRLengthLowest(_src, _len, _minLen, _step, _condition)
Identifies the length with the lowest R value.
Parameters:
_src (float): (float) The source data.
_len (int): (int) The maximum lookback length (minimum of 2).
_minLen (int): (int) The minimum length to start from.
_step (int): (int) The step for incrementing lengths.
_condition (bool): (bool) Flag to enable calculation. Set to true to calculate on every bar; otherwise, set to barstate.islast.
Returns: (int) The length with the lowest R value.
calcRLengthAbsolute(_src, _len, _minLen, _step, _condition)
Identifies the length with the highest absolute R value.
Parameters:
_src (float): (float) The source data.
_len (int): (int) The maximum lookback length (minimum of 2).
_minLen (int): (int) The minimum length to start from.
_step (int): (int) The step for incrementing lengths.
_condition (bool): (bool) Flag to enable calculation. Set to true to calculate on every bar; otherwise, set to barstate.islast.
Returns: (int) The length with the highest absolute R value.
calcDevReverse(_src, _len, _slope, _y1, _inputDev, _emphasis, _condition)
Calculates the regressive linear deviation in reverse order, with optional emphasis on recent data.
Parameters:
_src (float): (float) The source data.
_len (int): (int) The length of the lookback period.
_slope (float): (float) The slope of the linear regression.
_y1 (float): (float) The y-intercept (oldest bar) of the linear regression.
_inputDev (float): (float) The input deviation multiplier.
_emphasis (float): (float) Emphasis factor: 0 for equal weight; >0 emphasizes recent bars; <0 emphasizes older bars.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: A 2-element tuple: [positive deviation, negative deviation].
calcDevForward(_src, _len, _slope, _y1, _inputDev, _emphasis, _condition)
Calculates the progressive linear deviation in forward order (oldest to most recent bar), with optional emphasis.
Parameters:
_src (float): (float) The source data array, where _src[0] is oldest and _src[_len - 1] is most recent.
_len (int): (int) The length of the lookback period.
_slope (float): (float) The slope of the linear regression.
_y1 (float): (float) The y-intercept of the linear regression (value at the most recent bar, adjusted by slope).
_inputDev (float): (float) The input deviation multiplier.
_emphasis (float): (float) Emphasis factor: 0 for equal weight; >0 emphasizes recent bars; <0 emphasizes older bars.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: A 2-element tuple: [positive deviation, negative deviation].
calcDevBalanced(_src, _len, _slope, _y1, _inputDev, _emphasis, _condition)
Calculates the balanced linear deviation with optional emphasis on recent or older data.
Parameters:
_src (float): (float) Source data array, where _src[0] is the most recent and _src[_len - 1] is the oldest.
_len (int): (int) The length of the lookback period.
_slope (float): (float) The slope of the linear regression.
_y1 (float): (float) The y-intercept of the linear regression (value at the oldest bar).
_inputDev (float): (float) The input deviation multiplier.
_emphasis (float): (float) Emphasis factor: 0 for equal weight; >0 emphasizes recent bars; <0 emphasizes older bars.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: A 2-element tuple: [positive deviation, negative deviation].
calcDevMean(_src, _len, _slope, _y1, _inputDev, _emphasis, _condition)
Calculates the mean absolute deviation from a forward-applied linear trend (oldest to most recent), with optional emphasis.
Parameters:
_src (float): (float) The source data array, where _src[0] is the most recent and _src[_len - 1] is the oldest.
_len (int): (int) The length of the lookback period.
_slope (float): (float) The slope of the linear regression.
_y1 (float): (float) The y-intercept (oldest bar) of the linear regression.
_inputDev (float): (float) The input deviation multiplier.
_emphasis (float): (float) Emphasis factor: 0 for equal weight; >0 emphasizes recent bars; <0 emphasizes older bars.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: A 2-element tuple: [positive deviation, negative deviation].
calcDevMedian(_src, _len, _slope, _y1, _inputDev, _emphasis, _condition)
Calculates the median absolute deviation with optional emphasis on recent data.
Parameters:
_src (float): (float) The source data array (index 0 = oldest, index _len - 1 = most recent).
_len (int): (int) The length of the lookback period.
_slope (float): (float) The slope of the linear regression.
_y1 (float): (float) The y-intercept (oldest bar) of the linear regression.
_inputDev (float): (float) The deviation multiplier.
_emphasis (float): (float) Emphasis factor: 0 for equal weight; >0 emphasizes recent bars; <0 emphasizes older bars.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: [positive deviation, negative deviation]
calcDevPercent(_y1, _inputDev, _condition)
Calculates the percent deviation from a given value and a specified percentage.
Parameters:
_y1 (float): (float) The base value from which to calculate deviation.
_inputDev (float): (float) The deviation percentage.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: A 2-element tuple: [positive deviation, negative deviation].
calcDevFitted(_len, _slope, _y1, _emphasis, _condition)
Calculates the weighted fitted deviation based on high and low series data, showing max deviation, with optional emphasis.
Parameters:
_len (int): (int) The length of the lookback period.
_slope (float): (float) The slope of the linear regression.
_y1 (float): (float) The Y-intercept (oldest bar) of the linear regression.
_emphasis (float): (float) Emphasis factor: 0 for equal weight; >0 emphasizes recent bars; <0 emphasizes older bars.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: A 2-element tuple: [weighted highest deviation, weighted lowest deviation].
calcDevATR(_src, _len, _slope, _y1, _inputDev, _emphasis, _condition)
Calculates an ATR-style deviation with optional emphasis on recent data.
Parameters:
_src (float): (float) The source data (typically close).
_len (int): (int) The length of the lookback period.
_slope (float): (float) The slope of the linear regression.
_y1 (float): (float) The Y-intercept (oldest bar) of the linear regression.
_inputDev (float): (float) The input deviation multiplier.
_emphasis (float): (float) Emphasis factor: 0 for equal weight; >0 emphasizes recent bars; <0 emphasizes older bars.
_condition (bool): (bool) Flag to enable calculation (true = calculate).
Returns: A 2-element tuple: [positive deviation, negative deviation].
calcPricePositionPercent(_top, _bot, _src)
Calculates the percent position of a price within a linear regression channel. Top=100%, Bottom=0%.
Parameters:
_top (float): (float) The top (positive) deviation, corresponding to 100%.
_bot (float): (float) The bottom (negative) deviation, corresponding to 0%.
_src (float): (float) The source price.
Returns: (float) The percent position within the channel.
plotLinReg(_len, _y1, _y2, _slope, _devTop, _devBot, _scaleTypeLog, _lineWidth, _extendLines, _channelStyle, _colorFill, _colUpLine, _colDnLine, _colUpFill, _colDnFill)
Plots the linear regression line and its deviations, with configurable styles and fill.
Parameters:
_len (int): (int) The lookback period for the linear regression.
_y1 (float): (float) The starting y-value of the regression line.
_y2 (float): (float) The ending y-value of the regression line.
_slope (float): (float) The slope of the regression line (used to determine line color).
_devTop (float): (float) The top deviation to add to the line.
_devBot (float): (float) The bottom deviation to subtract from the line.
_scaleTypeLog (bool): (bool) Use a log scale if true; otherwise, linear scale.
_lineWidth (int): (int) The width of the plotted lines.
_extendLines (string): (string) How lines should extend (none, left, right, both).
_channelStyle (string): (string) The style of the channel lines (solid, dashed, dotted).
_colorFill (bool): (bool) Whether to fill the space between the top and bottom deviation lines.
_colUpLine (color): (color) Line color when slope is positive.
_colDnLine (color): (color) Line color when slope is negative.
_colUpFill (color): (color) Fill color when slope is positive.
_colDnFill (color): (color) Fill color when slope is negative.