2
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 1 year has passed since last update.

[Python]mplfinance で平均足を表示させる

Last updated at Posted at 2021-07-04

はじめに

トレンドをわかりやすく表示できる平均足を、pandasとmplfinanceで表示させる方法を記します。

環境

  • Windows 10 home
  • Python(3.7.3)
  • JupyterLab(3.0.6)
  • pandas(1.2.1)
  • investpy(1.0.3)
  • mplfinance(0.12.7a0)

平均足とは

平均足とは、以下の定義を持つ足のことです。

  • 平均足始値 = (前の足の始値 + 前の足の終値) /2

  • 平均足終値 = (始値 + 高値 + 安値 + 終値) /4

  • 平均足高値 = 高値

  • 平均足安値 = 安値

始値と終値の定義が、ローソク足と異なります。ローソク足と比較して、陽線と陰線が混じりにくく、トレンドがわかりやすい特徴を持っています。

データ

investpyでとってきたドル円の日足データを使います。

平均足を表示させる

定義のまま、データを加工して表示するのみです。

import numpy as np
import investpy
import mplfinance as mpf
usd_jpy = investpy.get_currency_cross_historical_data(currency_cross='USD/JPY', from_date='01/02/2021', to_date='30/03/2021')
cl = usd_jpy[['Open', 'High', 'Low', 'Close']].mean(axis=1)
op = [(usd_jpy['Open'].iloc[0] + usd_jpy['Close'].iloc[0])/2] * len(usd_jpy)
for i, c in enumerate(cl[:-1]):
    op[i+1] = (op[i] + c)/2
dataset = np.vstack([op, cl, usd_jpy['High'], usd_jpy['Low']])
hi = dataset.max(axis=0)
lo = dataset.min(axis=0)
usd_jpy['Open'] = op
usd_jpy['High'] = hi
usd_jpy['Low'] = lo
usd_jpy['Close'] = cl
usd_jpy = usd_jpy.dropna()

mpf.plot(usd_jpy, type='candle', savefig='heikinashi.png', closefig=False)

heikinashi.png

2
3
1

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