Plotly使った方が見やすいグラフができそう?と思ったのでやってみた。
参考
1分足チャートのデータの取得方法は以下
1分足チャートの5分足への変換は以下
コード
draw_candlestick_chart_with_plotly.py
import pandas as pd
import plotly.graph_objects as go
import datetime
# 1分足データを取得
df = pd.read_csv('./temp_historical_data/USDJPY.csv', nrows=1500)
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()
# グラフ化
interval = 12
vals = []
labels = []
for i in range(len(df5)//interval):
vals.append(df5.index[i*interval])
labels.append(df5.index[i*interval].strftime('%Y-%m-%d %H:%M'))
print(labels)
fig = go.Figure(
data=go.Candlestick(
x=df5.index,
open=df5['Open'],
high=df5['High'],
low=df5['Low'],
close=df5['Close'],
hovertext=['date:{}<br>open:{}<br>high:{}<br>low:{}<br>close:{}'
.format(i.strftime('%Y-%m-%d %H:%M'), df5.loc[i,'Open'],df5.loc[i,'High'],df5.loc[i,'Low'],df5.loc[i,'Close']) for i in df5.index],
hoverinfo="text"
),
layout = go.Layout(
xaxis = dict(
ticktext = labels,
tickvals = vals,
tickangle = -90
),
)
)
fig.show()
グラフ
感想
Plotlyの方がお手軽に見やすいグラフが作れそう。SMAとか同時に描画してみたい。