内容简介:本着不重复造轮子的原则,基本使用talib的库来实现各种股票的技术指标的运算。KDJ指标太经典了,不做新人教学了。想说的是具体算法和代码逻辑呈现。KDJ指标在talib中并不存在直接的算法,有相关的KD算法,经过研究后发现,两者很类似,但不能直接等同。所以就按照搜寻来的算法手工实现了一下,并对比了股票交易软件中的数值,完全一致。关于算法的说明:
本着不重复造轮子的原则,基本使用talib的库来实现各种股票的技术指标的运算。
KDJ指标太经典了,不做新人教学了。想说的是具体算法和代码逻辑呈现。KDJ指标在talib中并不存在直接的算法,有相关的KD算法,经过研究后发现,两者很类似,但不能直接等同。所以就按照搜寻来的算法手工实现了一下,并对比了股票交易软件中的数值,完全一致。
算法
关于算法的说明:
n日RSV=(Cn-Ln)/(Hn-Ln)×100
公式中,Cn为第n日收盘价;Ln为n日内的最低价;Hn为n日内的最高价。
当日K值=2/3×前一日K值+1/3×当日RSV
当日D值=2/3×前一日D值+1/3×当日K值
J值=3 当日K值-2 当日D值
一般n取9
若无前一日K 值与D值,则可分别用50来代替。对股票交易软件的数据对比中,对比了几个新股的KDJ值,发现不一定按这个初始值计算,因为算法上最后的数值影响最大的是当天和前一天的值,再之前的值影响依次减小。但经过一定运算量迭代后,最后的数值的精度和股票软件上的一致,所以初始值是啥其实没关系,算法有一定的震荡期,最前面几天的数值尽量不要直接用,使用最后面的数值。笼统的统计,最好是计算30天以上的KDJ,最后5天的数值的精度,基本就一致了。
代码
#KDJ具体计算: import numpy import talib from math import isnan def maxmin(data,fastk_period): close_prices = np.nan_to_num(np.array([v['close'] for v in data])) max_prices = np.nan_to_num(np.array([v['high'] for v in data])) min_prices = np.nan_to_num(np.array([v['low'] for v in data])) max_close = talib.MAX(max_prices, timeperiod=fastk_period) min_close = talib.MIN(min_prices, timeperiod=fastk_period) for k in range(len(min_prices)): if k<fastk_period and k>1: aaa = talib.MIN(min_prices,timeperiod=k) bbb = talib.MAX(max_prices,timeperiod=k) min_close[k]= aaa[k] max_close[k]= bbb[k] elif k==1 or k==0: min_close[k]=min_prices[k] max_close[k]=max_prices[k] indicators= { 'close': close_prices, 'max': max_close, 'min': min_close } return indicators def self_KDJ(data, fastk_period=9, slowk_period=3, slowd_period=3): #计算kd指标 high_prices = np.array([v['high'] for v in data]) low_prices = np.array([v['low'] for v in data]) close_prices = np.array([v['close'] for v in data]) rsv = maxmin(data, fastk_period) fast_k = (rsv['close']-rsv['min'])/(rsv['max']-rsv['min'])*100 ppp = rsv['max']-rsv['min'] for t in range(len(close_prices)): if rsv['max'][t]==rsv['min'][t]: fast_k[t] = 0 slow_k1 = np.full_like(close_prices,50) slow_d1 = np.full_like(close_prices,50) for k in range(1,len(fast_k)): slow_k1[k] = slow_k1[k-1]*2/3+fast_k[k]/3 slow_d1[k] = slow_d1[k-1]*2/3+slow_k1[k]/3 indicators= { 'rsv':fast_k, 'max':rsv['max'], 'min':rsv['min'], 'k': slow_k1, 'd': slow_d1, 'j': 3 * slow_k1 - 2 * slow_d1 } return indicators df=get_price(frequency='daily',fields=['close','high','low'], security='600036.XSHG', skip_paused=False, fq='pre',start_date='2018-1-16',end_date='2018-12-12') data = df.to_dict('records') bbb=self_KDJ(data) df['rsv']=bbb['rsv'] df['max']=bbb['max'] df['min']=bbb['min'] 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函数会把图形简单的画出来参考,不需要的可以去掉
date close high low rsv max min k d j 2018-01-16 30.88 31.26 30.55 46.478873 31.26 30.55 50.000000 50.000000 50.000000 2018-01-17 30.69 32.07 30.51 11.538462 32.07 30.51 37.179487 45.726496 20.085470 2018-01-18 31.30 31.72 31.09 50.641026 32.07 30.51 41.666667 44.373219 36.253561 2018-01-19 31.44 32.30 31.19 51.955307 32.30 30.51 45.096214 44.614217 46.060206 2018-01-22 32.04 32.58 31.14 73.913043 32.58 30.51 54.701824 47.976753 68.151965 2018-01-23 32.98 33.09 32.15 95.736434 33.09 30.51 68.380027 54.777844 95.584393 2018-01-24 32.78 34.24 32.40 60.857909 34.24 30.51 65.872654 58.476114 80.665734 2018-01-25 32.36 32.95 31.86 49.597855 34.24 30.51 60.447721 59.133317 63.076531 2018-01-26 32.41 33.22 32.14 50.938338 34.24 30.51 57.277927 58.514853 54.804074 2018-01-29 31.94 33.01 31.78 38.337802 34.24 30.51 50.964552 55.998086 40.897483 2018-01-30 31.54 32.11 31.39 14.285714 34.24 31.09 38.738273 50.244815 15.725188 2018-01-31 32.87 32.92 31.20 55.806452 34.24 31.14 44.427666 48.305765 36.671466 2018-02-01 33.36 33.82 32.78 71.612903 34.24 31.14 53.489411 50.033647 60.400940 2018-02-02 33.41 33.49 32.63 72.697368 34.24 31.20 59.892064 53.319786 73.036619 2018-02-05 33.79 34.00 32.73 85.197368 34.24 31.20 68.327165 58.322246 88.337004 2018-02-06 33.53 33.60 32.43 83.214286 34.00 31.20 73.289539 63.311343 93.245929 2018-02-07 31.37 33.83 31.09 9.621993 34.00 31.09 52.067024 59.563237 37.074597 2018-02-08 30.29 31.36 29.86 10.386473 34.00 29.86 38.173507 52.433327 9.653867 2018-02-09 29.74 29.84 27.70 32.380952 34.00 27.70 36.242655 47.036436 14.655093 2018-02-12 29.10 29.67 28.47 22.222222 34.00 27.70 31.569178 41.880683 10.946166 2018-02-13 29.94 30.28 29.39 35.555556 34.00 27.70 32.897970 38.886446 20.921019 2018-02-14 30.38 30.48 29.46 42.539683 34.00 27.70 36.111874 37.961589 32.412446 2018-02-22 30.51 31.18 30.20 44.603175 34.00 27.70 38.942308 38.288495 40.249933 2018-02-23 30.78 31.09 30.24 50.244698 33.83 27.70 42.709771 39.762254 48.604806 2018-02-26 30.52 31.33 30.25 46.003263 33.83 27.70 43.807602 41.110703 49.201399 2018-02-27 29.90 30.65 29.68 60.109290 31.36 27.70 49.241498 43.820968 60.082557 2018-02-28 29.34 29.78 29.06 45.179063 31.33 27.70 47.887353 45.176430 53.309200 2018-03-01 29.44 29.89 29.06 33.916084 31.33 28.47 43.230263 44.527707 40.635375 2018-03-02 29.08 29.50 29.07 0.881057 31.33 29.06 29.113861 39.389759 8.562066 2018-03-05 29.20 29.62 28.98 9.361702 31.33 28.98 22.529808 33.769775 0.049874 ... ... ... ... ... ... ... ... ... ... 2018-11-01 29.05 29.65 28.66 52.243590 30.54 27.42 46.334047 46.887935 45.226271 2018-11-02 30.33 30.45 29.41 96.039604 30.45 27.42 62.902566 52.226145 84.255408 2018-11-05 30.00 30.23 29.71 85.148515 30.45 27.42 70.317882 58.256724 94.440198 2018-11-06 29.87 30.11 29.36 80.858086 30.45 27.42 73.831283 63.448244 94.597362 2018-11-07 29.57 30.20 29.53 70.957096 30.45 27.42 72.873221 66.589903 85.439857 2018-11-08 30.01 30.37 29.80 85.478548 30.45 27.42 77.074997 70.084934 91.055121 2018-11-09 28.60 29.60 28.49 35.540070 30.45 27.58 63.230021 67.799963 54.090137 2018-11-12 28.56 28.70 28.23 14.864865 30.45 28.23 47.108302 60.902743 19.519421 2018-11-13 28.58 28.77 28.05 22.083333 30.45 28.05 38.766646 53.524044 9.251850 2018-11-14 28.16 28.65 28.00 6.530612 30.45 28.00 28.021301 45.023130 -5.982355 2018-11-15 28.38 28.47 28.08 16.033755 30.37 28.00 24.025453 38.023904 -3.971450 2018-11-16 28.48 28.68 28.19 20.253165 30.37 28.00 22.768023 32.938610 2.426849 2018-11-19 28.95 29.04 28.45 40.084388 30.37 28.00 28.540145 31.472455 22.675524 2018-11-20 28.42 28.84 28.31 17.721519 30.37 28.00 24.933936 29.292949 16.215911 2018-11-21 28.40 28.43 28.10 25.000000 29.60 28.00 24.955958 27.847285 19.173302 2018-11-22 28.30 28.45 28.10 28.846154 29.04 28.00 26.252690 27.315753 24.126562 2018-11-23 27.88 28.35 27.88 0.000000 29.04 27.88 17.501793 24.044433 4.416513 2018-11-26 28.27 28.35 27.91 33.620690 29.04 27.88 22.874759 23.654542 21.315192 2018-11-27 27.99 28.38 27.85 11.764706 29.04 27.85 19.171408 22.160164 13.193896 2018-11-28 28.28 28.35 28.05 36.134454 29.04 27.85 24.825756 23.048695 28.379880 2018-11-29 28.30 28.60 28.30 37.815126 29.04 27.85 29.155546 25.084312 37.298015 2018-11-30 28.55 28.58 28.21 70.707071 28.84 27.85 43.006054 31.058226 66.901711 2018-12-03 29.46 29.63 28.93 90.449438 29.63 27.85 58.820516 40.312323 95.836902 2018-12-04 29.57 29.63 29.31 96.629213 29.63 27.85 71.423415 50.682687 112.904871 2018-12-05 29.37 29.57 29.00 85.393258 29.63 27.85 76.080029 59.148468 109.943153 2018-12-06 28.90 29.15 28.78 58.988764 29.63 27.85 70.382941 62.893292 85.362239 2018-12-07 28.58 29.19 28.53 41.011236 29.63 27.85 60.592373 62.126319 57.524480 2018-12-10 28.34 28.50 28.22 18.354430 29.63 28.05 46.513059 56.921899 25.695378 2018-12-11 28.51 28.55 28.28 21.126761 29.63 28.21 38.050959 50.631586 12.889706 2018-12-12 28.46 29.00 28.41 17.605634 29.63 28.21 31.235851 44.166341 5.374871
验证
2018-12-12日 招商银行600036 KDJ数值为31.235851、44.166341、5.374871,有兴趣的可以打开自己的股票软件对比一下。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 技术指标KD
- 如何衡量研发效能?阿里资深技术专家提出了5组指标
- 指标体系:四个模型教会你指标体系构建的方法
- 每秒聚合5亿个指标,Uber 开源大规模指标平台 M3
- 有赞指标库实践
- 网站性能指标 - FMP
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
HTTP Developer's Handbook
Chris Shiflett / Sams Publishing / 2003-3-29 / USD 39.99
The largest group with an unsatisfied demand for a good book on HTTP is the worldwide group of Web developers. A good book on HTTP can help new and old Web developers alike, as a thorough understandin......一起来看看 《HTTP Developer's Handbook》 这本书的介绍吧!