3
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で5分足チャートからボリンジャーバンドを計算してグラフに表示してみた

Last updated at Posted at 2021-07-20

pyti(パイタイ)というライブラリを利用するのでpipでインストールしておく。

参考

下のコードとほぼ一緒です。

コード


import pandas as pd
import datetime
import mplfinance as mpf
from pyti.bollinger_bands import upper_bollinger_band as bb_up
from pyti.bollinger_bands import middle_bollinger_band as bb_mid
from pyti.bollinger_bands import lower_bollinger_band as bb_low

df = pd.read_csv('./temp_historical_data/USDJPY.csv', nrows=500)
df.columns = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)

# 5分足に変換
df5 = pd.DataFrame()
rule = '5T'
df5['Open'] = df['Open'].resample(rule).first()
df5['Close'] = df['Close'].resample(rule).last()
df5['High'] = df['High'].resample(rule).max()
df5['Low'] = df['Low'].resample(rule).min()

# ボリンジャーバンド計算
bb = pd.DataFrame()
data = df5['Close'].values.tolist()
period = 20
bb['Close'] = df5['Close']
bb['Up'] = bb_up(data, period)
bb['Mid'] = bb_mid(data, period)
bb['Low'] = bb_low(data, period)

bbplot = mpf.make_addplot(bb[['Close', 'Up', 'Mid', 'Low']])

mpf.plot(df5, type='candle', addplot=[bbplot],
        datetime_format='%Y/%m/%d %H:%M', xrotation=90, style='yahoo', savefig=dict(fname='./figures/draw_bb.png',dpi=100))

出来上がったグラフ

draw_bb.png

説明

ptytiで簡単。pytiはMACDの計算もできる。以下の通り。


from pyti.moving_average_convergence_divergence import moving_average_convergence_divergence as macd_func

macd = pd.DataFrame()
macd['Close'] = df5['Close']
data = macd['Close'].values.tolist()
macd['MACD'] = macd_func(data, 12, 26)
macd['Signal'] = macd['MACD'].ewm(span=9).mean()

macdplot = mpf.make_addplot(macd[['MACD', 'Signal']])

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