 # What is Inertia and InertiaAll from Linear Regression

#### flyer01

##### New member
Has anyone had luck with the InertiaAll function for a higher timeframe? For example, before entering a trade, I like to make sure the trend on both the one hour and four hour time frame are moving in the same direction (per InertiaAll). However, when using the script below on a one hour chart to show the straight line trend over four hours, it clearly isn't correct. Any ideas on how to use InertiaAll over multiple time frames?

Code:
``````declare lower;

plot inert = InertiaAll(data = close(period = AggregationPeriod.FOUR_HOURS), length = 100);``````

• Tidan

#### tomsk

##### Well-known member
VIP
@flyer01 I typically have used Inertiaall() on upper studies in the past. I took your code base and was able to plot it just fine on a 1 hour chart, using a 4 Hour secondary aggregation. Here's the code

Code:
``````input length = 100;
plot LR = InertiaAll(close(period = AggregationPeriod.FOUR_HOURS), length);
LR.setDefaultColor(Color.Cyan);
def dist = AbsValue(LR - close);
plot Upper = LR + HighestAll(dist);
plot Lower = LR - HighestAll(dist);``````

#### flyer01

##### New member
@tomsk I am getting similar results with the code in post #1 and post #2. I think the issue is it's using a length of 100 on the base chart. For example, if I have a one hour chart displayed, the four hour aggregation is based on 100 one hour bars instead of 100 four hour bars.

Today (Sunday 12/1), the code in post #2 on /ES has a midline value of 3076.75 100 bars ago on the 4 hour chart. When switching to a 1 hour chart, the beginning midline value is 3129.86. Since the aggregation period 4 hours, I would expect the beginning midline value to always be 3076.75.

Edit: added the word "beginning" in italics.

#### tomsk

##### Well-known member
VIP
Just make sure you have sufficient bars on your charts. On a 20 day 4 hour chart I have 78 bars while on 20 day 1 hour I have 212 bars
I personally don't use MTF studies.

Inertia and InertiaAll Comparison _Nube

18:20 Nube: I find I have a difficult time explaining Inertia() and came up with this as a tool to do that for me.

Code:
``````# Comparing Inertia and InertiaAll
# Nube 4.20.2020
#for visualizing what thinkScript Inertia() is doing

Script E {
input y = close;
input x = close;
plot E_ = fold i = 0 to x with s do s + GetValue(y, i);
}

input n = 5;#hint n: for regression length
input barsAgo = 0;#hint barsAgo: number of bars ago to stop the regression line calculation

# Variables
def c = close;
def nan = Double.NaN;
def bar = BarNumber();
def currentBar = HighestAll(if !IsNaN(c) then bar else nan);
def startBar = currentBar - n - Max(0, barsAgo);
def endBar = currentBar - Max(0, barsAgo);
def x = CompoundValue(1,
if Between(bar, startBar, endBar)
then x + 1
else x, 0);
def Ex  = E(x, endBar - startBar);
def Ey  = E(c, endBar - startBar);
def Exy = E(c * x, endBar - startBar);
def Exx = E(x * x, endBar - startBar);
def b = ((endBar - startBar) * Exy - Ex * Ey) /
((endBar - startBar) * Exx - Ex * Ex);
def getB = if Between(bar, startBar, endBar)
then GetValue(b, bar - endBar)
else nan;
def a = (HighestAll(if bar == endBar then Ey else nan) - getB *
HighestAll(if bar == endBar then Ex else nan)) / (endBar - startBar);

# plots
plot Regression_Bars_Ago =  a + getB * x;
plot ToS_Inertia = Inertia(c, n);``````

• markos

#### Daedalus77

##### New member
I'm tinkering with the InertiaAll function for the first time today. Does anyone know how to get it to extend the lines to the end of the chart (e.g. 50 bars into the future)?

Here's the code from ToS Learning Center (slightly modified)

Code:
``````declare upper;
input price = close;
input length = 200;

plot MiddleLR = InertiaAll(price,length,no,yes);
def dist = HighestAll(AbsValue(MiddleLR - price));
plot UpperLR = MiddleLR + dist;
plot LowerLR = MiddleLR - dist;``````

