TA-Lib / ta-lib-python

Python wrapper for TA-Lib (http://ta-lib.org/).
http://ta-lib.github.io/ta-lib-python
Other
9.55k stars 1.75k forks source link

Is it possible to add KDJ? #99

Closed zhenyiy closed 8 months ago

zhenyiy commented 8 years ago

There is no KDJ in this package. Is it possible to add it? Thanks

mrjbq7 commented 8 years ago

What is KDJ?

zhenyiy commented 8 years ago

It is a momentum indicator frequently used in Asia. http://vb.letsfx.com/showthread.php/797-I-want-to-know-abou-KDJ-indicator

mrjbq7 commented 8 years ago
%K = 100[(C – L5close)/(H5 – L5)]

C = the most recent closing price
L5 = the low of the five previous trading sessions
H5 = the highest price traded during the same 5 day period.

The formula for the more important %D line looks like this:
%D = 100 X (H3/L3)

Now calculate %J = 3 x D – 2 x K
mrjbq7 commented 8 years ago

That looks easy to implement, I generally haven't been interested in adding indicators that aren't in the underlying ta-lib project (http://ta-lib.org)...

Seems like you could implement it yourself by doing something like this:

import pandas as pd

def KDJ(H, L, C):
    L5 = pd.rolling_min(L, 5)
    H5 = pd.rolling_max(H, 5)
    K = 100 * (C - L5 / (H5 - L5) )
    D = 100 * H3 / L3
    J = (3 * D) - (2 * K)
    return K, D, J
mrjbq7 commented 8 years ago

Note: I'm not sure if H3 and L3 are typos for H5 and L5, or if they mean the 3-day versions.

Also, not sure what L5close is in the pseudocode.

zhenyiy commented 8 years ago

It is a typo. Usually people use 9 days instead of 5 days or 3 days. I think I can implement it by myself. Thanks.

631068264 commented 7 years ago

@zhenyiy you can use talib.STOCH to get K and D and then J = (3 D) - (2 K)

lfzCarlosC commented 5 years ago

But the k d value is not correct as what you can see in other website with the same parameter 9,3,3

mrjbq7 commented 5 years ago

Not correct how?

lfzCarlosC commented 5 years ago

This is one example of the result of what I have when using talib.STOCH:

2019-04-21 20:00:00 84.757888 87.737427 93.696504

This is what I got from tradingview:

Screen Shot 2019-04-21 at 20 46 04

I set parameter to 9, 3, 3 for generating the STOCH value for the sake of comparing the vlaue with the kdj value in tradingview. The value at 20:00:00 should be consistent with the value at 18:00:00 here

lfzCarlosC commented 5 years ago

Also, even if I set parameter to 14,3,3 to compare stoch value with the same indicator of what I saw in Trdaingview, it is also different.

lfzCarlosC commented 5 years ago

I opened an issue https://github.com/mrjbq7/ta-lib/issues/261

lfzCarlosC commented 5 years ago

It is more like a talib problem

prashanth058 commented 4 years ago
def KDJ(H, L, C):
    L5 = pd.rolling_min(L, 9)
    H5 = pd.rolling_max(H, 9)
    RSV = 100 * ((C - L5) / (H5 - L5)).values

    k0 = 50
    k_out = []
    for j in range(len(RSV)):
        if RSV[j] == RSV[j]: # check for nan
            k0 = 1/3 * RSV[j] + 2/3 * k0
            k_out.append(k0)
        else:
            k_out.append(np.nan)

    d0 = 50
    d_out = []
    for j in range(len(RSV)):
        if k_out[j] == k_out[j]:
            d0 =1/3 * k_out[j] + 2/3 * d0
            d_out.append(d0)
        else:
            d_out.append(np.nan)

    J = (3 * np.array(k_out)) - (2 * np.array(d_out))
    return pd.Series(J,  H.index)

The above might give KDJ values similar to the ones you see on trading view.

sandy-mahajan commented 3 years ago
def KDJ(H, L, C):
    L5 = pd.rolling_min(L, 9)
    H5 = pd.rolling_max(H, 9)
    RSV = 100 * ((C - L5) / (H5 - L5)).values

    k0 = 50
    k_out = []
    for j in range(len(RSV)):
        if RSV[j] == RSV[j]: # check for nan
            k0 = 1/3 * RSV[j] + 2/3 * k0
            k_out.append(k0)
        else:
            k_out.append(np.nan)

    d0 = 50
    d_out = []
    for j in range(len(RSV)):
        if k_out[j] == k_out[j]:
            d0 =1/3 * k_out[j] + 2/3 * d0
            d_out.append(d0)
        else:
            d_out.append(np.nan)

    J = (3 * np.array(k_out)) - (2 * np.array(d_out))
    return pd.Series(J,  H.index)

The above might give KDJ values similar to the ones you see on trading view.

Hi Prashant,

Does your code work for kdj calculation?