Regression Line Formula

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 :)
Gỡ bỏ khỏi Script Ưa thích Thêm vào Script Ưa thích

Become a Patreon and get access to exclusive technical indicators!

You can also check out some of the indicators I made for luxalgo :

Bình luận

the linreg function on the trading view pine asks for an offset, which number in your formula is representing that?
+2 Phản hồi
alexgrover CAB_Member
@CAB_Member, add the parameter offset as an input, then replace x by (x - offset) in line 16
Phản hồi
Hey Alex,
I'm just trying to get the exact function ported from pine to python,
do you have any idea how the "correlation" function works internally, or in other words could you mokup your formula without using any build in pine functions?

+1 Phản hồi
@cmos, Hi cmos, in python try using pandas with .rolling(window).corr where window is the correlation coefficient period, pandas allow you to compute any rolling statistic by using rolling(). The correlation coefficient from pine is calculated with : sma(y*x,length) - sma(y,length)*sma(x,length) divided by stdev(length*y,length)*stdev(length*x,length) where stdev is based on the naïve algorithm.
Phản hồi
cmos alexgrover
@alexgrover, you nailed it works perfectly!
+1 Phản hồi
@cmos, can you share your code please?
Phản hồi
Soviethead fx_analyst7
@fx_analyst7, did you find the solution?
Phản hồi
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


+1 Phản hồi
alexgrover DannyBaker