Disregard that. I wasn't specifying the start date and time.

#### john3

##### Active member
2019 Donor
According to TOS, the inertia plot is the linear regression curve of the Relative Volatility Index (RVI) using the least-square approximation method. TOS plots it as a lower study, but I have seen the code that plots it on an intraday chart.

According to TOS, RVI measures the direction of volatility on a scale from 0 to 100, it is also plotted as a lower indicator.

But I've seen the code that plots inertia on an intraday chart, not as a lower study. Specifically, here is a code: Specifically, here is the code:

Code:
``plot pp = Inertia(hl2(period = AggregationPeriod.DAY)[offset], 252);``

Question: Would somebody please explain in plain English what that piece of code does?

Thank you.

#### markos

##### Well-known member
VIP
@john3 That appears to be a straight forward linear regression line on a daily aggregation set to display on a 1 year chart. I don't follow the math very well, but, here is a label that shows a linear regression math.

Code:
``````# Linear Regression Math behind Inertia
# TOS Inertia is equivalent
# Mobius
# V01.2011

script LR {
input y = close;
input n = 60;
def x = x + 1;
def a = (n * Sum(x * y, n) - Sum(x, n) * Sum(y, n) ) /
(n * Sum(Sqr(x),n) - Sqr(Sum(x, n)));
def b = (Sum(Sqr(x), n) * Sum(y, n) - Sum(x, n) * Sum(x * y, n) ) /
(n * Sum(Sqr(x), n) - Sqr(Sum(x, n)));
plot InertiaTS = a * x + b;
}
input y = close;
input n = 252;

def lastBar = barNumber();
plot LinReg1 = LR(y, 252);
LinReg1.EnableApproximation();
plot TOS_Inertia = inertia(close, n); # TOS Inertia Function
#TOS_Inertia.SetStyle(Curve.Long_Dash);
#TOS_Inertia.SetLineWeight(3);
#TOS_Inertia.SetDefaultColor(Color.Yellow);
def LinDev = AbsValue(LinReg1 - close);
AddLabel(1, "Linear Deviation N(" + n + ") = " + AsDollars(LinDev) + "  " + AsPercent(LinDev / close), color.white);``````

Last edited by a moderator:

#### tomsk

##### Well-known member
VIP
There are actually two very similar sounding TOS library calls used in ThinkScript. Inertia() is TOS method for finding a linear regression curve. On the other hand, inertiaAll() is TOS method for finding a linear regression line

#### john3

##### Active member
2019 Donor
Thank you @markos and @tomsk.

plot pp = Inertia(hl2(period = AggregationPeriod.DAY)[offset], 252);

The one above has hl2 in it, so it isn't Inertia() or inertialAll(), which begs the question, what is it?

#### john3

##### Active member
2019 Donor
@tomsk So it takes 50% of a prior day's range (an anchor point) and plots a yearly linear regression projection from it?

#### TK_44

##### New member
@john3 That code is just using HL2, or (High + Low)/2, as the data points (as opposed to Close, etc.). It plots a linear regression of the HL2 data point of each bar for the past 252 bars. Think of it like a 252 period moving average using HL2, but instead it is using linear regression. In the case of the code that you posted above it is using a daily aggregation, so the plot will be calculated off of daily bars.

• markos

#### john3

##### Active member
2019 Donor
@TK_44 Makes sense now, thank you, I appreciate it.

But, it plots it on an intraday chart, for example, 20 days/15 min. I have always been told that TOS only uses the data visible on a chart. In other words, if I am looking at an intraday chart (20 days, 15 min), it will not plot correctly something that calls for, say, 30 days. Maybe it doesn't apply to a daily aggregation? Because how can it plot 252 days on 20 days?

#### TK_44

##### New member
@john3 It may not apply to daily aggregation, but I’m not for sure. I just say this because you can plot for example an Average Daily Volume with a 30 day look back on a 5 day/5 min chart. I will have to load the study and check it out, but somebody more knowledgeable than me can probably tell you.

• john3