• Get $40 off VIP by signing up for a free account! Sign Up

Linear Regression MTF for ThinkorSwim

british43

Member
VIP
I'm trying to create a linear regression MTF but have a problem with plotting the upper and lower lines. Any help would be greatly appreciate.

Code:
def fourHours = close(period = AggregationPeriod.FOUR_HOURS);
def day = close(period = AggregationPeriod.DAY);
def week = close(period = AggregationPeriod.WEEK);
def month = close(period = AggregationPeriod.MONTH);
def period=yes;

plot data=close;

input price = close;
input deviations = .8;

input length = 200; #set your channel lookback period here.
input opacity = 50;

def regression = InertiaAll(price, length);
def stdDeviation = StDevAll(price, length);

plot UpperLine = regression + deviations * stdDeviation;
plot LowerLine = regression - deviations * stdDeviation;
def dayreg=InertiaAll(price-day[1],length);
def daysd= StDevAll(price-day[1],length);

plot dayupper=dayreg+deviations*daysd;
 
Last edited by a moderator:
you can just add this study how many ever times you need it
the Week,Day,Hr,Min is configurable through the menu
remember your chart agreggation must be equal to or smaller than what ever timeframe you choose to configure the script for

Linear Regression MTF

Code:
#Linear Regression MTF by XeoNoX via usethinkscript.com
#the Week,Day,Hr,Min is configurable through the menu
#remember your chart agreggation must be equal to or smaller than whatever timeframe you choose to configure the script for
input TimeFrame = AggregationPeriod.DAY;
def price = (close(period =TimeFrame));
input deviations = .8;
input length = 200; #set your channel lookback period here.
input opacity = 50;
def regression = InertiaAll(price, length);
def stdDeviation = StDevAll(price, length);
plot UpperLine = regression + deviations * stdDeviation;
plot LowerLine = regression - deviations * stdDeviation;
def dayreg=InertiaAll(price-close(period =TimeFrame)[1],length);
def daysd= StDevAll(price-close(period =TimeFrame)[1],length);
plot dayupper=dayreg+deviations*daysd;
 
Last edited:

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

Hi, I am new here.

I am working on Thinkscript to display linear regression channel from a (5D1h) timeframe in a 5min chart.

I tried to to alter the aggreation period like below, however, the Linear Regression Channel's value plotted on 5min (in same interval of 5Days) chart is not the same as the 5D1h chart. Could anyone shed some light? Thanks!


input aggregationPeriod = AggregationPeriod.HOUR;
def price = close(period = aggregationPeriod);

plot MiddleLR = InertiaAll(price);
def dist = HighestAll(AbsValue(MiddleLR - price));
plot UpperLR = MiddleLR + dist;
plot LowerLR = MiddleLR - dist;

MiddleLR.setDefaultColor(GetColor(5));
UpperLR.setDefaultColor(GetColor(5));
LowerLR.setDefaultColor(GetColor(5));

plot MiddleLR2 = InertiaAll(price);
def dist2 = HighestAll(AbsValue(MiddleLR2 - price)) * 0.5;
plot UpperLR2 = MiddleLR2 + dist2;
plot LowerLR2 = MiddleLR2 - dist2;

MiddleLR2.setDefaultColor(GetColor(1));
UpperLR2.setDefaultColor(GetColor(1));
LowerLR2.setDefaultColor(GetColor(1));
 
Hi, I am new here.

I am working on Thinkscript to display linear regression channel from a (5D1h) timeframe in a 5min chart.

I tried to to alter the aggreation period like below, however, the Linear Regression Channel's value plotted on 5min (in same interval of 5Days) chart is not the same as the 5D1h chart. Could anyone shed some light? Thanks!


input aggregationPeriod = AggregationPeriod.HOUR;
def price = close(period = aggregationPeriod);

plot MiddleLR = InertiaAll(price);
def dist = HighestAll(AbsValue(MiddleLR - price));
plot UpperLR = MiddleLR + dist;
plot LowerLR = MiddleLR - dist;

