[关闭]
@Channelchan 2018-09-29T23:09:23.000000Z 字数 2547 阅读 78251

Talib

Talib是python用于金融技术分析的包。

TA_Lib提供什么样的函数?

重叠研究(Overlap Studies)

动能指标(Momentum Indicators)

周期指标(Cycle Indicators)

成交量指标(Volume Indicators)

波动率指标(Volatility Indicators)

数学操作(Math Operators)

数学变换(Math Transform)

统计功能(Statistic Functions)

价格转换(Price Transform)

形态识别(Pattern Recognition)

1、 TA_Lib需要什么样的数据格式?

'numpy.ndarray'

dataframe

  1. import talib as ta
  2. import pandas as pd
  3. import warnings
  4. warnings.filterwarnings('ignore')
  5. data = pd.read_excel('sz50.xlsx', sheetname='600036.XSHG', index_col='datetime')
  6. #读取'numpy.ndarray'
  7. print(ta.MA(data.close.values, 5)[-5:])
[109.272 109.902 110.724 112.568 114.85 ]
  1. # data
  1. print(type(data.close))
<class 'pandas.core.series.Series'>
  1. #直接读取DataFrame,默认读取cloumns名为close的数据。
  2. print(ta.abstract.MA(data, 5).tail())
datetime
2017-11-14 15:00:00    118.426
2017-11-15 15:00:00    118.462
2017-11-16 15:00:00    118.162
2017-11-17 15:00:00    118.442
2017-11-20 15:00:00    118.536
dtype: float64

2、 Band

多个返回值

  1. upperBand , middleBand, lowerBand = ta.BBANDS(data.close.values)
  1. print(upperBand[-5:], '\n', middleBand[-5:],'\n', lowerBand[-5:])
[120.07386407 120.07328024 120.60352084 121.2296413  121.54995687] 
 [118.426 118.462 118.162 118.442 118.536] 
 [116.77813593 116.85071976 115.72047916 115.6543587  115.52204313]
  1. fig = plt.figure(figsize=(15, 7))
  2. plt.plot(upperBand)
  3. plt.plot(middleBand)
  4. plt.plot(lowerBand)
  5. plt.show()

output_9_0.png-59.3kB

3、 Momentum

  1. stocks = pd.Panel({s: pd.read_excel('sz50.xlsx', sheetname=s, index_col='datetime')
  2. for s in ['600036.XSHG', '600050.XSHG', '600000.XSHG']})
  1. ROCR100 = pd.DataFrame({item: ta.ROCR100(value.values) for item, value in stocks.minor_xs('close').iteritems()},
  2. index = stocks.minor_xs('close').index)
  1. import matplotlib.pyplot as plt
  2. fig = plt.figure(figsize=(15, 7))
  3. plt.hlines(100,ROCR100.index[0],ROCR100.index[-1] , linestyles='dashed', alpha=0.5)
  4. plt.plot(ROCR100)
  5. plt.show()

output_13_0.png-63.8kB

4、 Volume

  1. import numpy as np
  2. volume = pd.Series(np.array(data.volume.values, dtype='float64'), index=data.index)
  3. volumeSMA = pd.Series(ta.MA(volume.values,10), index=data.index)
  4. volumeLMA = pd.Series(ta.MA(volume.values,20), index=data.index)
  5. fig, (ax, ax1) = plt.subplots(2, 1, sharex=True, figsize=(15,7))
  6. ax.plot(data.close)
  7. ax1.bar(volume.index, volume.values,color='g')
  8. ax1.plot(volumeSMA)
  9. ax1.plot(volumeLMA)
  10. plt.show()

output_15_0.png-38.7kB

5、 Regression

  1. Reg = ta.abstract.LINEARREG(data, 20)
  2. slope = ta.abstract.LINEARREG_SLOPE(data, 20)
  3. fig, (ax, ax1) = plt.subplots(2, 1, sharex=True, figsize=(15,7))
  4. ax.plot(data.close)
  5. ax.plot(Reg)
  6. ax1.plot(slope)
  7. plt.hlines(0,slope.index[0],slope.index[-1] , linestyles='dashed', alpha=0.5)
  8. plt.show()

output_17_0.png-42.8kB

6、 Volatility

  1. ta.ATR?
  1. atr = ta.ATR(data.high.values, data.low.values, data.close.values, 10)
  1. fig, (ax, ax1) = plt.subplots(2, 1, sharex=True, figsize=(15,7))
  2. ax.plot(data.close)
  3. ax1.plot(pd.Series(atr, index=data.index))
  4. plt.show()

output_21_0.png-35.4kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注