# Python进阶量化交易专栏场外篇19-建立基于TA-Lib的指标库

2020.01.26 21:23 1561浏览

TA-Lib库的强大我们已经有目共睹了，对此可以基于TA-Lib库建立起基础的指标库。首先创建一个py文件“IndicatorsLib.py”，文件中创建QtYx_Talib_Indictors类，用于集合所有TA-Lib实现的技术指标。

TA-Lib库的函数分为10个功能组，如Overlap Studies(重叠研究)、Momentum Indicators(动量指标)、Volume Indicators(交易量指标)、Cycle Indicators(周期指标)、Price Transform(价格变换)、Volatility Indicators(波动率指标)、Pattern Recognition(模式识别)、Statistic Functions(统计函数)、Math Transform(数学变换)、Math Operators(数学运算)，此处我们有代表性地选取几个指标接口去实现。

• ADX - Average Directional Movement Index
• ADXR - Average Directional Movement Index Rating
• CMO - Chande Momentum Oscillator
• MACD - Moving Average Convergence/Divergence
• STOCH - Stochastic
• ……

``````class QtYx_Talib_Indictors():

# Momentum Indicator Functions
@staticmethod
# ADX - Average Directional Movement Index
res = talib.ADX(DataFrame.high.values, DataFrame.low.values, DataFrame.close.values, N)

@staticmethod
# ADXR - Average Directional Movement Index Rating
res = talib.ADXR(DataFrame.high.values, DataFrame.low.values, DataFrame.close.values, N)

@staticmethod
def CMO(Series, timeperiod=14):
# CMO - Chande Momentum Oscillator
res = talib.CMO(Series.values, timeperiod)
return pd.Series(res, index=Series.index)

@staticmethod
def MACD_SER(Series, fastperiod=12, fastmatype=0, slowperiod=26, slowmatype=0, signalperiod=9, signalmatype=0):
# MACD - Moving Average Convergence/Divergence
macd_dif, macd_dea, macd_bar = talib.MACD(
Series.values, fastperiod, slowperiod, signalperiod)
return pd.Series(macd_dif, index=Series.index), pd.Series(macd_dea, index=Series.index), pd.Series(macd_bar, index=Series.index)

@staticmethod
def STOCH_DF(DataFrame, fastkperiod=9, slowkperiod=3, slowkmatype=0, slowdperiod=3, slowdmatype=0):
# STOCH - Stochastic
K, D = talib.STOCH(DataFrame.high.values, DataFrame.low.values, DataFrame.close.values, \
fastkperiod, slowkperiod, slowkmatype, slowdperiod, slowdmatype)
J = 3 * K - 2 * D
return pd.Series(K, index=DataFrame.index), pd.Series(D, index=DataFrame.index), pd.Series(J, index=DataFrame.index)

``````

``````indictors_dict = {"MA":QtYx_Base_Indictors.MA_DF,
"BBANDS":QtYx_Talib_Indictors.BBANDS_SER,
"MACD": QtYx_Talib_Indictors.MACD_SER,
"STOCH": QtYx_Talib_Indictors.STOCH_DF
}
``````

``````print(indictors_dict["BBANDS"](df_stockDat.close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0))
"""
2018-01-02          NaN
2018-01-03          NaN
2018-01-04          NaN
2018-01-05          NaN
2018-01-08    13.780169
2018-01-09    13.467012
2018-01-10    13.566040
2018-01-11    13.627829
2018-01-12    13.752504
2018-01-15    14.273103
2018-01-16    14.482287
2018-01-17    14.642702
2018-01-18    14.923935
2018-01-19    14.971701
"""
print(indictors_dict["MA"](df_stockDat, 120, 60, 20))
"""
MA120       MA60     MA20
2018-01-02       NaN        NaN      NaN
2018-01-29       NaN        NaN  13.8955
2018-01-30       NaN        NaN  13.8930
2018-01-31       NaN        NaN  13.9290
2018-02-01       NaN        NaN  13.9680
2018-02-02       NaN        NaN  14.0055
2018-02-05       NaN        NaN  14.0850
...              ...        ...      ...
2018-11-22  9.886917  10.454833  10.7490
2018-11-23  9.888917  10.470833  10.7060
2018-11-26  9.890250  10.476000  10.6855
"""

``````

``````def GetStockDatPro(stockName=None,stockTimeS=None,stockTimeE=None):

stockPro = GetStockDatApi(stockName, stockTimeS, stockTimeE)

# 处理移动平均线
stockDat = pd.DataFrame({'High': stockPro.high, 'Low': stockPro.low, 'Open': stockPro.open, 'Close': stockPro.close,\
'Volume': stockPro.vol})

stockDat = pd.concat([stockDat, indictors_dict['MA'](stockPro, 20, 60, 120)], axis=1)

# 处理MACD
stockDat['macd_dif'],stockDat['macd_dea'], stockDat['macd_bar'] = indictors_dict['MACD'](stockPro.close)

# 处理KDJ
stockDat['K'], stockDat['D'], stockDat['J'] = indictors_dict['STOCH'](stockPro)

return stockDat

``````

``````# 基础指标
Indicators_Text = wx.StaticText(self.ParaPanel, -1, u'基础指标')
Indicators_Combo_Val = list(indictors_dict.keys())
self.indicators_Val = "BBANDS"
self.indicators_CMBO = wx.ComboBox(self.ParaPanel, -1, self.indicators_Val, choices = Indicators_Combo_Val, style = wx.CB_READONLY|wx.CB_DROPDOWN) # 指标名称
``````

1人点赞

• 1
• 1
• 收藏
• 共同学习，写下你的评论

0/150