1
6

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.

日経平均株価のチャート分析用グラフ作成

Last updated at Posted at 2022-10-16

一目均衡表

  • 日本発祥のテクニカル指標のひとつ

1. import

import pandas as pd
from pandas_datareader import data
import matplotlib.pyplot as plt
%matplotlib inline
import mplfinance as mpf
import warnings
warnings.simplefilter('ignore')

2. 日経平均株価のデータを取得

start_date = '2022-01-01'
end_date = '2022-10-15'

df = data.DataReader('^N225', 'yahoo', start_date, end_date)
date = df.index

df.head()

スクリーンショット 2022-10-16 20.01.54.png

用語 意味
High 高値
Low 安値
Open 始値
Close 終値
Volume 出来高(株取引された量)
Adj Close 調整後の終値

3. 基準線のデータを作成

high = df['High']
low = df['Low']

# 過去26日間の最高値を計算
max26 = high.rolling(window=26).max()
# 過去26日間の最安値を計算
min26 = low.rolling(window=26).min()

# 基準線のカラムを追加
df['basic_line'] = (max26 + min26) / 2

4. 転換線のデータを作成

# 転換線は過去9日間の最高値と最安値の平均を結んだ線
high9 = high.rolling(window=9).max()
low9 = low.rolling(window=9).min()

df['turn_line'] = (high9 + low9) / 2

5. 「雲」に必要なデータの作成

# 基準線と転換線の平均で先行スパン1を作成
df['span1'] = (df['basic_line'] + df['turn_line']) / 2

# 52日間の最高値と最安値の平均で先行スパン2を作成
high52 = high.rolling(window=52).max()
low52 = low.rolling(window=52).min()
df['span2'] = (high52 + low52) / 2

6. 当日を含めた26日間遅らせたデータの遅行線を作成

# shiftメソッドで日付をずらす
df['slow_line'] = df['Adj Close'].shift(-25)

7. グラフを作成

plt.figure(figsize=(16, 6))
plt.plot(df['basic_line'], label='basic')
plt.plot(df['turn_line'], label='turn')
# 作成した二つのスパン(塗りつぶされた領域を「雲」と呼ぶ)
plt.fill_between(date, df['span1'], df['span2'], facecolor='gray', alpha=0.5, label='span')
plt.plot(df['slow_line'], label='slow')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.grid()
plt.show()

スクリーンショット 2022-10-16 20.13.02.png

7. ローソク足を追加してグラフを作成する場合

lines = [
    mpf.make_addplot(df['basic_line']), # 基準線
    mpf.make_addplot(df['turn_line']),  # 転換線
    mpf.make_addplot(df['slow_line'])   # 遅行線
]

labels = ['basic', 'turn', 'slow', 'span']

fig, ax = mpf.plot(df, type='candle', figsize=(16, 6), style='classic', xrotation=0, addplot=lines, returnfig=True,
                  fill_between=dict(y1=df['span1'].values, y2=df['span2'].values, alpha=0.5, color='gray'))

# 画像として保存する場合
# fig, ax = mpf.plot(df, type='candle', figsize=(16, 6), style='classic', xrotation=0, addplot=lines, returnfig=True,
#                   fill_between=dict(y1=df['span1'].values, y2=df['span2'].values, alpha=0.5, color='gray'), savefig='一目均衡表.png')

# 凡例を表示
ax[0].legend(labels)
plt.show()

スクリーンショット 2022-10-16 20.18.30.png

関連

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?