Regression Line Formula

3730 vizualizações
3730

A regression line is simply a single line that best fits the data.

In the pinescript you can plot a linear regression line using the linreg function.

Here i share the entire calculation of the linear regression line, you are free to take the code and modify the functions in the script for creating your own kind of filter.

Hope you enjoy :)
Hi Alex this is very useful thanks!

I am trying to us the homodyne discriminator from ehlers ideal rsi, to set a variable length based on the price cycle for some of my indicators. this does work but the pinescript standard functions do not like dealing with a variable length, so i normally have to code out the functions... i am having such a problem with linreg, so finding your formula is very useful but it has the stock function 'sma' stdev' and 'correlation' which stops the code dead...

I wondered if you might be able to write out the stdev and the correlation functions in pinescript, if you have time.

Let me know what you think

Cheers

H
Resposta
DannyBaker
@DannyBaker, Hi, you might want to check my adaptive least square moving average here . The function m() is an adaptive exponential moving average, in you case you can either use the frequency as alpha (1/DominantPeriod), if you want an adaptive simple moving average then use : a = cum(source), b = (a - a)/P where P is round(DominantPeriod)
Resposta
alexgrover
@alexgrover, thanks for the suggestion! sadly your answer is a bit beyond my knowledge right now... also it looks like the ma is to responsive for my needs.

Cheers

H
Resposta
Hi,

I'm trying to code a Linear Regression Band indicator that has the same standard deviations as the built in Linear Regression indicator/drawing tool.

The mean of my indicator (code below) is correct but the standard deviation bands are always in the wrong place when I compare them to the same length LR indicator or drawing tool.

Does anyone know how to fix this issue so that the bands match the built in drawing tool/indicator?

Here is my code ...

//@version=3
study(shorttitle="LRB", title="Linear Regression Bands", overlay=true)
source = close
length = input(50, minval=1)
offset = 0
mult = input(2, title="StDev", minval=0.001, maxval=5)
basis = linreg(source, length, offset)
dev = mult * stdev(source, length)
upper = basis + dev
lower = basis - dev
plot(basis, color=red)
plot(upper, color=blue)
plot(lower, color=blue)

Many thanks! Pete
Resposta
peterlondon
@peterlondon, Hi @peterlondon , the linear regression indicator/drawing tool use a linear regression with constant parameters, i mean by that that the formula y = ax + b use constant numbers for a and b, the same thing happen with the standard deviation, its a constant, but by using the linreg function and the standard deviation in pine you are using running parameters, i mean by that that they are calculated on a proportion of the entire data and they actualize when there is a new data point, so you cant match the linear regression indicator by using the code you provided, there is certainly a way and i think it has been done to do what you are looking for but the code is more complicated and use more complex contraptions of pinescript, the result will also be slower than the linear regression indicator/drawing tool of pinescript.
Resposta
alexgrover
@alexgrover, Hey Alex and thanks for the great reply. I think that I can get my head around the constant vs running parameters problem but I still am not sure how to fix it.
Any ideas on how the correct code would look? Or where I could look to get some more info?
Thanks do much, Pete
Resposta
hello alex, i am looking at your regression line formula on tradingview. i hope to do something with linereg and i was hoping to have a chat with you if you dont mind.
Kindly keep in touch immediately you get this male. Thank you
Resposta
the linreg function on the trading view pine asks for an offset, which number in your formula is representing that?
Resposta
CAB_Member
@CAB_Member, add the parameter offset as an input, then replace x by (x - offset) in line 16
Resposta