Author Message:
The Polynomial Regression Slope Indicator is a versatile and powerful tool for traders seeking to identify trends and potential entry or exit points in the market. By fitting a polynomial function to a dataset, this indicator can provide insights into the direction and strength of a trend over a specified time period. The indicator allows users to select the degree of the polynomial (1 for linear, 2 for quadratic, and 3 for cubic) to match their trading style and the characteristics of the market they are trading in.
While higher-degree polynomials can offer a better fit to the data, we recommend using a polynomial of degree less than 3 for the sake of stability and to avoid overfitting. Overfitting occurs when a model captures the noise in the data rather than the underlying trend, leading to false signals and decreased predictive power.
Slope is an important aspect of trading as it represents the rate of change of an asset's price over time. A positive slope indicates an upward trend, while a negative slope suggests a downward trend. By using the Polynomial Regression Slope Indicator, traders can better understand the current market dynamics, identify potential reversals or continuation patterns, and make informed decisions on when to enter or exit a position.
CODE:
CSS:
#// https://www.tradingview.com/v/0abB8v07/
#// © henryph24
#indicator(title='Polynomial Regression Slope', shorttitle='PRS'
# converted by Sam4Cok@Samer800 - 04/2023
declare lower;
#//CUSTOM
#RS(y, t, o, degree) =>
script RS1 {
input y = close;
input t = 60;
input o = 0;
def x; def b0; def b1; def c0; def c1; def a0; def a1; def lr; def LRS;
x = If(x[1] == 0, 1, x[1]) + 1;
b0 = Sum(x, t);
b1 = Sum(Power(x, 2), t);
c0 = Sum(y, t);
c1 = Sum(x * y, t);
a0 = (c0 * b1 - c1 * b0) / (t * b1 - Power(b0, 2));
a1 = (t * c1 - b0 * c0) / (t * b1 - Power(b0, 2));
lr = a0 + a1 * (x + o);
LRS = CompoundValue(1 , lr - (if IsNaN(lr[1]) then lr else lr[1]), 0);
plot out = LRS;
;
}
script RS2 {
input y = close;
input t = 60;
input o = 0;
def x = If(x[1] == 0, 1, x[1]) + 1;
def b0 = Sum(x, t);
def b1 = Sum(Power(x, 2), t);
def b2 = Sum(Power(x, 3), t);
def b3 = Sum(Power(x, 4), t);
def c0 = Sum(y, t);
def c1 = Sum(x * y, t);
def c2 = Sum(Power(x, 2) * y, t);
def a0 = ((b1 * b3 - Power(b2, 2)) * (c0 * b2 - c1 * b1) - (b0 * b2 - Power(b1, 2)) * (c1 * b3 - c2 * b2)) / ((b1 * b3 - Power(b2, 2)) * (t * b2 - b0 * b1) - (b0 * b2 - Power(b1, 2)) * (b0 * b3 - b1 * b2));
def a1 = (c0 * b2 - c1 * b1 - (t * b2 - b0 * b1) * a0) / (b0 * b2 - Power(b1, 2));
def a2 = (c0 - t * a0 - b0 * a1) / b1;
def qr = a0 + a1 * (x + o) + a2 * Power(x + o, 2);
def QRS = CompoundValue(1, qr - (if IsNaN(qr[1]) then qr else qr[1]), 0);
plot out = QRS;
}
script RS3 {
input y = close;
input t = 60;
input o = 0;
def x = 2;
def b0 = sum(x, t);
def b1 = sum(power(x, 2), t);
def b2 = sum(power(x, 3), t);
def b3 = sum(power(x, 4), t);
def b4 = sum(power(x, 5), t);
def b5 = sum(power(x, 6), t);
def c0 = sum(y, t);
def c1 = sum(x * y, t);
def c2 = sum(power(x, 2) * y, t);
def c3 = sum(power(x, 3) * y, t);
def det = b0 * (b2 * b4 * b5 - b3 * b4 * b4) + b1 * (b3 * b3 * b5 - b2 * b4 * b5) + b2 * (b1 * b4 * b4 - b2 * b3 * b4) - b0 * b0 * b5 * b5 - b1 * b1 * b4 * b4 - b2 * b2 * b3 * b3;
def a0 = (c0 * (b2 * b4 * b5 - b3 * b4 * b4) + c1 * (b3 * b3 * b5 - b2 * b4 * b5) + c2 * (b1 * b4 * b4 - b2 * b3 * b4) - c3 * (b0 * b4 * b4 - b1 * b3 * b4)) / det;
def a1 = (c1 * (b0 * b4 * b5 - b2 * b3 * b5) + c2 * (b0 * b3 * b4 - b1 * b4 * b5) + c3 * (b0 * b2 * b4 - b1 * b3 * b4) - c0 * (b1 * b4 * b5 - b2 * b3 * b5)) / det;
def a2 = (c2 * (b0 * b3 * b5 - b1 * b4 * b5) + c3 * (b0 * b1 * b4 - b0 * b2 * b3) + c0 * (b1 * b3 * b3 - b2 * b2 * b5) - c1 * (b0 * b2 * b4 - b1 * b3 * b4)) / det;
def a3 = (c3 * (b0 * b1 * b5 - b0 * b2 * b4) + c0 * (b1 * b2 * b3 - b1 * b1 * b5) + c1 * (b0 * b2 * b2 - b1 * b1 * b4) - c2 * (b0 * b1 * b3 - b0 * b0 * b5)) / det;
def cr = a0 + a1 * (x + o) + a2 * power(x + o, 2) + a3 * power(x + o, 3);
def CRS = cr - (if isNaN(cr[1]) then cr else cr[1]);
plot out = CRS;
}
#//TOGGLE
input ShowLinearRegression = yes; # "Show Linear Regression"
input ShowQuadraticRegression = yes; # "Show Quadratic Regression"
input ShowCubicRegression = yes; # "Show Cubic Regression"
input Source = close; # "Source"
input SampleSize = 60; # "Sample Size"
def na = Double.NaN;
#//INV
#--- Color
DefineGlobalColor("blue" , CreateColor(25, 0, 255));
DefineGlobalColor("red" , Color.DARK_ORANGE);
DefineGlobalColor("green" , CreateColor(0, 250, 8));
DefineGlobalColor("red1" , CreateColor(255, 0, 0));
DefineGlobalColor("blue1" , Color.CYAN);
DefineGlobalColor("mag" , Color.MAGENTA);
def lrs = RS1(Source, SampleSize, 0);
def qrs = RS2(Source, SampleSize, 0);
def crs = RS3(Source, SampleSize, 0);
#//PLOT
plot CubicSlope = if ShowCubicRegression then crs else na;
plot QuadraticSlope = if ShowQuadraticRegression then qrs else na;
plot LinearSlope = if ShowLinearRegression then lrs else na;
QuadraticSlope.SetLineWeight(3);
CubicSlope.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
QuadraticSlope.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
LinearSlope.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
CubicSlope.AssignValueColor(if crs>0 then GlobalColor("blue1") else GlobalColor("mag"));
QuadraticSlope.AssignValueColor(if qrs>0 then GlobalColor("green") else GlobalColor("red1"));
LinearSlope.AssignValueColor(if lrs > 0 then GlobalColor("blue") else GlobalColor("red"));
#--- End of Code