Linear Regression Fun -- Excursion from the Centerline For ThinkOrSwim

mashume

Expert
VIP
Lifetime

LINEAR REGRESSION CENTERLINE EXCURSION

A Very Fast Directional Indicator
[link]http://tos.mx/567RIwO[/link]

DESCRIPTION
This indicator works as a moving average crossover, plotting the difference between the linear regression curve of the CLOSE price and the curve of the HL2 (HIGH + LOW / 2) curve of the same length.

While the indicator can be very good at picking changes in direction, it -- like all moving average crossovers -- can whipsaw. There is a smoothing option added which will apply the smoothing of your choice to the indicator signal. If you are using it alone to enter or exit, adjusting the smoothing may help avoid whipsaws. If you are using it as confirmation, it is faster without smoothing. I use lengths between 20 and 30 for the linear regression normally and find them very fast and also fairly reliable.

USAGE
Enter when the indicator crosses above zero and exit when it goes below. There are some screen shots below somewhere.

Code:
####################################################
#
#  LINEAR REGRESSION CENTERLINE EXCURSION
#
#  A very fast directional indicator
#  with optional smoothing
#
#  by mashume for the usethinkscript.com community
#
#  2022.03.28
#
#  Released the MIT License as open source
#  (c) mashume 2022
#
####################################################

declare lower;

input length = 20;
input smoothed = {default "no", "yes"};
input smoothing = AverageType.SIMPLE;
input smooth_length = 3;

def base;
def signal;

switch (smoothed) {
    CASE "no":
        base = Inertia(HL2, length);
        signal = Inertia(CLOSE, length);
    CASE "yes":
        base = MovingAverage(averageType = smoothing, length = smooth_length, data = Inertia(HL2, length));
        signal = MovingAverage(averageType = smoothing, length = smooth_length, data = Inertia(CLOSE, length));
}    

plot indicator = signal - base;

indicator.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
indicator.AssignValueColor( if indicator >= 0 then color.green else color.red);

plot zero = 0;

EYE CANDY AND DESCRIPTION

This is not investment advice.
npSPztw.png


Several new indicators are included in this screen shot. But I'm lazy and only did one write up. You can find the other threads somewhere on usethinkscript.com

A
The LINEAR REGRESSION CENTERLINE EXCURSION indicator shows a clear enter signal which lasts nicely through the bulk of the upward movement.

B
The PRICE ACTION within LINEAR REGRESSION CHANNEL indicator shows a squeeze and a bounce off the lower line which corresponds nicely with a move upward in the price chart.

C
The LINEAR REGRESSION CENTERLINE EXCURSION indicator shows a good short signal, though the signal does not last as long as the downward trend.

D
Fourier RMS shows an increase in short term energy as a proportion of the total energy (the blue line rises and the red falls).

E
Fourier RMS shows an dramatic decrease in the short term (blue) line and a distinct rise in the long term (red).
 

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

LINEAR REGRESSION CENTERLINE EXCURSION

A Very Fast Directional Indicator
[link]http://tos.mx/567RIwO[/link]

DESCRIPTION
This indicator works as a moving average crossover, plotting the difference between the linear regression curve of the CLOSE price and the curve of the HL2 (HIGH + LOW / 2) curve of the same length.

While the indicator can be very good at picking changes in direction, it -- like all moving average crossovers -- can whipsaw. There is a smoothing option added which will apply the smoothing of your choice to the indicator signal. If you are using it alone to enter or exit, adjusting the smoothing may help avoid whipsaws. If you are using it as confirmation, it is faster without smoothing. I use lengths between 20 and 30 for the linear regression normally and find them very fast and also fairly reliable.

USAGE
Enter when the indicator crosses above zero and exit when it goes below. There are some screen shots below somewhere.

Code:
####################################################
#
#  LINEAR REGRESSION CENTERLINE EXCURSION
#
#  A very fast directional indicator
#  with optional smoothing
#
#  by mashume for the usethinkscript.com community
#
#  2022.03.28
#
#  Released the MIT License as open source
#  (c) mashume 2022
#
####################################################

declare lower;

input length = 20;
input smoothed = {default "no", "yes"};
input smoothing = AverageType.SIMPLE;
input smooth_length = 3;

def base;
def signal;

switch (smoothed) {
    CASE "no":
        base = Inertia(HL2, length);
        signal = Inertia(CLOSE, length);
    CASE "yes":
        base = MovingAverage(averageType = smoothing, length = smooth_length, data = Inertia(HL2, length));
        signal = MovingAverage(averageType = smoothing, length = smooth_length, data = Inertia(CLOSE, length));
}  

plot indicator = signal - base;

