TA-Lib的技术分析
我尝试了TA-Lib,该库可轻松进行技术分析。 TA-Lib有一个Python包装器,很容易引入。
准备
首先,您需要安装TA-Lib。对于Mac,您可以使用Homebrew安装它。
1 | $ brew install ta-lib |
使它在Python中可用。
1 | $ pip install TA-Lib |
技术分析
导入这次使用的库。
1 2 3 4 | import numpy as np import pandas as pd import talib import quandl |
这一次,我们将使用quandl获取数据。
1 | df = quandl.get('NIKKEI/INDEX') # quandlの場合 |
TA-Lib不能将DataFrame用作参数,因此请使用numpy的ndarray格式。
1 2 | close = np.array(df['Close Price']) close[:5] |
无论如何,我将尝试各种技术指标。 (这一次,仅使用收盘价,而不使用使用烛台的指数。)
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | output = close.copy() cols = ['Original'] # 単純移動平均(SMA: Simple Moving Average) output = np.c_[output, talib.SMA(close)] cols += ['SMA'] # 加重移動平均(WMA: Weighted Moving Average) output = np.c_[output, talib.WMA(close)] cols += ['WMA'] # 指数移動平均(EMA: Exponential Moving Average) output = np.c_[output, talib.EMA(close)] cols += ['EMA'] # 2重指数移動平均(DEMA: Double Exponential Moving Average) output = np.c_[output, talib.DEMA(close)] cols += ['DEMA'] # 3重指数移動平均(TEMA: Triple Exponential Moving Average) output = np.c_[output, talib.T3(close)] cols += ['TEMA'] # 三角移動平均(TMA: Triangular Moving Average) output = np.c_[output, talib.TRIMA(close)] cols += ['TMA'] # Kaufmanの適応型移動平均(KAMA: Kaufman Adaptive Moving Average) output = np.c_[output, talib.KAMA(close)] cols += ['KAMA'] # MESAの適応型移動平均(MAMA: MESA Adaptive Moving Average) for arr in talib.MAMA(close): output = np.c_[output, arr] cols += ['MAMA', 'FAMA'] # トレンドライン(Hilbert Transform - Instantaneous Trendline) output = np.c_[output, talib.HT_TRENDLINE(close)] cols += ['HT_TRENDLINE'] # ボリンジャー?バンド(Bollinger Bands) for arr in talib.BBANDS(close): output = np.c_[output, arr] cols += ['BBANDS_upperband', 'BBANDS_middleband', 'BBANDS_lowerband'] # MidPoint over period output = np.c_[output, talib.MIDPOINT(close)] cols += ['MIDPOINT'] # 変化率(ROC: Rate of change Percentage) output = np.c_[output, talib.ROCP(close)] cols += ['ROC'] # モメンタム(Momentum) output = np.c_[output, talib.MOM(close)] cols += ['MOM'] # RSI: Relative Strength Index output = np.c_[output, talib.RSI(close)] cols += ['RSI'] # MACD: Moving Average Convergence/Divergence for arr in talib.MACD(close): output = np.c_[output, arr] cols += ['MACD', 'MACD_signal', 'MACD_hist'] # APO: Absolute Price Oscillator output = np.c_[output, talib.APO(close)] cols += ['APO'] # PPO: Percentage Price Oscillator output = np.c_[output, talib.PPO(close)] cols += ['PPO'] # CMO: Chande Momentum Oscillator output = np.c_[output, talib.CMO(close)] cols += ['CMO'] # ヒルベルト変換 - Dominant Cycle Period output = np.c_[output, talib.HT_DCPERIOD(close)] cols += ['HT_DCPERIOD'] # ヒルベルト変換 - Dominant Cycle Phase output = np.c_[output, talib.HT_DCPHASE(close)] cols += ['HT_DCPHASE'] # ヒルベルト変換 - Phasor Components for arr in talib.HT_PHASOR(close): output = np.c_[output, arr] cols += ['HT_PHASOR_inphase', 'HT_PHASOR_quadrature'] # ヒルベルト変換 - SineWave for arr in talib.HT_SINE(close): output = np.c_[output, arr] cols += ['HT_SINE_sine', 'HT_SINE_leadsine'] # ヒルベルト変換 - Trend vs Cycle Mode output = np.c_[output, talib.HT_TRENDMODE(close)] cols += ['HT_TRENDMODE'] output.shape # 60日単純移動平均 output = np.c_[output, talib.SMA(close, timeperiod=60)] cols += ['SMA60'] # 15日ボリンジャー?バンド for arr in talib.BBANDS(close, timeperiod=15, nbdevup=2, nbdevdn=2, matype=0): output = np.c_[output, arr] cols += ['BBANDS15_upperband', 'BBANDS15_middleband', 'BBANDS15_lowerband'] # 21日RSI output = np.c_[output, talib.RSI(close, timeperiod=21)] cols += ['RSI21'] |
您可以通过指定
参数的时间段来计算各种时间段。
转换为DataFrame格式并与日期相对应。
1 2 | data = pd.DataFrame(output, index=df.index, columns=cols) data.tail() |
<表格>
tr>
tr>
header>
<身体>
tr>
tr>
tr>
tr>
tr>
tbody>
table>
5行x 36列
暂时将其写入CSV。
1 | data.to_csv('NIKKEI_ta.csv') |
TA-Lib也可用于创建预测模型的特征。