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:
Hello Experts - I am dropping two TTM_LRC into the charts - with longer LENGTH and one with SHORTER LENGTH... I want to create a scanner to see when the price are at the extreme areas. Scan this condition for stocks. See marked - Essentially price at top of each band. Help will be great thanks

AAAA.jpg


Code:
#
# TD Ameritrade IP Company, Inc. (c) 2010-2020
#
# Source code isn't available.

input price = CLOSE;
input length = 38;
input beginDate = 0;
input beginTime = 0;
input numDevDn1 = -1.0;
input numDevDn2 = -2.0;
input numDevUp1 = 1.0;
input numDevUp2 = 2.0;
input extLeft = No;
input extRight = Yes;

plot LR = Double.NaN;
plot UB1 = Double.NaN;
plot LB1 = Double.NaN;
plot UB2 = Double.NaN;
plot LB2 = Double.NaN;
 
Last edited by a moderator:
Hi @ssara, it's an interesting idea.

Can you provide the next level of detail? TTM_PRC is a protected study source code is not available within the TOS, can you provide an example of inputs you're using on both, and the condition you want to trigger a signal?
 
Just use regular LRC or Std Dev Channels or Std Error Channels. All the same plots if using the same inputs. Thought I put this on here a long time ago. But my memory may be bad. At any rate use any one of them, all the same.

Code:
# LRC Variable Scan
input price = hl2;
input n = 252;
input widthOfChannel = 69;

def MidLine = InertiaAll(price, n);
def dist = HighestAll(AbsValue(MidLine - price)) * (widthOfChannel / 100.0);

# Scan only allows one plot. Set plot to Upper or Lower and def to the other
def "Upper Band Cross"  = close crosses MidLine + dist;
plot "Lower Band Cross" = close crosses MidLine - dist;
 
Here ya go. Not sure what you wanted it to look like so it just looks like the Bollingers

Code:
#LinearRegwithSTDevBands
#mods by WTF_Dude

input displace = 0;
input length = 9;
input price = close;

plot LinReg = Inertia(price[-displace], length);
LinReg.SetDefaultColor(GetColor(1));

input SD_length = 14;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;

def sDev = StDev(data = LinReg[-displace], length = SD_length);

plot LowerBand = LinReg + Num_Dev_Dn * sDev;
plot UpperBand = LinReg + Num_Dev_up * sDev;

LowerBand.SetDefaultColor(GetColor(0));
#LowerBand.setstyle(Curve.Long_DASH);
LinReg.SetDefaultColor(GetColor(1));
#LinReg.setstyle(Curve.MEDIUM_DASH);
UpperBand.SetDefaultColor(GetColor(5));
#UpperBand.setstyle(Curve.Long_DASH);
. I set standard deviation to the regular 14 length that the TD indicator uses by default.
 
I am trying to put study based orders where I want to use linear reg Ch50 upper / lower lines for my order signals .

I want to use a 15 minute chart on 10 day time frame . Current default goes to only 5 day window on 15 minute chart and no option in the system to change that . Obviously time frame is important as linear Reg lines change based on time window selected .

Is there anyway I can do it using think script

Thank you
 
thanks for the reply but i am not looking to change time interval on charts. I know that. I want to manipulate Time interval not only Aggregation period in thinkscript code so i can program and execute orders based on studies which use specfic time intervals ( for example 10 days) on 15 minute aggregation period. Currently all studies defaults to 5 day time interval if i use 15 minute aggregation period. Hope it clarifies .
 
Currently all studies defaults to 5 day time interval if i use 15 minute aggregation period.

What does that mean? Are you saying that we can't increase the time interval if the Aggregation period is set to 15 min?

It would be helpful if you can also provide the script that you're working on.
 
So you know i am talking about creating a buy or sell order . So i want to buy a stock if it goes below the lower line of linear regression ch50 where aggregation period is 15 minutes and time interval is 10 days. time interval here is very important as linear regression lines completely change when a time interval change . so the tos platform only as a default use 5 day interval for 15 min aggregation period.

and there is no way i have found which can change that thats why i am asking here
 
You cannot attempt to use custom aggregation periods, we have to select from those offered in a given scenario... We are bound by the limitations set upon us by the platform itself...
 
Hey All. First time poster. Would appreciate some help on this.

I am trying to create a scan where results populate based on the Linear Regression channel Upper and Lower limits.

To be more specific:
  • Price close is near or crosses above the UpperLimit
  • Price close is near or crosses below the LowerLimit
So far I have come up with the following. Not sure if it is correct.
Any of the following condition:
Code:
Between(LinearRegCh100()."UpperLR",close, close * 1.05)
Code:
Between(LinearRegCh100()."LowerLR", close * .95, close )

Any corrections or improvements would be appreciated.

Some additional thoughts would be that I would like to scan for multiple bounces off the limit lines which might be an entry/exit signal.

Thanks
 
might want to add slope of the linear regression line. I used to use Stockfetcher for scans and had one that followed stocks that had a positive slope and the price was at the bottom of the channel and vice versa. Thus buying pullbacks in an up move and selling rips in a downturn
 
because linear regression changes due to chart time frame, you must manually specify the bars back you want to be scanned for and your chart will have to match. There has been this topic of scanning against linear regression on this forum already, Please use the search feature.
@BenTen can move this topic to existing thread
 
Last edited:
here are some examples however:


Code:
#LinearRegChVar scan
# Mobius©: GldMtn  this is a scan I have in a dynamic watch list with my favorite companies to trade.
#It works very well as a signal when a #company is moving up through resistance in a trough or lower area. For a long trade.
Plot scan = low crosses LinearRegChVar ("width of channel" = 69, "full range" = no, "length" = 252)."LowerLR"

Code:
#Mob channel scan
# Mobius©: Ralf - here is a channel scan
# LRC Variable Scan
input price = hl2;
input n = 252;
input widthOfChannel = 69;
def MidLine = InertiaAll(price, n);
def dist = HighestAll(AbsValue(MidLine - price)) * (widthOfChannel / 100.0);
# Scan only allows one plot. Set plot to Upper or Lower and def to the other
def "Upper Band Cross"  = close crosses MidLine + dist;
plot "Lower Band Cross" = close crosses MidLine - dist;


Code:
#Mr. Script:
#scan for declining LR
def input price = close;
def MiddleLR = Inertia(price,20);
plot scan = isDescending(MiddleLR);
 
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);
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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