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.21k stars 1.02k forks source link

TA-Lib option isn't working on "VIDYA" Indicator #738

Closed Hellooosir closed 8 months ago

Hellooosir commented 9 months ago

Which version are you running? The lastest version is on Github. Pip is for major releases. -> python 3.12.0 / pandas_ta 0.3.14b

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

Have you tried the development version? Did it resolve the issue? -> No

When I use "VIDYA" Indicator, "talib = False" isn't working. "CMO" Indicator is working well, however it doesn't working on "VIDYA" Indicator.

x = ta.cmo(df['close'], 14, talib = True)
x_2 = ta.cmo(df['close'], 14, talib = False)

y = ta.vidya(df['close'], 14, talib = True)
y_2 = ta.vidya(df['close'], 14, talib = False)
         CMO_14     CMO_14     VIDYA_14     VIDYA_14
1495 -29.713676 -27.812194  2086.131474  2086.131474
1496 -29.664858 -44.916821  2085.508539  2085.508539
1497 -28.004686 -44.489495  2084.947465  2084.947465
1498 -39.347130 -53.856041  2083.996908  2083.996908
1499 -36.518572 -60.833053  2083.043297  2083.043297

I don't know the reason.

twopirllc commented 9 months ago

Hello @Hellooosir,

There is no vidya in TA Lib. Seems I forgot to remove the talib option before it's submission to the library. Apologies for the confusion.

Kind Regards, KJ

Hellooosir commented 9 months ago

Hello @twopirllc , I always enjoy using this library. Thank you very much.

There is no "vidya" in TA Lib. However, "vidya" is calculated based on the "cmo" indicator. I believe there is an option for "cmo" values, as seen on below.

help(ta.vidya)
talib (bool): If True, uses TA-Libs implementation for CMO. Otherwise uses EMA version. Default: True

It appears that the "cmo" indicator has different values in the TA-Lib and EMA versions. Therefore, I think there is no EMA version of "vidya".

x = ta.cmo(df['close'], 14, talib = True)
x_2 = ta.cmo(df['close'], 14, talib = False)

y = ta.vidya(df['close'], 14, talib = True)
y_2 = ta.vidya(df['close'], 14, talib = False)

         CMO_14     CMO_14     VIDYA_14     VIDYA_14
1495 -29.713676 -27.812194  2086.131474  2086.131474
1496 -29.664858 -44.916821  2085.508539  2085.508539
1497 -28.004686 -44.489495  2084.947465  2084.947465
1498 -39.347130 -53.856041  2083.996908  2083.996908
1499 -36.518572 -60.833053  2083.043297  2083.043297

Could you please check this?

Best Regards, SH

twopirllc commented 9 months ago

@Hellooosir

If I understand correctly, you want the option for TA Lib's cmo correct? Either way, I can add it.

I always enjoy using this library. Thank you very much.

Thank you KJ

twopirllc commented 9 months ago

Hello @Hellooosir,

There is now an option for vidya to use TA Lib's cmo when calling ta.vidya(df['close'], talib=True) instead of the default.

To utilize this option, you will need to install the development version.

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

Let us know if it works as intended.

Kind Regards KJ

Hellooosir commented 9 months ago

Hello @twopirllc ,

There is now an option for vidya to use TA Lib's cmo when calling ta.vidya(df['close'], talib=True) instead of the default.

Sorry for the late reply. I was testing various indicators. Awesome! Thank you very much. I appreciate it.

But I don't know why there is a difference TradingView(TV) and pandas_ta value. Did I miss something?

This is TV Pine Script code.

length = input(type=input.integer)
src = input(title="source", type=input.source, defval=close)

// Chande Momentum Oscillator
getCMO(src, length) =>
    mom = change(src)
    upSum = sum(max(mom, 0), length)
    downSum = sum(-min(mom, 0), length)
    out = (upSum - downSum) / (upSum + downSum)
    out

cmo = abs(getCMO(src, length))

alpha = 2 / (length + 1)

vidya = 0.0
vidya := src * alpha * cmo + nz(vidya[1]) * (1 - alpha * cmo)

And this is Pandas_ta code.

def vidya(close, length=None, drift=None, offset=None, **kwargs):
    """Indicator: Variable Index Dynamic Average (VIDYA)"""
    # Validate Arguments
    length = int(length) if length and length > 0 else 14
    close = verify_series(close, length)
    drift = get_drift(drift)
    offset = get_offset(offset)

    if close is None: return

    def _cmo(source: Series, n:int , d: int):
        """Chande Momentum Oscillator (CMO) Patch
        For some reason: from pandas_ta.momentum import cmo causes
        pandas_ta.momentum.coppock to not be able to import it's
        wma like from pandas_ta.overlap import wma?
        Weird Circular TypeError!?!
        """
        mom = source.diff(d)
        positive = mom.copy().clip(lower=0)
        negative = mom.copy().clip(upper=0).abs()
        pos_sum = positive.rolling(n).sum()
        neg_sum = negative.rolling(n).sum()
        return (pos_sum - neg_sum) / (pos_sum + neg_sum)

    # Calculate Result
    m = close.size
    alpha = 2 / (length + 1)
    abs_cmo = _cmo(close, length, drift).abs()
    vidya = Series(0, index=close.index)
    for i in range(length, m):
        vidya.iloc[i] = alpha * abs_cmo.iloc[i] * close.iloc[i] + vidya.iloc[i - 1] * (1 - alpha * abs_cmo.iloc[i])
    vidya.replace({0: npNaN}, inplace=True)

    # Offset
    if offset != 0:
        vidya = vidya.shift(offset)

    # Handle fills
    if "fillna" in kwargs:
        vidya.fillna(kwargs["fillna"], inplace=True)
    if "fill_method" in kwargs:
        vidya.fillna(method=kwargs["fill_method"], inplace=True)

    # Name & Category
    vidya.name = f"VIDYA_{length}"
    vidya.category = "overlap"

    return vidya

Best Regards, SH

twopirllc commented 8 months ago

@Hellooosir

There might be difference because you are comparing apples (TV data) and oranges (your data) data which is very common thing to do.

Hellooosir commented 8 months ago

@twopirllc

I understand it. Thank you for helping me :)

Best Regards, SH