MiddleLR.setDefaultColor(GetColor(5));
UpperLR.setDefaultColor(GetColor(5));
LowerLR.setDefaultColor(GetColor(5));

plot MiddleLR2 = InertiaAll(price);
def dist2 = HighestAll(AbsValue(MiddleLR2 - price)) * 0.5;
plot UpperLR2 = MiddleLR2 + dist2;
plot LowerLR2 = MiddleLR2 - dist2;

MiddleLR2.setDefaultColor(GetColor(1));
UpperLR2.setDefaultColor(GetColor(1));
LowerLR2.setDefaultColor(GetColor(1));


InertiaAll(price); and HighestAll have no length defined. Therefore, they default to using all the bars on your chart.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Statistical/InertiaAll

If you are attempting to "match" the MTF plot on your 5min chart to your plot on the hourly chart then your 5min chart requires 12 bars for every 1 bar on your hourly chart.
So if you have 40 candles on your hourly chart, you need to have exactly 480 candles on your 5min chart.

The workaround would be instead of using the default, provide the lengths to base your calculations on.

So you would change:
plot MiddleLR = InertiaAll(price);
def dist = HighestAll(AbsValue(MiddleLR - price));
To:
input length = however many bars are on your hourly chart *12 ;
plot MiddleLR = InertiaAll(price,length);
def dist = Highest(AbsValue(MiddleLR - price),length);
where the length for the 5min chart is set to 12x the length on whatever you set the length of the hourly chart. This would allow you to have apples and apples.

Keep in mind:
You will also have to deal with the fact that ToS no longer updates any of the ALL functions in real time. So they might not match until the hourly candle closes:
https://usethinkscript.com/threads/...e-no-update-in-real-time-in-thinkorswim.8794/
 
Last edited:
InertiaAll(price); and HighestAll have no length defined. Therefore, they default to using all the bars on your chart.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Statistical/InertiaAll

If you are attempting to "match" the MTF plot on your 5min chart to your plot on the hourly chart then your 5min chart requires 12 bars for every 1 bar on your hourly chart.
So if you have 40 candles on your hourly chart, you need to have exactly 480 candles on your 5min chart.

The workaround would be instead of using the default, provide the lengths to base your calculations on.

So you would change:

To:

where the length for the 5min chart is set to 12x the length on whatever you set the length of the hourly chart. This would allow you to have apples and apples.

Keep in mind:
You will also have to deal with the fact that ToS no longer updates any of the ALL functions in real time. So they might not match until the hourly candle closes:
https://usethinkscript.com/threads/...e-no-update-in-real-time-in-thinkorswim.8794/
Thanks for the detailed reply.

I tried to follow along your approach but it seems that the "HighestAll" method does not accept "length" as a parameter?

I got the following error: params expected but 2 found while calling highestall at 13:12
 
Thanks for the detailed reply.

I tried to follow along your approach but it seems that the "HighestAll" method does not accept "length" as a parameter?

I got the following error: params expected but 2 found while calling highestall at 13:12

Sorry!
I was typing too fast on my phone.
No, there is no HighestAll, it's just Highest

Here Ya go:

So you would change:
plot MiddleLR = InertiaAll(price);
def dist = HighestAll(AbsValue(MiddleLR - price));
To:
input length = however many bars are on your hourly chart *12 ;
plot MiddleLR = InertiaAll(price,length);
def dist = Highest(AbsValue(MiddleLR - price),length);
 
Hello would like to see about getting this into an mtf version thanks

Ruby:
#Linear regression by cslipilot

input StandardDeviationsOne = 1.0;
input StandardDeviationsTwo = 2.0;
input ShowHalfDeviations = Yes;
input ShowPriceBubbles = no;
input ShowFullRegressionRange = yes;
input RegressionLength = 21;

#Regression and Deviation Definitions

def C = close;

def Regression;
def StdDeviation;

if
(ShowFullRegressionRange)

