91
95

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TA-Libでテクニカル分析

Last updated at Posted at 2018-02-01

TA-Libでテクニカル分析

簡単にテクニカル分析ができるライブラリTA-Libを試しました.TA-LibにはPythonのラッパーがあり,かなり簡単にですが紹介します.

準備

はじめに,TA-Libをインストールする必要があります.Macの場合はHomebrewでインストールできます.

$ brew install ta-lib

Pythonで使えるようにします.

$ pip install TA-Lib

テクニカル分析

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

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

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

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

TA-LibはDataFrameを引数にできないので,numpyのndarray形式にします.

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

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

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']

引数のtimeperiodを指定して,いろんな期間を計算できます.

DataFrame形式にして,日付と対応させます.

data = pd.DataFrame(output, index=df.index, columns=cols)
data.tail()
Original SMA WMA EMA DEMA TEMA TMA KAMA MAMA FAMA ... HT_PHASOR_inphase HT_PHASOR_quadrature HT_SINE_sine HT_SINE_leadsine HT_TRENDMODE SMA60 BBANDS15_upperband BBANDS15_middleband BBANDS15_lowerband RSI21
Date
2018-01-26 23631.88 23336.888333 23577.879333 23396.084808 23950.015680 23881.542216 23358.915125 23401.776887 23774.262642 23579.472022 ... 21.325892 -43.817842 -0.589713 -0.988059 1.0 22920.246667 24056.610766 23800.404000 23544.197234 58.287358
2018-01-29 23629.34 23362.327333 23596.747183 23411.133530 23943.404763 23819.197807 23416.499625 23410.021533 23701.801321 23610.054347 ... 9.146663 81.389471 -0.713710 -0.999956 1.0 22947.261500 24061.838563 23794.724667 23527.610770 58.232275
2018-01-30 23291.97 23380.124000 23592.208000 23403.445560 23894.184742 23717.474237 23468.224208 23407.458663 23681.309755 23611.835732 ... 139.081959 117.191239 -0.837169 -0.978716 1.0 22968.599833 24121.395394 23757.523333 23393.651273 51.451184
2018-01-31 23098.29 23393.585333 23574.025161 23383.758105 23824.419397 23575.717839 23512.524500 23402.617512 23652.158767 23612.843808 ... 165.813706 -406.110394 -0.942737 -0.902462 0.0 22986.711167 24200.998868 23711.529333 23222.059798 48.076476
2018-02-01 23486.11 23424.681667 23579.994495 23390.361453 23808.770310 23470.100518 23551.332833 23405.839309 23569.134384 23601.916452 ... -122.751421 -700.414139 -0.978461 -0.837845 0.0 23004.478333 24198.805592 23696.574667 23194.343742 54.369135

5 rows × 36 columns

とりあえず,CSVに書き出します.

data.to_csv('NIKKEI_ta.csv')

TA-Libは予測モデルの特徴量の作成にも便利です.

91
95
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
91
95

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?