技术指标KD

博主: Simon Lin 创建于: Mar 1, 2019 更新于: Mar 1, 2019
分类: stock
标签: finance stock

  本着不重复造轮子的原则,基本使用talib的库来实现各种股票的技术指标的运算。
  以下为个人观点,主要来自自己的数值验证的结果。这次讲解KD指标,注意不是KDJ。对于某些百科上乱写的东西,真是误导人。KDJ的算法上次已经分享,有兴趣的去那里进行参考。

算法

关于算法的说明:
1.未成熟随机值(RSV)=(收盘价-N日内最低价)/(N日内最高价-N日内最低价)*100
2.K=RSV的M1日移动平均
3.D=K的M2日移动平均
4.参数N设置为9日,参数M1设置为3日,参数M2设置为3日。

这里可以看到,K和D的值和KDJ指标里的K和D的值算法根本不一致,所以出来的数值也完全不同嘛。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#KD具体计算:
import numpy
import talib
from math import isnan

df=get_price(frequency='daily',fields=['close','high','low'], security='600036.XSHG', skip_paused=False, fq='pre',start_date='2018-8-16',end_date='2018-12-12')

def talib_KDJ(data, fastk_period=9, slowk_period=3, slowd_period=3):
indicators={}
high = np.array([v['high'] for v in data])
low = np.array([v['low'] for v in data])
close = np.array([v['close'] for v in data])
#计算kd指标
indicators['k'], indicators['d'] = talib.STOCH(high, low, close,
fastk_period=fastk_period,
slowk_period=slowk_period,
slowd_period=slowd_period,
slowk_matype=0,slowd_matype=0)

indicators['j'] = 3 * indicators['k'] - 2 * indicators['d']
return indicators

data = df.to_dict('records')

bbb=talib_KDJ(data)

df['k']=bbb['k']
df['d']=bbb['d']
df['j']=bbb['j']

dm=df[['k','d','j']]
dm.plot(figsize=(20,10))
df

