提交 74727a72 编写于 作者: Z zengbin93

0.5.5 新增 KDJ

上级 4eef818d
# 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"
......
......@@ -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
......@@ -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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册