🔥

FXデータ分析:USDJPYの時間帯別ボラティリティの可視化

2023/06/03に公開

ドル円の時間帯の可視化です。

import pandas as pd
import MetaTrader5 as mt5
from datetime import timedelta

# MT5への接続
mt5.initialize()

# 読み込む通貨ペアと時間枠、データの件数を指定
symbol = 'USDJPYm'
timeframe = mt5.TIMEFRAME_M5
count = 100000

# データの取得
rates = mt5.copy_rates_from_pos(symbol, timeframe, 0, count)

# DataFrameに変換
df = pd.DataFrame(rates)

# 日時をPandasの日時形式に変換し、GMT+9に変換
df['time'] = pd.to_datetime(df['time'], unit='s') + timedelta(hours=9)

df['high_low_ratio'] = (df['high'] - df['low']) / df['low']

import seaborn as sns
import matplotlib.pyplot as plt

# インデックスを日付に変換
df.set_index('time', inplace=True)

# 5分ごとの平均リターンを計算
df['5min_avg_return'] = df['high_low_ratio'].rolling(window=5).mean()

# 時刻をグループ化して平均を計算
grouped_df = df.groupby(df.index.time).mean()

# 時刻を文字列に変換
grouped_df.index = grouped_df.index.astype(str)

# グラフの作成
plt.figure(figsize=(50, 10))
sns.barplot(x=grouped_df.index, y=grouped_df['5min_avg_return'])
plt.xlabel('Time')
plt.ylabel('5min Average Return')
plt.title('5-minute Average Return (Grouped by Time)')

# X軸のラベルを斜めに表示
plt.xticks(rotation=90)

plt.show()

Discussion