Linear Regression (LRC)

J007RMC

Well-known member
2019 Donor
Probably most of you all know the answer to my question but I need answers too. So, when using the linear regression indicator and it seems realistic to me is to turn the drawn lines off and use the averages alone to find targets? And Im just throwing this out there. My for instance is the model I am using shows optionable: Avg, Avg 2, Avg 3.
# Mobius
# Mobius at MyTrade
# V02.04.2014 :

input price = hl2;
input n = 500;
input StartDate = 0;
input StartTime = 00;
input ExtendToRight = yes;
input ExtendToLeft = no;
input nAvg200 = 200;
input nAvg50 = 50;
input nAvg21 = 21;
input widthOfChannel = 2.0;
input fib1 = 0.382;
input fib2 = 0.618;
input fib3 = 0.764;

def o = open;
def h = high;
def l = low;
def c = close;
def ATR = Average(TrueRange(h, c, l), 21);
plot Avg = Average(c, nAvg200);
Avg.SetDefaultColor(Color.WHITE);
plot Avg2 = Average(c, nAvg50);
Avg2.SetDefaultColor(Color.YELLOW);
plot Avg3 = Average(c, nAvg21);
Avg3.SetDefaultColor(Color.GRAY);
plot MidLine = InertiaAll(price, n, StartDate, StartTime, ExtendToLeft, ExtendToRight);
def dist = HighestAll(StDev(h + l) / 2) * widthOfChannel;

plot UpperBand = MidLine + dist;
plot LowerBand = MidLine - dist;
plot UpperFibLine1 = (UpperBand - MidLine) * fib1 + MidLine;
plot UpperFibLine2 = (UpperBand - MidLine) * fib2 + MidLine;
plot UpperFibLine3 = (UpperBand - MidLine) * fib3 + MidLine;
plot LowerFibLine1 = (LowerBand - MidLine) * fib1 + MidLine;
plot LowerFibLine2 = (LowerBand - MidLine) * fib2 + MidLine;
plot LowerFibLine3 = (LowerBand - MidLine) * fib3 + MidLine;

LowerBand.SetLineWeight(2);
MidLine.SetLineWeight(2);
UpperBand.SetLineWeight(2);
LowerBand.SetDefaultColor(Color.BLUE);
MidLine.SetDefaultColor(Color.GRAY);
UpperBand.SetDefaultColor(Color.BLUE);
UpperFibLine1.SetDefaultColor(Color.GRAY);
UpperFibLine2.SetDefaultColor(Color.GRAY);
UpperFibLine3.SetDefaultColor(Color.GRAY);
LowerFibLine1.SetDefaultColor(Color.GRAY);
LowerFibLine2.SetDefaultColor(Color.GRAY);
LowerFibLine3.SetDefaultColor(Color.GRAY);
UpperFibLine1.SetStyle(Curve.SHORT_DASH);
UpperFibLine2.SetStyle(Curve.SHORT_DASH);
UpperFibLine3.SetStyle(Curve.SHORT_DASH);
LowerFibLine1.SetStyle(Curve.SHORT_DASH);
LowerFibLine2.SetStyle(Curve.SHORT_DASH);
LowerFibLine3.SetStyle(Curve.SHORT_DASH);
#end
 
Last edited:
Could someone please tell me how to allow my TOS screen to continue going right (viewing more of the past)? I am practicing drawing trend lines and S/R, but I can only go back so far. Thanks in advance.
 

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

The code for the Linear Regression Chanel Var looks like this:
Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2021
#

input price = close;
input widthOfChannel = 100.0;
input fullRange = Yes;
input length = 21;

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

def dist = HighestAll(AbsValue(MiddleLR - price)) * (widthOfChannel / 100.0);

plot UpperLR = MiddleLR + dist;
plot LowerLR = MiddleLR - dist;

MiddleLR.SetDefaultColor(GetColor(5));
UpperLR.SetDefaultColor(GetColor(5));
LowerLR.SetDefaultColor(GetColor(5));
This will plot a linear regression channel over the last n bars (default is 21).

