LoginSignup
8
11

More than 3 years have passed since last update.

PythonでFXの1分足データを5分足に変換

Posted at

概要

前回は、このサイトのFXデータをPythonでローソク足表示させてみました。データは1分足のものを使っていましたが、任意の分足に変換したいことがあったので、その実装コードを残しておきます。

環境

前回と同じく以下です。
* Python 3.6 on Anaconda
* Jupiter notebook on Anaconda

1分足データ表示

このサイトのHISTDATA_COM_ASCII_EURJPY_M1_201911.zipをグラフ表示します。データダウンロード後、以下を実行させて下さい。

import plotly.graph_objects as go

import pandas as pd
from datetime import datetime

df_one = df["2019-11-18": "2019-11-20"]

df_show = df_one

fig = go.Figure(data=[
    go.Candlestick(
        x=df_show.index,
        open=df_show.Open,
        high=df_show.High,
        low=df_show.Low,
        close=df_one.Close
    )
])

fig.show()

以下のように表示されます。
newplot (2).png

1分足から5分足に変換

以下でresampleを使って5分足に変換出来ます。rateを10T、1Hなど変更すれば、任意の足に変換可能です。

df_five = pd.DataFrame()
rule = "5T"
df_five["Open"] = df_one["Open"].resample(rule).first()
df_five["Close"] = df_one["Close"].resample(rule).last()
df_five["High"] = df_one["High"].resample(rule).max()
df_five["Low"] = df_one["Low"].resample(rule).min()

5分足データ表示

以下を実行して下さい。

df_show = df_five

fig = go.Figure(data=[
    go.Candlestick(
        x=df_show.index,
        open=df_show.Open,
        high=df_show.High,
        low=df_show.Low,
        close=df_show.Close
    )
])

fig.show()

以下のように表示されます。
newplot (3).png

一分足と5分足で違いがよくわからないので、長さと先頭を見比べて見ましょう。
1分足は、、、

print(df_one.shape)
print(df_one.head())

'''
output: 
(4319, 5)
                        Open     High      Low    Close  Volume
datetime                                                       
2019-11-18 00:00:00  120.360  120.371  120.360  120.368       0
2019-11-18 00:01:00  120.368  120.372  120.360  120.360       0
2019-11-18 00:02:00  120.361  120.362  120.360  120.360       0
2019-11-18 00:03:00  120.360  120.376  120.360  120.376       0
2019-11-18 00:04:00  120.377  120.380  120.374  120.376       0
'''

5分足は、、、

print(df_five.shape)
print(df_five.head())

'''
output:
(864, 4)
                        Open    Close     High      Low
datetime                                               
2019-11-18 00:00:00  120.360  120.376  120.380  120.360
2019-11-18 00:05:00  120.376  120.382  120.387  120.369
2019-11-18 00:10:00  120.381  120.361  120.381  120.352
2019-11-18 00:15:00  120.361  120.354  120.365  120.341
2019-11-18 00:20:00  120.354  120.349  120.354  120.341
'''

たいたい5分の一のサイズになっていて、刻みも5分になってますね。いいのではないでしょうか。

以上!

8
11
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
8
11