Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Results of linreg does not match talib.LINEARREG() #255

Closed
drixie opened this issue Mar 20, 2021 · 11 comments
Closed

Results of linreg does not match talib.LINEARREG() #255

drixie opened this issue Mar 20, 2021 · 11 comments
Labels
info Informational

Comments

@drixie
Copy link

drixie commented Mar 20, 2021

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

import pandas_ta as ta
print(ta.version)

0.2.45b0
Upgrade.

$ pip install -U git+https://github.com/twopirllc/pandas-ta

Describe the bug
I compared the results of ta.linreg() and talib.LINEARREG(). There is a significant difference. Are you doing it differently? For comparison, talib.LINEARREG() matches what is on tradingview (Pinescript linreg function). This is commonly used for the histogram of the TTM squeeze indicator

To Reproduce
df["hist_talib"] = talib.LINEARREG(df["donchian_midline"], timeperiod=20)
df["hist_pandas_ta"] = ta.linreg(df["donchian_midline"], length=20)

Expected behavior
I except the output of ta.linreg to match the output of talib.LINEARREG and pinescript linreg function

@drixie drixie added the bug Something isn't working label Mar 20, 2021
@twopirllc twopirllc added info Informational and removed bug Something isn't working labels Mar 20, 2021
@twopirllc twopirllc assigned drixie and unassigned twopirllc Mar 20, 2021
@twopirllc
Copy link
Owner

Hello @drixie,

I compared the results of ta.linreg() and talib.LINEARREG(). There is a significant difference. Are you doing it differently?

That is interesting. I do test Pandas TA linreg against TA Lib linereg in /tests/test_indicator_overlap.py which routinely passes with greater than 99% confidence accuracy. Do you have non-anecdotal evidence to support your claim? Do you have data with reproducible code and screenshots that you can provide that can help resolve this discrepancy?

This is commonly used for the histogram of the TTM squeeze indicator

There is already a TTM squeeze indicator implemented in Pandas TA. Please see help(ta.squeeze) for more information.

df["hist_talib"] = talib.LINEARREG(df["donchian_midline"], timeperiod=20)
df["hist_pandas_ta"] = ta.linreg(df["donchian_midline"], length=20)

How is your df["donchian_midline"] calculated? Is it using Pandas TA's ta.donchian, TA Lib, TradingView or some another platform? Hard to isolate with an unidentified source.

Kind Regards,
KJ

@drixie
Copy link
Author

drixie commented Mar 21, 2021

@twopirllc

First off, thanks for your super prompt response, and for the amazing work you have done in this module!

I went down this rabbit hole because I was trying to implement LazyBear TTM Squeeze in an Algo, so I was delighted to see that you coded that option in your version of TTM Squeeze.

ta.squeeze gives the same results as applying td.linreg on the channel line computed in SQZMOM_LB which is different from what I get with talib.LINEARREG and eyeballing values on TradingView

I put together an easy-to-follow comparison on Colab here: https://colab.research.google.com/drive/16jPle9Dy5I8F5NugsBkKlOtRl_19FR0N?usp=sharing

@drixie drixie removed their assignment Mar 21, 2021
@twopirllc
Copy link
Owner

@drixie,

First off, thanks for your super prompt response, and for the amazing work you have done in this module!

Thanks. I take accuracy issues seriously. Especially claims without evidence.

I put together an easy-to-follow comparison on Colab

I would have led with this. 😉 This I can work with, anecdotal evidence I can not. I have some pending PRs and Issues to take care of but I will take a deeper look as soon as I get a chance.

I went down this rabbit hole because I was trying to implement LazyBear TTM Squeeze in an Algo, so I was delighted to see that you coded that option in your version of TTM Squeeze.

Makes sense. I try to incorporate popular and interesting indicators from TradingView and other sources time permitting. The Pandas TA version implements both John Carter's and LazyBear's version.

ta.squeeze gives the same results as applying td.linreg on the channel line computed in SQZMOM_LB which is different from what I get with talib.LINEARREG and eyeballing values on TradingView

I try to avoid eyeballing and at a minimum run a correlation test, if the indicator exists in TA Lib, to make sure it is inline. Testing TradingView requires manually exporting the data to run a correlation test as they have no publicly exported library and their indicators are in Pine Script as you know.

Thanks again for providing some code to work with. I appreciate it.

Kind Regards,
KJ

@twopirllc
Copy link
Owner

twopirllc commented Jul 31, 2021

@drixie,

New stable version v0.3.14b should help resolve this Issue. Please let me know otherwise.