We can not adjust the fact that this indicator plots using the last n bars as the data for calculating the linear regression.

We can however make a new indicator that plots a linear regression curve at a point in the past:
Code:
plot MiddleLR2 = InertiaAll(price[length], length)[-length];
What we're doing is this:
price[length] specifies that we take the value n bars in the past,
[-length] tells ToS to plot it n bars back on the chart.

We can add as many of these as we'd like to the plot at whatever intervals we need
Code:
plot MiddleLR3 = InertiaAll(price[length * 2], length)[-length * 2];
plot MiddleLR4 = InertiaAll(price[length * 3], length)[-length * 3];
and so on as you see fit.

I have omitted the code for making the upper and lower bands for the sake of showing where the changes need to be made.

This does not dynamically adjust the channel, but it lets you plot historic channels on your chart.

This may or may not help, but gives you some idea of the power of offsets in data and plotting.

Happy Trading
-mashume
 
So I've been trying to get this to work for hours now and I'm absolutely stuck, so I'm hoping someone can help me here. I am trying to create a study as follows: 10 bar LR line. Once a new bar is added, the 10 LR line stays with its 10 bars and a new one is plotted with the current 10 bars (so in this case the same bars except adding the most recent and subtracting the now 11th bar). This repeats with as many bars as I want. I realize this was not a good explanation, so I'll illustrate what I want:

I drew those lines myself so they're definitely not 100% accurate LR lines but they're supposed to give a rough estimate of what it would look like. The key is that each time a new bar is added it plots a 10 LR line and keeps the previous LR lines. I got very close to getting this with this script:

input price = close;
input length = 10;
def regression = InertiaAll(price, length);
plot LR1 = regression;
plot LR2 = InertiaAll(close[1], length);
plot LR3 = InertiaAll(close[2], length);
plot LR4 = InertiaAll(close[3], length);
plot LR5 = InertiaAll(close[4], length);
plot LR6 = InertiaAll(close[5], length);
plot LR7 = InertiaAll(close[6], length);
plot LR8 = InertiaAll(close[7], length);
plot LR9 = InertiaAll(close[8], length);
plot LR10 = InertiaAll(close[9], length);

Here's what it looks like:

This was exactly what I wanted except for one crucial issue: it plots each line on the current 10 bars. What I'm looking for is a way for each line to be plotted on its respective 10 bars like the first picture. Can anyone give me a script or edit my current one? Thanks a ton!
 
So I've been trying to get this to work for hours now and I'm absolutely stuck, so I'm hoping someone can help me here. I am trying to create a study as follows: 10 bar LR line. Once a new bar is added, the 10 LR line stays with its 10 bars and a new one is plotted with the current 10 bars (so in this case the same bars except adding the most recent and subtracting the now 11th bar). This repeats with as many bars as I want. I realize this was not a good explanation, so I'll illustrate what I want:

I drew those lines myself so they're definitely not 100% accurate LR lines but they're supposed to give a rough estimate of what it would look like. The key is that each time a new bar is added it plots a 10 LR line and keeps the previous LR lines. I got very close to getting this with this script:

input price = close;
input length = 10;
def regression = InertiaAll(price, length);
plot LR1 = regression;
plot LR2 = InertiaAll(close[1], length);
plot LR3 = InertiaAll(close[2], length);
plot LR4 = InertiaAll(close[3], length);
plot LR5 = InertiaAll(close[4], length);
plot LR6 = InertiaAll(close[5], length);
plot LR7 = InertiaAll(close[6], length);
plot LR8 = InertiaAll(close[7], length);
plot LR9 = InertiaAll(close[8], length);
plot LR10 = InertiaAll(close[9], length);

Here's what it looks like:

