这篇文章简单阐述下Talib在股票中的使用介绍,具体使用例子见(https://uqer.io/community/share/57a45428228e5b9b97a87aab)
1、TaLib与技术分析技术分析是股票分析十分有效直接的手段,在实际投资中我们常常需要计算各种简单或复杂的技术指标来分析参考
对于技术指标的定义基本都大同小异,很多都是通用的且模块化的东西
对于不会写代码或者不想自己写函数计算这些技术指标的人而言,强大的Talib给我们带来了福音
我们只需要调用Talib的函数,输入简单的参数就可以得到自己想要的结
2、初看Talib
先简单看看Talib都给我们提供了那些计算技术指标的函数,按技术指标的类型列示如下:Overlap Studies Functions
BBANDS - Bollinger Bandsupperband, middleband, lowerband = BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)
DEMA - Double Exponential Moving Averagereal = DEMA(close, timeperiod=30)
EMA - Exponential Moving Averagereal = EMA(close, timeperiod=30)
HT_TRENDLINE - Hilbert Transform - Instantaneous Trendlinereal = HT_TRENDLINE(close)
KAMA - Kaufman Adaptive Moving Averagereal = KAMA(close, timeperiod=30)
................
................
................
MA - Moving averagereal = MA(close, timeperiod=30, matype=0)
TEMA - Triple Exponential Moving Averagereal = TEMA(close, timeperiod=30)
Momentum Indicator Functions
ADX - Average Directional Movement Indexreal = ADX(high, low, close, timeperiod=14)
ADXR - Average Directional Movement Index Ratingreal = ADXR(high, low, close, timeperiod=14)
APO - Absolute Price Oscillatorreal = APO(close, fastperiod=12, slowperiod=26, matype=0)
AROON - Aroonaroondown, aroonup = AROON(high, low, timeperiod=14)
.............
.............
.............
RSI - Relative Strength Indexreal = RSI(close, timeperiod=14)
Volume Indicator Functions
AD - Chaikin A/D Linereal = AD(high, low, close, volume)
ADOSC - Chaikin A/D Oscillatorreal = ADOSC(high, low, close, volume, fastperiod=3, slowperiod=10)
OBV - On Balance Volumereal = OBV(close, volume)
Volatility Indicator Functions
ATR - Average True Rangereal = ATR(high, low, close, timeperiod=14)
NATR - Normalized Average True Rangereal = NATR(high, low, close, timeperiod=14)
TRANGE - True Rangereal = TRANGE(high, low, close)
如上只是列举了一部分,更多函数可以参见官网,或在原文中参看(https://uqer.io/community/share/57a45428228e5b9b97a87aab)
3、如何应用:MA实例
从上面可以看到,MA这个函数的参数为:real = MA(close, timeperiod=30, matype=0)
close表示收盘价序列,timeperiod指定义好均线的计算长度即几日均线,不输入的话,默认为30日,matype可以默认不用输入,然后就可以得到均线的值
所以简单来讲,只取获取收盘价序列,就可以轻松计算MA值
下面以近期很火的万科A为例进行说明data['MA5'] = talib.MA(data['closePrice'].values, timeperiod=5) #调用talib计算5日均线的值就这样,我们便捷地计算出了均线,下面不妨计算更复杂的EMA,MACD
EMA,MACD
有关EMA,MACD的基础知识,可以参考社区大神的帖子『研究|技术指标|第一弹』MACD
关于EMA,MACD计算的函数的描述是:real = EMA(close, timeperiod=30)macd, macdsignal, macdhist = MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)
输入参数:close是收盘价,timeperiod指的是指数移动平均线EMA的长度,fastperiod指更短时段的EMA的长度,slowperiod指更长时段的EMA的长度,signalperiod指DEA长度
返回值:注意有些地方的macdhist = 2(dif-dea),但是talib中MACD的计算是macdhist = dif-dea
data['EMA12'] = talib.EMA(data['closePrice'].values, timeperiod=12) #调用talib计算12日移动移动平均线的值
data['EMA26'] = talib.EMA(data['closePrice'].values, timeperiod=26)
data['MACD'],data['MACDsignal'],data['MACDhist'] = talib.MACD(data['closePrice'].values)