indicator.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
indicator.AssignValueColor( if indicator >= 0 then color.green else color.red);

plot zero = 0;

EYE CANDY AND DESCRIPTION

This is not investment advice.
npSPztw.png


Several new indicators are included in this screen shot. But I'm lazy and only did one write up. You can find the other threads somewhere on usethinkscript.com

A
The LINEAR REGRESSION CENTERLINE EXCURSION indicator shows a clear enter signal which lasts nicely through the bulk of the upward movement.

B
The PRICE ACTION within LINEAR REGRESSION CHANNEL indicator shows a squeeze and a bounce off the lower line which corresponds nicely with a move upward in the price chart.

C
The LINEAR REGRESSION CENTERLINE EXCURSION indicator shows a good short signal, though the signal does not last as long as the downward trend.

D
Fourier RMS shows an increase in short term energy as a proportion of the total energy (the blue line rises and the red falls).

E
Fourier RMS shows an dramatic decrease in the short term (blue) line and a distinct rise in the long term (red).


Why not wash the Fourier through a LaPlace transform and see if you can add a predictive element to those waves?
 

LINEAR REGRESSION CENTERLINE EXCURSION

A Very Fast Directional Indicator
[link]http://tos.mx/567RIwO[/link]

DESCRIPTION
This indicator works as a moving average crossover, plotting the difference between the linear regression curve of the CLOSE price and the curve of the HL2 (HIGH + LOW / 2) curve of the same length.

While the indicator can be very good at picking changes in direction, it -- like all moving average crossovers -- can whipsaw. There is a smoothing option added which will apply the smoothing of your choice to the indicator signal. If you are using it alone to enter or exit, adjusting the smoothing may help avoid whipsaws. If you are using it as confirmation, it is faster without smoothing. I use lengths between 20 and 30 for the linear regression normally and find them very fast and also fairly reliable.

USAGE
Enter when the indicator crosses above zero and exit when it goes below. There are some screen shots below somewhere.

Code:
####################################################
#
#  LINEAR REGRESSION CENTERLINE EXCURSION
#
#  A very fast directional indicator
#  with optional smoothing
#
#  by mashume for the usethinkscript.com community
#
#  2022.03.28
#
#  Released the MIT License as open source
#  (c) mashume 2022
#
####################################################

declare lower;

input length = 20;
input smoothed = {default "no", "yes"};
input smoothing = AverageType.SIMPLE;
input smooth_length = 3;

def base;
def signal;

switch (smoothed) {
    CASE "no":
        base = Inertia(HL2, length);
        signal = Inertia(CLOSE, length);
    CASE "yes":
        base = MovingAverage(averageType = smoothing, length = smooth_length, data = Inertia(HL2, length));
        signal = MovingAverage(averageType = smoothing, length = smooth_length, data = Inertia(CLOSE, length));
}  

plot indicator = signal - base;

indicator.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
indicator.AssignValueColor( if indicator >= 0 then color.green else color.red);

plot zero = 0;

EYE CANDY AND DESCRIPTION

This is not investment advice.
npSPztw.png


Several new indicators are included in this screen shot. But I'm lazy and only did one write up. You can find the other threads somewhere on usethinkscript.com

A
The LINEAR REGRESSION CENTERLINE EXCURSION indicator shows a clear enter signal which lasts nicely through the bulk of the upward movement.

B
The PRICE ACTION within LINEAR REGRESSION CHANNEL indicator shows a squeeze and a bounce off the lower line which corresponds nicely with a move upward in the price chart.

C
The LINEAR REGRESSION CENTERLINE EXCURSION indicator shows a good short signal, though the signal does not last as long as the downward trend.

D
Fourier RMS shows an increase in short term energy as a proportion of the total energy (the blue line rises and the red falls).

E
Fourier RMS shows an dramatic decrease in the short term (blue) line and a distinct rise in the long term (red).
hello @mashume. thank you for the indicator, a quick question, do you mine share the firts lower indicator in the picture about? linear regression lower, the one with small arroy signal. thank you in advance
 
Last edited by a moderator:
hello @mashume. thank you for the indicator, a quick question, do you mine share the firts lower indicator in the picture about? linear regression lower, the one with small arroy signal. thank you in advance
I thought this was around here somewhere, but here it is again in any event...

Code:
####################################################
#
#  PRICE MOVEMENT WITHIN LINEAR REGRESSION CHANNEL
#
#  Price Action Relative to Linear Regression
#
#  by mashume for the usethinkscript.com community
#
#  2022.03.28
#
#  Released the MIT License as open source
#  (c) mashume 2022
#
####################################################

declare lower;