You can toggle talib mode if you have TA Lib also in your environment, talib=True by default. See help(ta.linreg). However, in it's current situation ta.squeeze(lazybear=True) will run the talib version of ta.linreg if you have TA Lib also in your environment. But you can edit your local copy and manually set ta.linreg(*, **, talib=False) to use the Pandas TA version.

$ pip install pandas_ta

I will try and make a correlation test between TV and Pandas TA's squeeze(lazybear=True) soon.

Thanks,
KJ

@twopirllc
Copy link
Owner

twopirllc commented Aug 6, 2021

@drixie,

Actually I was a bit too early with v0.3.14 fixing the Issue. It should now be working correctly on the development branch. If you want to check it out before I update the main branch, it would be appreciated. Apologies for the mix up.

$ pip install -U git+https://github.com/twopirllc/pandas-ta.git@development

Also, I also did a correlation test versus TV's LazyBear's Squeeze.

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.colors as colors

import talib as tal
import pandas_ta as ta

def background_gradient(s, m, M, cmap="BuPu_r", low=0, high=0):
    rng = M - m
    norm = colors.Normalize(m - (rng * low), M + (rng * high))
    normed = norm(s.values)
    c = [colors.rgb2hex(x) for x in plt.cm.get_cmap(cmap)(normed)]
    return ["background-color: %s" % color for color in c]

path = # path to TV csv export with LB Squeeze Indicator
df = pd.read_csv(path)
df.set_index(pd.DatetimeIndex(df["time"]), inplace=True, drop=True)
# print(df.columns) # I renamed the csv column names prior to reading csv

print(f"Total Bars: {df.shape[0]}")

# Isolate Squeeze Data
sqzdf = pd.DataFrame({
    "TV_SQZlb_20_2_20_1.5": df["TV_SQZlb_20_2_20_1.5"],
    "PTA_SQZlb_20_2_20_1.5": df.ta.squeeze(bb_length=20, bb_std=2.0, kc_length=20, kc_scalar=1.5, lazybear=True, use_tr=True).iloc[:,0],
})

# Isolate Linear Regression Data
linregdf = pd.DataFrame({
    "TV_LR_20": df["TV_LINREG_20"],
    "TAL_LR_20": df.ta.linreg(length=20, talib=True),
    "PTA_LR_20": df.ta.linreg(length=20, talib=False)
})

# Clean up
df.drop(["time", "TV_LINREG_20", "TV_SQZlb_20_2_20_1.5", "zero"], axis=1, errors="ignore", inplace=True)

sqzdf.corr().style.background_gradient()
linregdf.corr().style.background_gradient()

Correlations for Squeeze and Linear Regression respectively:
Screen Shot 2021-08-05 at 8 58 14 PM

Screen Shot 2021-08-05 at 8 58 20 PM

Apologies for the delay and I appreciate your patience.

Kind Regards,
KJ

@twopirllc
Copy link
Owner

Hello @drixie,

I assume by no response that the solution provided was sufficient. Thus I will be closing this issue in a few days.

Kind Regards,
KJ

@drixie
Copy link
Author

drixie commented Oct 31, 2021

@twopirllc I justed checked using the latest version (0.3.14b). There are still differences (I used BTC/USD and AAPL). Just like before, both talib.LINEARREG and Tradingview results are matching. However, ta.linreg results do not match either of the aforementioned

@twopirllc
Copy link
Owner

twopirllc commented Oct 31, 2021

@drixie,

Again like in my last #255 (comment) and #408 (comment), I get matching results with TA Lib and TradingView for both ta.linreg and ta.squeeze using the development version and not v0.3.14b.

Have you installed the development version and used the accompanying exported TV data and indicator results from #408 (comment) and ran the code provided from either comment to corroborate the results?

There are still differences (I used BTC/USD and AAPL).

Yet, I do not see any actual results or proof. 😕


If you are unwilling to do the stuff above, then at a minimum, it would be great if you could follow this comment and provide your exported data of "BTC/USD and AAPL", as csvs, from TV with their ohlcv and linreg values and attach it to this Issue so I can do yet another correlation test.

Kind Regards,
KJ

@drixie
Copy link
Author

drixie commented Nov 2, 2021

I just tried the development build again, and it works perfectly. The results match with both TA Lib and TradingView. Probably didn't work the first time because of issues with Colab.

Once again, thanks for your time and energy working on this great library

@twopirllc
Copy link
Owner

Thank you

@pepeto
Copy link

pepeto commented Feb 14, 2024

I am finding differences in calculation with periods beyond 1024. Everything match under that threshold.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
info Informational
Projects
None yet
Development

No branches or pull requests

3 participants