2
5

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 3 years have passed since last update.

【Python】TA-Libでテクニカル指標計算 Part 4. MACDの描画

Last updated at Posted at 2021-04-10

今回は,テクニカル指標である MACD(Moving Average Convergence Divergence)をPythonライブラリTA-Libで計算し,描画する方法を紹介します.

【Python】TA-Libでテクニカル指標計算 Part 1. RSIの描画
【Python】TA-Libでテクニカル指標計算 Part 2. ボリンジャーバンドの描画
・[【Python】TA-Libでテクニカル指標計算 Part 3. SMA(単純移動平均線)の描画]
(https://qiita.com/Fujinoinvestor/items/49dc284279c7e9217c30)
【Python】TA-Libでテクニカル指標計算 Part 4. MACDの描画

#TA-Libのインストール

以前の記事

をご覧ください.

#TA-LibによるMACD描画

##各ライブラリインポート

import datetime
import numpy as np
import matplotlib.pyplot as plt
import pandas_datareader.data as web
import talib as ta

##株価データ取得
今回は,2020/5/1~2021/3/1におけるS&P500のMACDを取得してみたいと思います.

株価は,pandas_datareaderで取得します.もちろんウェブやローカルにあるCSVファイルを読み込むなどでも構いません.

start = datetime.date(2020,5,1)
end = datetime.date(2021,3,1)
df = web.DataReader('^GSPC', 'yahoo', start, end) #get data

pandas-datareaderで取得したデータは以下のように,High(高値),Low(低値),Open(始値),Close(終値),Volume(出来高),Adj Close(調整後終値)を持つDataFarameとなっています.

                   High          Low         Open        Close      Volume    Adj Close
Date
2020-04-30  2930.909912  2892.469971  2930.909912  2912.429932  6523120000  2912.429932
2020-05-01  2869.090088  2821.610107  2869.090088  2830.709961  4753160000  2830.709961
2020-05-04  2844.239990  2797.850098  2815.010010  2842.739990  4723140000  2842.739990
2020-05-05  2898.229980  2863.550049  2868.879883  2868.439941  5129590000  2868.439941
2020-05-06  2891.110107  2847.649902  2883.139893  2848.419922  4861920000  2848.419922
...                 ...          ...          ...          ...         ...          ...
2021-02-23  3895.979980  3805.590088  3857.070068  3881.370117  6280650000  3881.370117
2021-02-24  3928.649902  3859.600098  3873.709961  3925.429932  5942350000  3925.429932
2021-02-25  3925.020020  3814.040039  3915.800049  3829.340088  6513060000  3829.340088
2021-02-26  3861.080078  3789.540039  3839.659912  3811.149902  6512950000  3811.149902
2021-03-01  3914.500000  3842.510010  3842.510010  3901.820068  5071540000  3901.820068

##MACDに必要なデータ
pandas-datareaderで取得したデータのうち,MACDを計算するために必要なデータは,終値(Close)だけです.dfの’Close’の列だけをcloseとして取り出します(他のカラムでも計算自体はできます).

close = df['Close']

##TA-LibでMACDを計算

#MACD(Moving Average Convergence Divergence)
macd, macdsignal, macdhist = ta.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)

##描画

plt.plot(macd,label='macd')
plt.plot(macdsignal,label='macdsignal')
plt.bar(macdhist.index,macdhist ,label='macdhist')
plt.xlabel('date')
plt.ylabel('indicator value')
plt.legend()
plt.show()

image.png

#まとめコード

import datetime
import numpy as np
import matplotlib.pyplot as plt
import pandas_datareader.data as web
import talib as ta

start = datetime.date(2020,5,1)
end = datetime.date(2021,3,1)

df = web.DataReader('^GSPC', 'yahoo', start, end) #get data

close = df['Close']

#MACD - Moving Average Convergence/Divergence
macd, macdsignal, macdhist = ta.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)

plt.plot(macd,label='macd')
plt.plot(macdsignal,label='macd signal')
plt.bar(macdhist.index,macdhist ,label='macd hist')
plt.xlabel('date')
plt.ylabel('indicator value')
plt.legend()
plt.show()

もう少しだけ丁寧な解説を以下の記事で行っています.

2
5
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
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?