input price = close;
input widthOfChannel = 90.0;
input fullRange = No;
input length = 50;
input colour = yes;
input labels = yes;

def MiddleLR;
if (fullRange)
then {
    MiddleLR = InertiaAll(price);
} else {
    MiddleLR = Inertia(price, length);
}

def dist = Highest((AbsValue(MiddleLR - price)) * (widthOfChannel / 100.0), length);

plot UpperLR = dist;
plot LowerLR = - dist;

UpperLR.SetDefaultColor(GetColor(3));
LowerLR.SetDefaultColor(GetColor(3));


plot zero = 0;
zero.SetDefaultColor(GetColor(3));

def h = high - MiddleLR;
def l = low - MiddleLR;
def o = open - MiddleLR;
def c = close - MiddleLR;

AddChart(open = o, high = h, low = l, close = c, growColor = GetColor(7), fallColor = GetColor(7), neutralColor = GetColor(7), type = ChartType.BAR);

plot LRS = 100 * ( wma(price, length) -  Average(price, length) ) / (length - 1);

LRS.SetStyle(Curve.LONG_DASH);
LRS.SetDefaultColor(GetColor(12));

plot enter = if l crosses above LowerLR
then l else Double.NaN;

Enter.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

plot exit = if h crosses below UpperLR 
then h else Double.NaN;
Exit.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
 
@mashume Interesting indicator I noticed that when combined with the TMO you can increase the accuracy of your entries and exits when the TMO is in overbought or oversold areas. I was wondering if a candle painting strategy can be added within the code? It would easier to read the indicator that way. Thanks for sharing this with us btw!
 
@mashume Interesting indicator I noticed that when combined with the TMO you can increase the accuracy of your entries and exits when the TMO is in overbought or oversold areas. I was wondering if a candle painting strategy can be added within the code? It would easier to read the indicator that way. Thanks for sharing this with us btw!
I am looking for either a variable length Regression code or Anchored regression. I have a specific 'rec' variable that tracks across the screen. Once the lowestall(var) where the var is the data I'm measuring I want the trendline to plot from that candle, specifically until it is replaced, but not by using inertiaAll(). I also tried by getting YYYYMMDD or barnumber() but was not able to work out the logic. I think I have to compute the slope from the point (ohlc4) of the candle that rec with 'var' starts, which will continue for days until the data retraces and replaces it.

Are you aware of any code snippets I could look at?
 

LINEAR REGRESSION CENTERLINE EXCURSION

A Very Fast Directional Indicator
[link]http://tos.mx/567RIwO[/link]

DESCRIPTION
This indicator works as a moving average crossover, plotting the difference between the linear regression curve of the CLOSE price and the curve of the HL2 (HIGH + LOW / 2) curve of the same length.

While the indicator can be very good at picking changes in direction, it -- like all moving average crossovers -- can whipsaw. There is a smoothing option added which will apply the smoothing of your choice to the indicator signal. If you are using it alone to enter or exit, adjusting the smoothing may help avoid whipsaws. If you are using it as confirmation, it is faster without smoothing. I use lengths between 20 and 30 for the linear regression normally and find them very fast and also fairly reliable.

USAGE
Enter when the indicator crosses above zero and exit when it goes below. There are some screen shots below somewhere.

Code:
####################################################
#
#  LINEAR REGRESSION CENTERLINE EXCURSION
#
#  A very fast directional indicator
#  with optional smoothing
#
#  by mashume for the usethinkscript.com community
#
#  2022.03.28
#
#  Released the MIT License as open source
#  (c) mashume 2022
#
####################################################

declare lower;

input length = 20;
input smoothed = {default "no", "yes"};
input smoothing = AverageType.SIMPLE;
input smooth_length = 3;

def base;
def signal;

switch (smoothed) {
    CASE "no":
        base = Inertia(HL2, length);
        signal = Inertia(CLOSE, length);
    CASE "yes":
        base = MovingAverage(averageType = smoothing, length = smooth_length, data = Inertia(HL2, length));
        signal = MovingAverage(averageType = smoothing, length = smooth_length, data = Inertia(CLOSE, length));
}   

plot indicator = signal - base;

indicator.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
indicator.AssignValueColor( if indicator >= 0 then color.green else color.red);

plot zero = 0;

EYE CANDY AND DESCRIPTION

This is not investment advice.
npSPztw.png


Several new indicators are included in this screen shot. But I'm lazy and only did one write up. You can find the other threads somewhere on usethinkscript.com

A
The LINEAR REGRESSION CENTERLINE EXCURSION indicator shows a clear enter signal which lasts nicely through the bulk of the upward movement.

