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分足チャートをPlotlyを使ってグラフ化してみた

Posted at

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()

グラフ

スクリーンショット 2021-07-27 19.48.49.png

スクリーンショット 2021-07-27 19.49.02.png

感想

Plotlyの方がお手軽に見やすいグラフが作れそう。SMAとか同時に描画してみたい。

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?