{Regression = InertiaAll(C);
StdDeviation = StDevAll(C);}
else
{Regression = InertiaAll(C, RegressionLength);
StdDeviation = StDevAll(C, RegressionLength);}

#Regression and Deviation Plots

plot UpperLine = Regression + StandardDeviationsTwo * StdDeviation;
plot UpperHalfLine = if ShowHalfDeviations then Regression + StandardDeviationsTwo * .75 * StdDeviation else double.NaN;
plot UpperMidLine = Regression + StandardDeviationsOne * StdDeviation;
plot UpperHalfMidLine = if ShowHalfDeviations then Regression + StandardDeviationsOne * .5 * StdDeviation else double.NaN;
plot MiddleLine = Regression;
plot LowerHalfMidLine = if ShowHalfDeviations then Regression - StandardDeviationsOne * .5 * StdDeviation else double.NaN;
plot LowerMidLine = Regression - StandardDeviationsOne * StdDeviation;
plot LowerHalfLine = if ShowHalfDeviations then Regression - StandardDeviationsTwo * .75 * StdDeviation else double.NaN;
plot LowerLine = Regression - StandardDeviationsTwo * StdDeviation;

#Dynamic Color Line Code

UpperLine.AssignValueColor(if MiddleLine >= MiddleLine[1] then Color.GREEN else Color.RED);

UpperHalfLine.SetDefaultColor (Color.LIGHT_GRAY);
UpperHalfLine.SetStyle (Curve.SHORT_DASH);

UpperMidLine.SetDefaultColor (Color.LIGHT_ORANGE);

UpperHalfMidLine.SetDefaultColor (Color.LIGHT_GRAY);
UpperHalfMidLine.SetStyle (Curve.SHORT_DASH);

MiddleLine.SetDefaultColor (Color.LIGHT_GRAY);

LowerHalfMidLine.SetDefaultColor (Color.LIGHT_GRAY);
LowerHalfMidLine.SetStyle (Curve.SHORT_DASH);

LowerMidLine.SetDefaultColor (Color.LIGHT_ORANGE);

LowerHalfLine.SetDefaultColor (Color.LIGHT_GRAY);
LowerHalfLine.SetStyle (Curve.SHORT_DASH);

LowerLine.AssignValueColor(if MiddleLine < MiddleLine[1] then Color.RED else Color.GREEN);

#Price Bubble Code

AddChartBubble (!IsNaN(close) and IsNaN(close[-1])
and ShowPriceBubbles, UpperLine, "" + Round (UpperLine, 2),
(if MiddleLine >= MiddleLine[1] then Color.GREEN else Color.RED), yes);

AddChartBubble (!IsNaN(close) and IsNaN(close[-1])
and ShowPriceBubbles, UpperMidLine, "" + Round (UpperMidLine, 2),
Color.LIGHT_ORANGE, yes);

AddChartBubble (!IsNaN(close) and IsNaN(close[-1])
and ShowPriceBubbles, MiddleLine, "" + Round (MiddleLine, 2),
Color.LIGHT_GRAY, yes);

AddChartBubble (!IsNaN(close) and IsNaN(close[-1])
and ShowPriceBubbles, LowerMidLine, "" + Round (LowerMidLine, 2),
Color.LIGHT_ORANGE, yes);

AddChartBubble (!IsNaN(close) and IsNaN(close()[-1])
and ShowPriceBubbles, LowerLine, "" + Round (LowerLine, 2),
(if MiddleLine >= MiddleLine[1] then Color.GREEN else Color.RED), yes);
 
Last edited by a moderator:
Hello would like to see about getting this into an mtf version thanks

Ruby:
#Linear regression by cslipilot

input StandardDeviationsOne = 1.0;
input StandardDeviationsTwo = 2.0;
input ShowHalfDeviations = Yes;
input ShowPriceBubbles = no;
input ShowFullRegressionRange = yes;
input RegressionLength = 21;

#Regression and Deviation Definitions

def C = close;

def Regression;
def StdDeviation;

if
(ShowFullRegressionRange)