This was exactly what I wanted except for one crucial issue: it plots each line on the current 10 bars. What I'm looking for is a way for each line to be plotted on its respective 10 bars like the first picture. Can anyone give me a script or edit my current one? Thanks a ton!
See if this works how you want
Capture.jpg
Ruby:
input price = close;
input length = 10;
def regression = InertiaAll(price, length);
plot LR1 = regression;
plot LR2 = Inertiaall(close[1], length)[-1];
plot LR3 = Inertiaall(close[2], length)[-2];
plot LR4 = Inertiaall(close[3], length)[-3];
plot LR5 = Inertiaall(close[4], length)[-4];
plot LR6 = Inertiaall(close[5], length)[-5];
plot LR7 = Inertiaall(close[6], length)[-6];
plot LR8 = Inertiaall(close[7], length)[-7];
plot LR9 = Inertiaall(close[8], length)[-8];
plot LR10 = Inertiaall(close[9], length)[-9];
 
Thanks for the help XeoNox. I have been messing around today in thinkScript trying to incorporate the existing scans and LR slope. This is what I have come up with.

Bullisth Entry Scanner
Code:
input price = close;
input sLenght = 14;

def LRS = 6 * ( WMA(price, sLenght) -  Average(price, sLenght) ) / (sLenght - 1);


def MiddleLR = InertiaAll(price);
def dist = HighestAll(AbsValue(MiddleLR - price));
def UpperLR = MiddleLR + dist;
def LowerLR = MiddleLR - dist;
def offset = Round((UpperLR - LowerLR)* .11, 3);
def LowerLR_offset = LowerLR + offset;
def risingLRS = LRS[1] <= LRS;
def lowZone = price <= LowerLR_offset within 1 bars;
plot bullEntry = lowZone and risingLRS;


Bearish Entry Scanner
Code:
input price = close;
input sLenght = 14;

def LRS = 6 * ( WMA(price, sLenght) -  Average(price, sLenght) ) / (sLenght - 1);


def MiddleLR = InertiaAll(price);
def dist = HighestAll(AbsValue(MiddleLR - price));
def UpperLR = MiddleLR + dist;
def LowerLR = MiddleLR - dist;
def offset = Round((UpperLR - LowerLR)* .11, 3);
def UpperLR_offset = UpperLR - offset;
def droppingLRS = LRS[1] >= LRS;
def upperZone = price >= UpperLR_offset within 5 bars;
plot bearEntry = upperZone and droppingLRS;

Essentially I am defining a 10% offset from the upper or lower line and then utilizing the LR slope to attempt to find candidates. LR slope is inspected to determine if the previous LRS is below/above current depending on scan.

I also created an indicator for this on the chart. I am sure its not perfect but this is just a start for me.
Code:
# Linear_Regression_Custom

input price = close;
input sLenght = 14;

def LRS = 6 * ( WMA(price, sLenght) -  Average(price, sLenght) ) / (sLenght - 1);


plot MiddleLR = InertiaAll(price);
def dist = HighestAll(AbsValue(MiddleLR - price));
plot UpperLR = MiddleLR + dist;
plot LowerLR = MiddleLR - dist;
def offset = Round((UpperLR - LowerLR)* .11, 3);
plot UpperLR_offset = UpperLR - offset;
plot LowerLR_offset = LowerLR + offset;

#Bullish stuff
def bullCrossLRS = LRS[1] <= LRS;
def lowRevZone = price <= LowerLR_offset within 1 bars;
plot bullEntry = lowRevZone and bullCrossLRS;
bullEntry.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
bullEntry.SetDefaultColor(Color.GREEN);
bullEntry.setLineWeight(3);

#Bearish Stuff
def bearCrossLRS = LRS[1] >= LRS;
def upperRevZone = price >= UpperLR_offset within 1 bars;
plot bearEntry = upperRevZone and bearCrossLRS;
bearEntry.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
bearEntry.SetDefaultColor(Color.RED);
bearEntry.setLineWeight(3);


#Default Look & feel
MiddleLR.SetDefaultColor(Color.YELLOW);
UpperLR.SetDefaultColor(Color.RED);
LowerLR.SetDefaultColor(Color.GREEN);
LowerLR_offset.SetDefaultColor(Color.GREEN);
LowerLR_offset.SetStyle(curve.SHORT_DASH);
UpperLR_offset.SetDefaultColor(Color.RED);
UpperLR_offset.SetStyle(curve.SHORT_DASH);
Nice job. Thanks for sharing.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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