twopirllc / pandas-ta

Technical Analysis Indicators - Pandas TA is an easy to use Python 3 Pandas Extension with 150+ Indicators
https://twopirllc.github.io/pandas-ta/
MIT License
5.23k stars 1.02k forks source link

linreg returns a wrong value #676

Closed monkey0619 closed 8 months ago

monkey0619 commented 1 year ago

Which version are you running? The lastest version is on Github. Pip is for major releases. 0.3.14b0

Do you have TA Lib also installed in your environment? No

Have you tried the development version? Did it resolve the issue? Not tried

Describe the bug A wrong return. I checked the linreg help and found the following calculation formula. return m * (length - 1) + b

The actual return is m * (length - 2) + b with coefficient of 10.0952380952381 and intercept of 11.1666666666667 for the first return 71.738095. In this case, length is 8 and (length - 2) equals 6.

To Reproduce

ta.linreg(pd.Series([10,25,33,38,49,61,72,84,99]), 8)
0          NaN
1          NaN
2          NaN
3          NaN
4          NaN
5          NaN
6          NaN
7    71.738095
8    84.023810

**Expected behavior**
0          NaN
1          NaN
2          NaN
3          NaN
4          NaN
5          NaN
6          NaN
7    81.8
8    94.6

Screenshots 圖片

Additional context Add any other context about the problem here.

Thanks for using Pandas TA!

twopirllc commented 1 year ago

Hello @monkey0619,

Thanks for sharing a potential solution for v0.3.14b. Furthermore, as mentioned in Issue #659:

The Linear Regression indicator, like many others, is due for an update as well as be rewritten using numpy to speed it up. ... Unfortunately there are too many things outstanding for one person to do. So hopefully someone can help by checking out the development branch and submit a PR to get this resolved sooner. 😎

Kind Regards, KJ

twopirllc commented 1 year ago

@monkey0619,

You did not specify which library or trading platform it is incongruent with. Was it TA-Lib, TradingView, ToS, IBKR, MT4/5, Sierra, ... ?

Note: It is really hard to easily test anything but TA-Lib. TradingView requires a manual approach. Any others are even more cumbersome to compare. :sob:

Thanks

xieliaing commented 1 year ago

I compared the result with TA-Lib, his result is consistent with what's expected from TA-lib, see screenshot below: image

When I get time, I could try to fix it, and do a PR/Contribution.

xieliaing commented 1 year ago

Looks like it requires tsf=True with all other parameter outputs such as "intercept", "r", etc., set to False to be compatible with TA-Lib. So TA-Lib output: m * length + b, not m * (length - 1) + b

image

twopirllc commented 9 months ago

@xieliaing

If TA Lib is installed, by default TA Lib will run. Thus it must be disabled using talib=False in the call.

Basic linreg with ta.linreg(close, length=8, talib=False) with the first Series is Pandas TA, the following is TA Lib.

Screenshot 2023-12-23 at 5 48 35 PM

Kind Regards, KJ

Does this work as expected for you as well?

xieliaing commented 8 months ago

Thank you, KJ, it works for me.