超簡単Pythonで株価テクニカル分析チャート作成(mplfinance)ボリンジャーバンド・MACD・RSI・ストキャスティクス・ATR
Pythonでmplfinanceを使用して株価テクニカル分析チャートを超簡単に作成(ボリンジャーバンド・MACD・RSI・ストキャスティクス・ATR)
基本編はこちらをどうぞ
1. ツールインストール
$ pip install mplfinance pandas-datareader
2. ボリンジャーバンドチャート作成
bband.py
import pandas_datareader as pdr
import mplfinance as mpf
df = pdr.get_data_yahoo("AAPL", "2020-09-01", "2020-11-01")
std = df.Close.rolling(20).std()
mean = df.Close.rolling(20).mean()
upper = mean + (std * 2)
lower = mean - (std * 2)
add_plot = [mpf.make_addplot(upper), mpf.make_addplot(mean), mpf.make_addplot(lower)]
mpf.plot(df, type = "candle", volume = True, addplot = add_plot, savefig = "bband.png")
チャート作成
$ python bband.py
3. MACDチャート作成
macd.py
import pandas_datareader as pdr
import mplfinance as mpf
df = pdr.get_data_yahoo("AAPL", "2020-09-01", "2020-11-01")
macd = (
df.Close.ewm(span=12).mean()
- df.Close.ewm(span=26).mean()
)
signal = macd.ewm(span=9).mean()
add_plot = [
mpf.make_addplot(macd, panel = 1, secondary_y = False, ylabel = "MACD"),
mpf.make_addplot(signal, panel = 1, secondary_y = False),
mpf.make_addplot(macd - signal, type = "bar", color = "g", panel = 1, secondary_y = True)
]
mpf.plot(df, type = "candle", volume = True, volume_panel = 2, addplot = add_plot, savefig = "macd.png")
チャート作成
$ python macd.py
4. RSIチャート作成
rsi.py
import pandas_datareader as pdr
import mplfinance as mpf
df = pdr.get_data_yahoo("AAPL", "2020-09-01", "2020-11-01")
rsi = 100 - 100 / (
1
- df.Close.diff().clip(lower=0).rolling(14).mean()
/ df.Close.diff().clip(upper=0).rolling(14).mean()
)
df["upper"] = 70
df["lower"] = 30
add_plot = [
mpf.make_addplot(rsi, panel = 1, secondary_y = False, ylabel = "RSI"),
mpf.make_addplot(df["upper"], panel = 1, secondary_y = False),
mpf.make_addplot(df["lower"], panel = 1, secondary_y = False)
]
mpf.plot(df, type = "candle", volume = True, volume_panel = 2, addplot = add_plot, savefig = "rsi.png")
チャート作成
$ python rsi.py
5. ストキャスティクスチャート作成
stoch.py
import pandas_datareader as pdr
import mplfinance as mpf
df = pdr.get_data_yahoo("AAPL", "2020-09-01", "2020-11-01")
k = (
(df.Close - df.Low.rolling(5).min())
/ (df.High.rolling(5).max() - df.Low.rolling(5).min())
* 100
)
d = k.rolling(3).mean()
add_plot = [
mpf.make_addplot(k, panel = 1, secondary_y = False, ylabel = "Stoch"),
mpf.make_addplot(d, panel = 1, secondary_y = False)
]
mpf.plot(df, type = "candle", volume = True, volume_panel = 2, addplot = add_plot, savefig = "stoch.png")
チャート作成
$ python stoch.py
6. ATRチャート作成
atr.py
import pandas_datareader as pdr
import mplfinance as mpf
import pandas as pd
df = pdr.get_data_yahoo("AAPL", "2020-09-01", "2020-11-01")
a = (df.High - df.Low).abs()
b = (df.High - df.Close.shift()).abs()
c = (df.Low - df.Close.shift()).abs()
atr = pd.concat([a, b, c], axis=1).max(axis=1).ewm(span=14).mean()
add_plot = [
mpf.make_addplot(atr, panel = 1, secondary_y = False, ylabel = "ATR")
]
mpf.plot(df, type = "candle", volume = True, volume_panel = 2, addplot = add_plot, savefig = "atr.png")
チャート作成
$ python atr.py
7. 参考
8. 関連記事
この記事が気に入ったらサポートをしてみませんか?