Closed zhenyiy closed 8 months ago
What is KDJ?
It is a momentum indicator frequently used in Asia. http://vb.letsfx.com/showthread.php/797-I-want-to-know-abou-KDJ-indicator
%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
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
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.
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.
@zhenyiy you can use talib.STOCH to get K and D and then J = (3 D) - (2 K)
But the k d value is not correct as what you can see in other website with the same parameter 9,3,3
Not correct how?
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:
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
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.
I opened an issue https://github.com/mrjbq7/ta-lib/issues/261
It is more like a talib problem
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.
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?
There is no KDJ in this package. Is it possible to add it? Thanks