TA-Libでテクニカル分析

# TA-Libでテクニカル分析

簡単にテクニカル分析ができるライブラリ[TA-Lib](https://github.com/mrjbq7/ta-lib)を試しました．TA-LibにはPythonのラッパーがあり，かなり簡単にですが紹介します．

## 準備

はじめに，[TA-Lib](http://ta-lib.org/)をインストールする必要があります．Macの場合はHomebrewでインストールできます．

```
$ brew install ta-lib
```
Pythonで使えるようにします．

```
$ pip install TA-Lib
```

## テクニカル分析

今回使用するライブラリをインポートします．


```python
import numpy as np
import pandas as pd
import talib
import quandl
```

今回はquandlでデータを取得します．


```python
df = quandl.get('NIKKEI/INDEX')  # quandlの場合
```

[TA-Lib](https://github.com/mrjbq7/ta-lib)はDataFrameを引数にできないので，numpyのndarray形式にします．


```python
close = np.array(df['Close Price'])
close[:5]
```

とにかく，いろいろなテクニカル指標を試してみます．（今回は終値のみで，ローソク足を用いる指標は使っていません．）


```python
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']
# ２重指数移動平均(DEMA: Double Exponential Moving Average)
output = np.c_[output, talib.DEMA(close)]
cols += ['DEMA']
# ３重指数移動平均(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']
```

引数のtimeperiodを指定して，いろんな期間を計算できます．<br>

DataFrame形式にして，日付と対応させます．


```python
data = pd.DataFrame(output, index=df.index, columns=cols)
data.tail()
```

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Original</th>
      <th>SMA</th>
      <th>WMA</th>
      <th>EMA</th>
      <th>DEMA</th>
      <th>TEMA</th>
      <th>TMA</th>
      <th>KAMA</th>
      <th>MAMA</th>
      <th>FAMA</th>
      <th>...</th>
      <th>HT_PHASOR_inphase</th>
      <th>HT_PHASOR_quadrature</th>
      <th>HT_SINE_sine</th>
      <th>HT_SINE_leadsine</th>
      <th>HT_TRENDMODE</th>
      <th>SMA60</th>
      <th>BBANDS15_upperband</th>
      <th>BBANDS15_middleband</th>
      <th>BBANDS15_lowerband</th>
      <th>RSI21</th>
    </tr>
    <tr>
      <th>Date</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2018-01-26</th>
      <td>23631.88</td>
      <td>23336.888333</td>
      <td>23577.879333</td>
      <td>23396.084808</td>
      <td>23950.015680</td>
      <td>23881.542216</td>
      <td>23358.915125</td>
      <td>23401.776887</td>
      <td>23774.262642</td>
      <td>23579.472022</td>
      <td>...</td>
      <td>21.325892</td>
      <td>-43.817842</td>
      <td>-0.589713</td>
      <td>-0.988059</td>
      <td>1.0</td>
      <td>22920.246667</td>
      <td>24056.610766</td>
      <td>23800.404000</td>
      <td>23544.197234</td>
      <td>58.287358</td>
    </tr>
    <tr>
      <th>2018-01-29</th>
      <td>23629.34</td>
      <td>23362.327333</td>
      <td>23596.747183</td>
      <td>23411.133530</td>
      <td>23943.404763</td>
      <td>23819.197807</td>
      <td>23416.499625</td>
      <td>23410.021533</td>
      <td>23701.801321</td>
      <td>23610.054347</td>
      <td>...</td>
      <td>9.146663</td>
      <td>81.389471</td>
      <td>-0.713710</td>
      <td>-0.999956</td>
      <td>1.0</td>
      <td>22947.261500</td>
      <td>24061.838563</td>
      <td>23794.724667</td>
      <td>23527.610770</td>
      <td>58.232275</td>
    </tr>
    <tr>
      <th>2018-01-30</th>
      <td>23291.97</td>
      <td>23380.124000</td>
      <td>23592.208000</td>
      <td>23403.445560</td>
      <td>23894.184742</td>
      <td>23717.474237</td>
      <td>23468.224208</td>
      <td>23407.458663</td>
      <td>23681.309755</td>
      <td>23611.835732</td>
      <td>...</td>
      <td>139.081959</td>
      <td>117.191239</td>
      <td>-0.837169</td>
      <td>-0.978716</td>
      <td>1.0</td>
      <td>22968.599833</td>
      <td>24121.395394</td>
      <td>23757.523333</td>
      <td>23393.651273</td>
      <td>51.451184</td>
    </tr>
    <tr>
      <th>2018-01-31</th>
      <td>23098.29</td>
      <td>23393.585333</td>
      <td>23574.025161</td>
      <td>23383.758105</td>
      <td>23824.419397</td>
      <td>23575.717839</td>
      <td>23512.524500</td>
      <td>23402.617512</td>
      <td>23652.158767</td>
      <td>23612.843808</td>
      <td>...</td>
      <td>165.813706</td>
      <td>-406.110394</td>
      <td>-0.942737</td>
      <td>-0.902462</td>
      <td>0.0</td>
      <td>22986.711167</td>
      <td>24200.998868</td>
      <td>23711.529333</td>
      <td>23222.059798</td>
      <td>48.076476</td>
    </tr>
    <tr>
      <th>2018-02-01</th>
      <td>23486.11</td>
      <td>23424.681667</td>
      <td>23579.994495</td>
      <td>23390.361453</td>
      <td>23808.770310</td>
      <td>23470.100518</td>
      <td>23551.332833</td>
      <td>23405.839309</td>
      <td>23569.134384</td>
      <td>23601.916452</td>
      <td>...</td>
      <td>-122.751421</td>
      <td>-700.414139</td>
      <td>-0.978461</td>
      <td>-0.837845</td>
      <td>0.0</td>
      <td>23004.478333</td>
      <td>24198.805592</td>
      <td>23696.574667</td>
      <td>23194.343742</td>
      <td>54.369135</td>
    </tr>
  </tbody>
</table>
<p>5 rows × 36 columns</p>
</div>

とりあえず，CSVに書き出します．


```python
data.to_csv('NIKKEI_ta.csv')
```

[TA-Lib](https://github.com/mrjbq7/ta-lib)は予測モデルの特徴量の作成にも便利です．