B
The PRICE ACTION within LINEAR REGRESSION CHANNEL indicator shows a squeeze and a bounce off the lower line which corresponds nicely with a move upward in the price chart.

C
The LINEAR REGRESSION CENTERLINE EXCURSION indicator shows a good short signal, though the signal does not last as long as the downward trend.

D
Fourier RMS shows an increase in short term energy as a proportion of the total energy (the blue line rises and the red falls).

E
Fourier RMS shows an dramatic decrease in the short term (blue) line and a distinct rise in the long term (red).
I didnt find a script on to to create "the linear regression lower" or "linear regression channel" on Think Script nor did I find these listed under studies. Can you help? thanks
 
I am looking for either a variable length Regression code or Anchored regression. I have a specific 'rec' variable that tracks across the screen. Once the lowestall(var) where the var is the data I'm measuring I want the trendline to plot from that candle, specifically until it is replaced, but not by using inertiaAll(). I also tried by getting YYYYMMDD or barnumber() but was not able to work out the logic. I think I have to compute the slope from the point (ohlc4) of the candle that rec with 'var' starts, which will continue for days until the data retraces and replaces it.

Are you aware of any code snippets I could look at?
ThinkScript doesn't like (read: I've never found a way to) variable lengths for indicators. I don't know if you could use NaNs for values that shouldn't be included... I don't have a full understanding of how various indicators deal with nans.

Sorry I can't be more help on this one.

-mashume
 
I didnt find a script on to to create "the linear regression lower" or "linear regression channel" on Think Script nor did I find these listed under studies. Can you help? thanks
You imported the scripts from the link above, or copied and pasted the indicator code into the code editor window? I think the links and code work. Did last time I checked on them at least.

-mashume
 

LINEAR REGRESSION CENTERLINE EXCURSION

A Very Fast Directional Indicator
[link]http://tos.mx/567RIwO[/link]

DESCRIPTION
This indicator works as a moving average crossover, plotting the difference between the linear regression curve of the CLOSE price and the curve of the HL2 (HIGH + LOW / 2) curve of the same length.

While the indicator can be very good at picking changes in direction, it -- like all moving average crossovers -- can whipsaw. There is a smoothing option added which will apply the smoothing of your choice to the indicator signal. If you are using it alone to enter or exit, adjusting the smoothing may help avoid whipsaws. If you are using it as confirmation, it is faster without smoothing. I use lengths between 20 and 30 for the linear regression normally and find them very fast and also fairly reliable.

USAGE
Enter when the indicator crosses above zero and exit when it goes below. There are some screen shots below somewhere.

Code:
####################################################
#
#  LINEAR REGRESSION CENTERLINE EXCURSION
#
#  A very fast directional indicator
#  with optional smoothing
#
#  by mashume for the usethinkscript.com community
#
#  2022.03.28
#
#  Released the MIT License as open source
#  (c) mashume 2022
#
####################################################

declare lower;

input length = 20;
input smoothed = {default "no", "yes"};
input smoothing = AverageType.SIMPLE;
input smooth_length = 3;

def base;
def signal;

switch (smoothed) {
    CASE "no":
        base = Inertia(HL2, length);
        signal = Inertia(CLOSE, length);
    CASE "yes":
        base = MovingAverage(averageType = smoothing, length = smooth_length, data = Inertia(HL2, length));
        signal = MovingAverage(averageType = smoothing, length = smooth_length, data = Inertia(CLOSE, length));
}   

plot indicator = signal - base;

indicator.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
indicator.AssignValueColor( if indicator >= 0 then color.green else color.red);

plot zero = 0;

EYE CANDY AND DESCRIPTION

This is not investment advice.
npSPztw.png


Several new indicators are included in this screen shot. But I'm lazy and only did one write up. You can find the other threads somewhere on usethinkscript.com

A
The LINEAR REGRESSION CENTERLINE EXCURSION indicator shows a clear enter signal which lasts nicely through the bulk of the upward movement.

B
The PRICE ACTION within LINEAR REGRESSION CHANNEL indicator shows a squeeze and a bounce off the lower line which corresponds nicely with a move upward in the price chart.

C
The LINEAR REGRESSION CENTERLINE EXCURSION indicator shows a good short signal, though the signal does not last as long as the downward trend.

D
Fourier RMS shows an increase in short term energy as a proportion of the total energy (the blue line rises and the red falls).

E
Fourier RMS shows an dramatic decrease in the short term (blue) line and a distinct rise in the long term (red).
Thank you for this explnation. if it possibele share upper study and last lower study too.
 

LINEAR REGRESSION CENTERLINE EXCURSION

A Very Fast Directional Indicator
[link]http://tos.mx/567RIwO[/link]

