TradingView
tbiktag
Jan 3, 2021 8:48 PM

Moving Regression 

WTI CRUDE OILTVC

Description

Moving Regression is a generalization of moving average and polynomial regression.

The procedure approximates a specified number of prior data points with a polynomial function of a user-defined degree. Then, polynomial interpolation of the last data point is used to construct a Moving Regression time series.

Application:
Moving Regression allows one to smooth noise on the analyzed chart, assess momentum, confirm trends, and establish areas of support and resistance.
In addition, it can be used as a simple stand-alone forecasting method to identify trend direction and trend​ reversal points. When the local polynomial is predicted to move up in the next time step, the color of the Moving Regression curve will be green. Otherwise, the color of the curve is red. This function is (de)activated using the Predict Trend Direction flag.

Selecting the ​model parameters:
The effects of the moving window Length and the Local Polynomial Degree are confounded. This allows for​ finding the optimal trade-off between noise (variance) and lag (bias). Higher Length and lower Polynomial Degree (such as 1, i.e. linear), will result in "smoother" time series but at the cost of greater lag. Increasing the Polynomial​ Degree to, for example, 2 (squared) while maintaining the Length will diminish the lag and thus compromise the noise-lag tradeoff.

Relation to other methods:
When the degree of the local polynomial is set to 0 (i.e., fitting data to a constant level), the Moving Regression time series exactly matches the Simple Moving Average of the same length.

Release Notes

minor corrections
Comments
PineCoders
tbiktag
@PineCoders, Many thanks!
allanster
Outstanding work, thanks for sharing!
tbiktag
@allanster, Thank you mate!
justinsandell
This looks like the Least Squares Moving Average
tbiktag
@Dealth, In my experience, the name "Least Squares Moving Average" is commonly associated with local linear regression. The current script is built on polynomial regression and therefore can be viewed as a generalization. But of course you can achieve the local linear regression by setting Polynomial Degree to 1.
Bull_Bear003
I’m speechless at the complexity of the work done. Amazing!

Is there really no other way to simplify the code since PineScript version 5 is out? I’m still (very much!) green in the coding world and I’m interested in modifying (read: simplify) the script and convert it to Version 5 if you could indicate me where to start.
HisNomad
Brilliant, thanks a lot!
OutsideCircle
Hello thank you for awesome scripts. Is there anyway to plot the stdev of the regression line? Thanks
MMC11
This looks splendid. However I don't get the blue line to show up nor can I find how to activate it in settings. Help!
Thanks
More