本代码get_price函数使用了聚宽平台的函数,只要返回合适的格式,完全可以使用别的平台或者原始数据来代替。
plot函数会把图形简单的画出来参考,不需要的可以去掉

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
date	close	high	low	k	d	j
2018-08-16 26.74 27.14 26.20 NaN NaN NaN
2018-08-17 26.52 27.20 26.44 NaN NaN NaN
2018-08-20 26.96 27.11 26.42 NaN NaN NaN
2018-08-21 27.10 27.42 26.89 NaN NaN NaN
2018-08-22 27.04 27.20 26.82 NaN NaN NaN
2018-08-23 27.30 27.45 26.82 NaN NaN NaN
2018-08-24 27.70 28.30 26.96 NaN NaN NaN
2018-08-27 28.54 28.61 27.85 NaN NaN NaN
2018-08-28 28.62 29.01 28.54 NaN NaN NaN
2018-08-29 28.76 29.00 28.45 NaN NaN NaN
2018-08-30 28.14 28.87 28.08 NaN NaN NaN
2018-08-31 28.29 28.75 27.98 NaN NaN NaN
2018-09-03 28.14 28.44 27.96 64.602176 73.396036 47.014455
2018-09-04 28.64 28.75 27.93 70.167428 69.798762 70.904760
2018-09-05 27.69 28.48 27.68 59.662917 64.810840 49.367071
2018-09-06 27.50 27.85 27.43 41.048386 56.959577 9.226005
2018-09-07 28.17 28.20 27.60 28.958526 43.223277 0.429026
2018-09-10 27.90 28.30 27.78 27.067376 32.358096 16.485936
2018-09-11 27.40 28.10 27.14 30.600217 28.875373 34.049904
2018-09-12 27.40 27.60 27.01 22.459667 26.709087 13.960827
2018-09-13 27.76 28.02 27.29 26.848714 26.636199 27.273744
2018-09-14 27.88 27.99 27.68 38.505747 29.271376 56.974489
2018-09-17 27.45 27.80 27.37 41.011807 35.455423 52.124575
2018-09-18 28.18 28.25 27.38 56.876549 45.464701 79.700245
2018-09-19 28.40 28.65 27.96 68.461915 55.450090 94.485564
2018-09-20 28.51 28.85 28.37 85.658504 70.332323 116.310866
2018-09-21 30.30 30.30 28.45 88.759279 80.959899 104.358038
2018-09-25 29.62 30.09 29.32 86.951015 87.122933 86.607181
2018-09-26 30.45 30.99 29.41 88.245571 87.985288 88.766136
2018-09-27 30.43 30.69 30.01 83.089033 86.095206 77.076686
... ... ... ... ... ... ...
2018-11-01 29.05 29.65 28.66 43.269231 37.091393 55.624906
2018-11-02 30.33 30.45 29.41 65.346535 46.354828 103.329948
2018-11-05 30.00 30.23 29.71 77.810570 62.142112 109.147485
2018-11-06 29.87 30.11 29.36 87.348735 76.835280 108.375645
2018-11-07 29.57 30.20 29.53 78.987899 81.382401 74.198894
2018-11-08 30.01 30.37 29.80 79.097910 81.811514 73.670700
2018-11-09 28.60 29.60 28.49 63.991904 74.025904 43.923905
2018-11-12 28.56 28.70 28.23 45.294494 62.794769 10.293944
2018-11-13 28.58 28.77 28.05 24.162756 44.483052 -16.477835
2018-11-14 28.16 28.65 28.00 14.492937 27.983396 -12.487981
2018-11-15 28.38 28.47 28.08 14.882567 17.846087 8.955528
2018-11-16 28.48 28.68 28.19 14.272511 14.549338 13.718856
2018-11-19 28.95 29.04 28.45 25.457103 18.204060 39.963188
2018-11-20 28.42 28.84 28.31 26.019691 21.916435 34.226202
2018-11-21 28.40 28.43 28.10 27.601969 26.359587 30.086732
2018-11-22 28.30 28.45 28.10 23.855891 25.825850 19.915972
2018-11-23 27.88 28.35 27.88 17.948718 23.135526 7.575102
2018-11-26 28.27 28.35 27.91 20.822281 20.875630 20.715583
2018-11-27 27.99 28.38 27.85 15.128465 17.966488 9.452419
2018-11-28 28.28 28.35 28.05 27.173283 21.041343 39.437163
2018-11-29 28.30 28.60 28.30 28.571429 23.624392 38.465501
2018-11-30 28.55 28.58 28.21 48.218884 34.654532 75.347587
2018-12-03 29.46 29.63 28.93 66.323878 47.704730 103.562175
2018-12-04 29.57 29.63 29.31 85.928574 66.823779 124.138165
2018-12-05 29.37 29.57 29.00 90.823970 81.025474 110.420962
2018-12-06 28.90 29.15 28.78 80.337079 85.696541 69.618154
2018-12-07 28.58 29.19 28.53 61.797753 77.652934 30.087391
2018-12-10 28.34 28.50 28.22 39.451477 60.528769 -2.703108
2018-12-11 28.51 28.55 28.28 26.830809 42.693346 -4.894265
2018-12-12 28.46 29.00 28.41 19.028942 28.437076 0.212673

验证

2018-12-12日 招商银行600036 KD数值为19.028942 28.437076,当天KDJ数值为31.235851、44.166341、5.374871,所以再次强调,KD不是KDJ。
我参考了一下个别的PC上的股票软件,它给的KD指标的数值和KDJ里的前两个指标完全一致,而不是我这里给的数值。可能很多人搞混这个东西就是这个原因吧。也有可能是我的验证不够严密,欢迎留言反驳。


打赏 支付宝打赏 微信打赏

如果文章对你有帮助,欢迎点击上方按钮打赏作者