Linear Regression Line w/ Difference Histogram

User avatar
dataheck
Posts: 30
Joined: 19 Nov 2019
Location: Amherst, Nova Scotia
Been thanked: 5 times
Contact:

Linear Regression Line w/ Difference Histogram

Postby dataheck » 14 Mar 2020

I like the linear regression line to get a clear view of the trend, but what I really wanted was an idea of how far away we are from that regression line with respect to recent history. So, I took the built-in linear regression line indicator and added a difference histogram to it.

Make sure you plot it on its own subchart. The regression line should stay where it's supposed to be.

Warning regarding this and any other moving average / oscillator things: the regression changes with each new tick. The difference histogram definitely looks mean reverting, but it's guaranteed to be. You don't know if an "over extension" will be corrected by the line meeting price, or by the price meeting the line.

The difference is calculated by the latest regression value against the latest price at each point in the histogram. This is why you will find it doesn't currently match up with the line, since the line has changed since that point in history. Might add a separate histogram that always recalculates with the current line.
Annotation 2020-03-14 110416.png
(23.17 KiB) Not downloaded yet
e:

Updated to add "show_deviation" parameter, which defaults to true. If true, the historgram subplot shows the distance from the line to the price. If false, it shows the slope of the linear regression instead. Unfortunately we can't show both in different subplots, and showing both in the same plot is very messy.
Attachments
MS_Linear_Reg_Line.pln
(1.53 KiB) Downloaded 273 times
Last edited by dataheck on 30 Apr 2020, edited 1 time in total.

Emmanuel
Posts: 355
Joined: 21 May 2009
Has thanked: 109 times
Been thanked: 28 times

Re: Linear Regression Line w/ Difference Histogram

Postby Emmanuel » 30 Apr 2020

The histogram is good idea Dataheck !!!

It could give an overbought / oversold !!!

Maybe we could add the slope of the trendline in the histogram ?

I always like trendline, it gives a clear idea of the trend.

Dataheck , thank you

User avatar
dataheck
Posts: 30
Joined: 19 Nov 2019
Location: Amherst, Nova Scotia
Been thanked: 5 times
Contact:

Re: Linear Regression Line w/ Difference Histogram

Postby dataheck » 30 Apr 2020

Glad you like it!

Be careful with it, as with all "overbought"/"oversold" indicators, they have a way of always looking like they work even if they are not tradeable. They "cheat". If the price is really high above the line, it doesn't mean the price will come back to the line - the line could go up to meet the price over time and it'll look like the indicator is working great, even though you'd lose money trading it.

I tried to add the slope like you suggested, unfortunately it makes it really messy. I can't add it to a separate subchart without making a new indicator, unfortunately - see here viewtopic.php?t=49717

Instead I made a new input - "show_deviation". If it's true (the default) the histogram shows how far the price is from the line. If it's false, it instead shows the slope. So you can have whichever you prefer. I'll replace the code package in the top post here with this change.

Emmanuel
Posts: 355
Joined: 21 May 2009
Has thanked: 109 times
Been thanked: 28 times

Re: Linear Regression Line w/ Difference Histogram

Postby Emmanuel » 01 May 2020

excellent !!!! thank you


Return to “User Contributed Studies”