python TA-Lib テクニカル分析

超簡単Pythonで株価テクニカル分析チャート作成(TA-Lib・matplotlib・pandas)移動平均・ボリンジャーバンド・出来高・MACD・RSI

September 21, 2019

#テクニカル分析 #株価 #MACD #ボリンジャーバンド #RSI #出来高 #TA-Lib #移動平均 #python

PythonでTA-Lib・matplotlib・pandasを使用して株価テクニカル分析チャートを超簡単に作成(移動平均・ボリンジャーバンド・出来高・MACD・RSI)

*株価ローソク足チャート作成については超簡単Pythonで株価ローソク足チャート作成(matplotlib・pandas)

1. TA-Libインストール(linux)

macやwindowsの場合はドキュメント参照

 $ curl -L http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz -O
 $ tar -xzf ta-lib-0.4.0-src.tar.gz
 $ cd ta-lib/
 $ ./configure --prefix=/usr
 $ make
 $ sudo make install

2. ツールインストール

 $ pip install TA-Lib mpl_finance matplotlib pandas-datareader

3. 移動平均チャート作成

ma5.py

 import pandas_datareader as pdr
 import matplotlib.dates as mdates
 import matplotlib.pyplot as plt
 import talib as ta
 from mpl_finance import candlestick_ohlc
 
 df = pdr.get_data_yahoo('AAPL', '2019-08-09', '2019-09-19')
 df['ma5'] = ta.SMA(df['Close'], timeperiod=5)
 df = df[5:]
 ax = plt.subplot()
 ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d'))
 candlestick_ohlc(ax, zip(mdates.date2num(df.index), df['Open'], df['High'], df['Low'], df['Close']), width=0.4)
 ax.plot(mdates.date2num(df.index), df['ma5'])
 plt.title('apple')
 plt.savefig('ma5.png')

チャート作成

 $ python ma5.py

4. ボリンジャーバンドチャート作成

bbands.py

 import pandas_datareader as pdr
 import matplotlib.dates as mdates
 import matplotlib.pyplot as plt
 import talib as ta
 from mpl_finance import candlestick_ohlc
 
 df = pdr.get_data_yahoo('AAPL', '2019-08-09', '2019-09-19')
 df['upper'], df['middle'], df['lower'] = ta.BBANDS(df['Close'], timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)
 df = df[5:]
 ax = plt.subplot()
 ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d'))
 candlestick_ohlc(ax, zip(mdates.date2num(df.index), df['Open'], df['High'], df['Low'], df['Close']), width=0.4)
 ax.plot(mdates.date2num(df.index), df['upper'])
 ax.plot(mdates.date2num(df.index), df['middle'])
 ax.plot(mdates.date2num(df.index), df['lower'])
 plt.title('apple')
 plt.savefig('bbands.png')

チャート作成

 $ python bbands.py

5. 出来高チャート作成

volume.py

 import pandas_datareader as pdr
 import matplotlib.dates as mdates
 import matplotlib.pyplot as plt
 from mpl_finance import candlestick_ohlc
 
 df = pdr.get_data_yahoo('AAPL', '2019-08-16', '2019-09-19')
 fig = plt.figure()
 ax1 = fig.add_axes((0.1, 0.4, 0.8, 0.5))
 ax1.tick_params(labelbottom = False, bottom = False)
 ax1.set_title('apple')
 ax2 = fig.add_axes((0.1, 0.1, 0.8, 0.2), sharex=ax1)
 ax2.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d'))
 candlestick_ohlc(ax1, zip(mdates.date2num(df.index), df['Open'], df['High'], df['Low'], df['Close']), width=0.4)
 ax2.bar(mdates.date2num(df.index),df['Volume'])
 plt.savefig('volume.png')

チャート作成

 $ python volume.py

6. MACDチャート作成

macd.py

 import pandas_datareader as pdr
 import matplotlib.dates as mdates
 import matplotlib.pyplot as plt
 import talib as ta
 from mpl_finance import candlestick_ohlc
 
 df = pdr.get_data_yahoo('AAPL', '2019-06-29', '2019-09-19')
 df['macd'], df['macdsignal'], df['macdhist'] = ta.MACD(df['Close'], fastperiod=12, slowperiod=26, signalperiod=9)
 df = df[33:]
 fig = plt.figure()
 ax1 = fig.add_axes((0.1, 0.4, 0.8, 0.5))
 ax1.tick_params(labelbottom = False, bottom = False)
 ax1.set_title('apple')
 ax2 = fig.add_axes((0.1, 0.1, 0.8, 0.2), sharex=ax1)
 ax2.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d'))
 candlestick_ohlc(ax1, zip(mdates.date2num(df.index), df['Open'], df['High'], df['Low'], df['Close']), width=0.4)
 ax2.plot(mdates.date2num(df.index),df['macd'])
 ax2.plot(mdates.date2num(df.index),df['macdsignal'])
 plt.savefig('macd.png')

チャート作成

 $ python macd.py

7. RSIチャート作成

rsi.py

 import pandas_datareader as pdr
 import matplotlib.dates as mdates
 import matplotlib.pyplot as plt
 import talib as ta
 from mpl_finance import candlestick_ohlc
 
 df = pdr.get_data_yahoo('AAPL', '2019-06-29', '2019-09-19')
 df['rsi'] = ta.RSI(df['Close'], timeperiod=14)
 df = df[33:]
 fig = plt.figure()
 ax1 = fig.add_axes((0.1, 0.4, 0.8, 0.5))
 ax1.tick_params(labelbottom = False, bottom = False)
 ax1.set_title('apple')
 ax2 = fig.add_axes((0.1, 0.1, 0.8, 0.2), sharex=ax1)
 ax2.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d'))
 candlestick_ohlc(ax1, zip(mdates.date2num(df.index), df['Open'], df['High'], df['Low'], df['Close']), width=0.4)
 ax2.plot(mdates.date2num(df.index),df['rsi'])
 ax2.plot(mdates.date2num(df.index),[70]*25)
 ax2.plot(mdates.date2num(df.index),[30]*25)
 plt.savefig('rsi.png')

チャート作成

 $ python rsi.py

以上、超簡単!

8. 参考

TA-Lib