diff --git a/czsc/__init__.py b/czsc/__init__.py index ef88fb6b7748786b5ff806deb09c290a40c74cce..42b477738f095f60fdb23fc8a47633ee1a4d63f7 100644 --- a/czsc/__init__.py +++ b/czsc/__init__.py @@ -1,7 +1,7 @@ # coding: utf-8 from .analyze import KlineAnalyze, find_zs -from .ta import SMA, EMA, MACD +from .ta import SMA, EMA, MACD, KDJ __version__ = "0.5.5" __author__ = "zengbin93" diff --git a/czsc/ta.py b/czsc/ta.py index fef5693ead632e9a2d4e9430241e515936453285..3d1ab1df2d9d65491da1e2a4c3f5b6e2b15f4c38 100644 --- a/czsc/ta.py +++ b/czsc/ta.py @@ -71,3 +71,51 @@ def MACD(close: np.array, fastperiod=12, slowperiod=26, signalperiod=9): return diff, dea, macd +def KDJ(close: np.array, high: np.array, low: np.array): + """ + + :param close: np.array + 收盘价序列 + :param high: + :param low: + :return: + """ + n = 9 + hv = [] + lv = [] + for i in range(len(close)): + if i < n: + h_ = high[0: i+1] + l_ = low[0: i+1] + else: + h_ = high[i - n + 1: i + 1] + l_ = low[i - n + 1: i + 1] + hv.append(max(h_)) + lv.append(min(l_)) + + hv = np.array(hv, dtype=np.double) + lv = np.array(lv, dtype=np.double) + rsv = np.where(hv == lv, 0, (close - lv) / (hv - lv) * 100) + + k = [] + d = [] + j = [] + for i in range(len(rsv)): + if i < n: + k_ = rsv[i] + d_ = k_ + else: + k_ = (2 / 3) * k[i-1] + (1 / 3) * rsv[i] + d_ = (2 / 3) * d[i-1] + (1 / 3) * k_ + + k.append(k_) + d.append(d_) + j.append(3 * k_ - 2 * d_) + + k = np.array(k, dtype=np.double) + d = np.array(d, dtype=np.double) + j = np.array(j, dtype=np.double) + return k, d, j + + + diff --git a/test/test_ta.py b/test/test_ta.py index c959b67e3fd3416b77075007b2e26b4737426d09..def5b8a72fb639831a02d12c8aa3ec2e72c718d7 100644 --- a/test/test_ta.py +++ b/test/test_ta.py @@ -11,8 +11,8 @@ import czsc warnings.warn("czsc version is {}".format(czsc.__version__)) -cur_path = os.path.split(os.path.realpath(__file__))[0] -# cur_path = "./test" +# cur_path = os.path.split(os.path.realpath(__file__))[0] +cur_path = "./test" file_kline = os.path.join(cur_path, "data/000001.SH_D.csv") kline = pd.read_csv(file_kline, encoding="utf-8") kline.loc[:, "dt"] = pd.to_datetime(kline.dt) @@ -37,3 +37,13 @@ def test_macd(): assert round(dea[-1], 2) == 110.62 assert round(dea[-5], 2) == 83.51 + +def test_jdk(): + high = np.array([x['high'] for x in bars], dtype=np.double) + low = np.array([x['low'] for x in bars], dtype=np.double) + k, d, j = czsc.KDJ(close, high, low) + + assert round(k[-1], 2) == 59.94 + assert round(d[-1], 2) == 80.47 + assert round(j[-1], 2) == 18.87 +