1
3

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 3. SMA(単純移動平均線)の描画

Last updated at Posted at 2021-04-09

今回は,テクニカル指標であるSMA(単純移動平均線)を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によるSMA描画

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

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

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

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

start = datetime.date(2018,1,1)
end = datetime.date(2020,1,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
2018-01-02  2695.889893  2682.360107  2683.729980  2695.810059  3367250000  2695.810059
2018-01-03  2714.370117  2697.770020  2697.850098  2713.060059  3538660000  2713.060059
2018-01-04  2729.290039  2719.070068  2719.310059  2723.989990  3695260000  2723.989990
2018-01-05  2743.449951  2727.919922  2731.330078  2743.149902  3236620000  2743.149902
2018-01-08  2748.510010  2737.600098  2742.669922  2747.709961  3242650000  2747.709961
...                 ...          ...          ...          ...         ...          ...
2019-12-24  3226.429932  3220.510010  3225.449951  3223.379883  1296540000  3223.379883
2019-12-26  3240.080078  3227.199951  3227.199951  3239.909912  2160680000  3239.909912
2019-12-27  3247.929932  3234.370117  3247.229980  3240.020020  2428670000  3240.020020
2019-12-30  3240.919922  3216.570068  3240.090088  3221.290039  3013290000  3221.290039
2019-12-31  3231.719971  3212.030029  3215.179932  3230.780029  2893810000  3230.780029

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

close = df['Close']

##TA-LibでSMAを計算

#Simple Moving Average
sma5 = ta.SMA(close, timeperiod=5)
sma25 = ta.SMA(close, timeperiod=25)
sma50 = ta.SMA(close, timeperiod=50)
sma75 = ta.SMA(close, timeperiod=75)
sma100 = ta.SMA(close, timeperiod=100)

##描画

plt.plot(close,label='price')
plt.plot(sma5,label='sma5')
plt.plot(sma25,label='sma25')
plt.plot(sma50,label='sma50')
plt.plot(sma75,label='sma75')
plt.plot(sma100,label='sma100')
plt.xlabel('date')
plt.ylabel('price')
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(2018,1,1)
end = datetime.date(2020,1,1)

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

close = df['Close']

#Simple Moving Average
sma5 = ta.SMA(close, timeperiod=5)
sma25 = ta.SMA(close, timeperiod=25)
sma50 = ta.SMA(close, timeperiod=50)
sma75 = ta.SMA(close, timeperiod=75)
sma100 = ta.SMA(close, timeperiod=100)

plt.plot(close,label='price')
plt.plot(sma5,label='sma5')
plt.plot(sma25,label='sma25')
plt.plot(sma50,label='sma50')
plt.plot(sma75,label='sma75')
plt.plot(sma100,label='sma100')
plt.xlabel('date')
plt.ylabel('price')
plt.legend()
plt.show()

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

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?