DESCRIPTION
This indicator works as a moving average crossover, plotting the difference between the linear regression curve of the CLOSE price and the curve of the HL2 (HIGH + LOW / 2) curve of the same length.

While the indicator can be very good at picking changes in direction, it -- like all moving average crossovers -- can whipsaw. There is a smoothing option added which will apply the smoothing of your choice to the indicator signal. If you are using it alone to enter or exit, adjusting the smoothing may help avoid whipsaws. If you are using it as confirmation, it is faster without smoothing. I use lengths between 20 and 30 for the linear regression normally and find them very fast and also fairly reliable.

USAGE
Enter when the indicator crosses above zero and exit when it goes below. There are some screen shots below somewhere.

Code:
####################################################
#
#  LINEAR REGRESSION CENTERLINE EXCURSION
#
#  A very fast directional indicator
#  with optional smoothing
#
#  by mashume for the usethinkscript.com community
#
#  2022.03.28
#
#  Released the MIT License as open source
#  (c) mashume 2022
#
####################################################

declare lower;

input length = 20;
input smoothed = {default "no", "yes"};
input smoothing = AverageType.SIMPLE;
input smooth_length = 3;

def base;
def signal;

switch (smoothed) {
    CASE "no":
        base = Inertia(HL2, length);
        signal = Inertia(CLOSE, length);
    CASE "yes":
        base = MovingAverage(averageType = smoothing, length = smooth_length, data = Inertia(HL2, length));
        signal = MovingAverage(averageType = smoothing, length = smooth_length, data = Inertia(CLOSE, length));
}   

plot indicator = signal - base;

indicator.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
indicator.AssignValueColor( if indicator >= 0 then color.green else color.red);

plot zero = 0;

EYE CANDY AND DESCRIPTION

This is not investment advice.
npSPztw.png


Several new indicators are included in this screen shot. But I'm lazy and only did one write up. You can find the other threads somewhere on usethinkscript.com

A
The LINEAR REGRESSION CENTERLINE EXCURSION indicator shows a clear enter signal which lasts nicely through the bulk of the upward movement.

B
The PRICE ACTION within LINEAR REGRESSION CHANNEL indicator shows a squeeze and a bounce off the lower line which corresponds nicely with a move upward in the price chart.

C
The LINEAR REGRESSION CENTERLINE EXCURSION indicator shows a good short signal, though the signal does not last as long as the downward trend.

D
Fourier RMS shows an increase in short term energy as a proportion of the total energy (the blue line rises and the red falls).

E
Fourier RMS shows an dramatic decrease in the short term (blue) line and a distinct rise in the long term (red).
Can you share a link for your chart setup?
 
Thank you for this explnation. if it possibele share upper study and last lower study too.
@khsato and @TradingNumbers
I cannot share the chart as it has some proprietary indicators on it.
here is a link to the bottom study:
https://usethinkscript.com/threads/fourier-analysis-decomposition-rms-energy-for-thinkorswim.10728/

I haven't posted up the upper yet... though I may try to get it typed up soon. I don't really like to do code dumps for things I've developed. I tend to do long winded explanations so that traders understand a bit of how the thing works internally.

-mashume
 
@khsato and @TradingNumbers
I cannot share the chart as it has some proprietary indicators on it.
here is a link to the bottom study:
https://usethinkscript.com/threads/fourier-analysis-decomposition-rms-energy-for-thinkorswim.10728/

I haven't posted up the upper yet... though I may try to get it typed up soon. I don't really like to do code dumps for things I've developed. I tend to do long winded explanations so that traders understand a bit of how the thing works internally. Smarter traders make better trades.

-mashume
Thanks @mashume. We all appreciate the attention to detail you have.

In the lower indicator, you have an option for "full range" which selects between the "inertia with length" and "inertiaAll". If full range is selected, does this mean the indicator will repaint?
 
Thanks @mashume. We all appreciate the attention to detail you have.

In the lower indicator, you have an option for "full range" which selects between the "inertia with length" and "inertiaAll". If full range is selected, does this mean the indicator will repaint?
No idea, honestly. If you find that it does, please do let us know.
-mashume
 
Thanks @mashume. We all appreciate the attention to detail you have.

In the lower indicator, you have an option for "full range" which selects between the "inertia with length" and "inertiaAll". If full range is selected, does this mean the indicator will repaint?
The difference is Schwab has decided that "inertiaAll" is too resource-intensive, so it will no longer update real-time.
They applied this change to all the "range all" functions.
Read more:
https://usethinkscript.com/threads/...e-no-update-in-real-time-in-thinkorswim.8794/
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
323 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