Repaints Polynomial Regression Slope [QTL] for ThinkOrSwim

Repaints

samer800

Moderator - Expert
VIP
Lifetime
V1THcDl.png

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
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Yes, agreed. The coding is way way above this little walnut head, but I grasp that it is useful, and somehow reminds me of converting anything to a scaled order of change like multiplying both Higher Highs vs Lower Lows in the useful HHSLLS indicator on TOS which I find useful, when observant to what is really going on.
Best wishes, Again @samer800, especially thankfor. You are a Bodhisattva of the first order! and I am grateful. I wish I could learn Thinkscript . Feel so isolated and such a slow process. Any suggestions as to where I can be linked to to see simple examples of executed critical codes, so my mind can begin to fill the emptyness. I am motivated . Best to All
 
I use a modified version of this polynomial.
Shown as the #4 indicator in this image: https://usethinkscript.com/threads/...ghts-rumors-sightings.9326/page-9#post-139307)

Yes, it repaints.
Think of it like getting a weather report. Knowing there's even a slight chance of rain allows me to break out the trusty umbrella.

In trading, it allows me to prepare for whatever possibility is indicated. Such as getting in early on a potential run of momentum, if my other indicators and market indices correlate. Or perhaps be tighter with a stop-loss, if bumpy weather is predicted.

Yes, the report may very well change, and it will end up being a sunny day, no need for the umbrella or that stop-loss, but I am a "like to be prepared sort-of-guy".
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
377 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top