{Regression = InertiaAll(C);
StdDeviation = StDevAll(C);}
else
{Regression = InertiaAll(C, RegressionLength);
StdDeviation = StDevAll(C, RegressionLength);}

#Regression and Deviation Plots

plot UpperLine = Regression + StandardDeviationsTwo * StdDeviation;
plot UpperHalfLine = if ShowHalfDeviations then Regression + StandardDeviationsTwo * .75 * StdDeviation else double.NaN;
plot UpperMidLine = Regression + StandardDeviationsOne * StdDeviation;
plot UpperHalfMidLine = if ShowHalfDeviations then Regression + StandardDeviationsOne * .5 * StdDeviation else double.NaN;
plot MiddleLine = Regression;
plot LowerHalfMidLine = if ShowHalfDeviations then Regression - StandardDeviationsOne * .5 * StdDeviation else double.NaN;
plot LowerMidLine = Regression - StandardDeviationsOne * StdDeviation;
plot LowerHalfLine = if ShowHalfDeviations then Regression - StandardDeviationsTwo * .75 * StdDeviation else double.NaN;
plot LowerLine = Regression - StandardDeviationsTwo * StdDeviation;

#Dynamic Color Line Code

UpperLine.AssignValueColor(if MiddleLine >= MiddleLine[1] then Color.GREEN else Color.RED);

UpperHalfLine.SetDefaultColor (Color.LIGHT_GRAY);
UpperHalfLine.SetStyle (Curve.SHORT_DASH);

UpperMidLine.SetDefaultColor (Color.LIGHT_ORANGE);

UpperHalfMidLine.SetDefaultColor (Color.LIGHT_GRAY);
UpperHalfMidLine.SetStyle (Curve.SHORT_DASH);

MiddleLine.SetDefaultColor (Color.LIGHT_GRAY);

LowerHalfMidLine.SetDefaultColor (Color.LIGHT_GRAY);
LowerHalfMidLine.SetStyle (Curve.SHORT_DASH);

LowerMidLine.SetDefaultColor (Color.LIGHT_ORANGE);

LowerHalfLine.SetDefaultColor (Color.LIGHT_GRAY);
LowerHalfLine.SetStyle (Curve.SHORT_DASH);

LowerLine.AssignValueColor(if MiddleLine < MiddleLine[1] then Color.RED else Color.GREEN);

#Price Bubble Code

AddChartBubble (!IsNaN(close) and IsNaN(close[-1])
and ShowPriceBubbles, UpperLine, "" + Round (UpperLine, 2),
(if MiddleLine >= MiddleLine[1] then Color.GREEN else Color.RED), yes);

AddChartBubble (!IsNaN(close) and IsNaN(close[-1])
and ShowPriceBubbles, UpperMidLine, "" + Round (UpperMidLine, 2),
Color.LIGHT_ORANGE, yes);

AddChartBubble (!IsNaN(close) and IsNaN(close[-1])
and ShowPriceBubbles, MiddleLine, "" + Round (MiddleLine, 2),
Color.LIGHT_GRAY, yes);

AddChartBubble (!IsNaN(close) and IsNaN(close[-1])
and ShowPriceBubbles, LowerMidLine, "" + Round (LowerMidLine, 2),
Color.LIGHT_ORANGE, yes);

AddChartBubble (!IsNaN(close) and IsNaN(close()[-1])
and ShowPriceBubbles, LowerLine, "" + Round (LowerLine, 2),
(if MiddleLine >= MiddleLine[1] then Color.GREEN else Color.RED), yes);

why?
this uses InertiaAll( ) and StDevAll( ) , so it looks at all the bars on a chart and the lines span the whole chart.
if you change the time frame , but have the same amount of bars, the lines will be the same.
a chart time of 5 min / 30days will have the same lines as 30min / 30days


maybe a different study would be a better choice, one that doesn't plot lines across whole chart?
https://usethinkscript.com/threads/linear-regression-dev-for-thinkorswim.15963/
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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