0
1

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 2023-05-10

はじめに

今回は、FRED(Federal Reserve Economic Data)からライブラリー(fredapi)を使って円ドル為替レートを取得し、インタラクティブなグラフを描くことにした。

事前準備

取得してあるAPIキーは’FRED_api_key.txt’というファイル名にしてGoogleドライブに保存してあるので、まずは、Googleドライブのマウント。

# Colaboratory 上で Google Drive をマウント
from google.colab import drive
drive.mount('/content/drive')

Google Colabにfredapiはインストールされていないのでインストール。

! pip install fredapi

コード

FREDにおける円ドル為替レートの系列IDがDEXJPUSであることを確認して、以下のコードを実行して描画。

import pandas as pd
import plotly.graph_objects as go
from fredapi import Fred

# APIキーファイルのパス
api_key_file_path = '/content/drive/MyDrive/FRED_api_key.txt'

# APIキーを読み込む
with open(api_key_file_path, 'r') as file:
    api_key = file.read().strip()

# FREDオブジェクトを作成
fred = Fred(api_key=api_key)

# FREDから為替レートのデータをダウンロード
data = fred.get_series('DEXJPUS')

# データをPandasデータフレームに変換
df = pd.DataFrame(data, columns=['Rate'])

# インデックスを日付に変換
df.index = pd.to_datetime(df.index)

# 1990年代、2010年代、2020年代の最小値および最大値を見つける
min_1990s = df['1990':'1999']['Rate'].idxmin()
min_rate_1990s = df.loc[min_1990s, 'Rate']

min_2010s = df['2010':'2019']['Rate'].idxmin()
min_rate_2010s = df.loc[min_2010s, 'Rate']

max_2020s = df['2020':'2029']['Rate'].idxmax()
max_rate_2020s  = df.loc[max_2020s, 'Rate']

# グラフを作成
fig = go.Figure()

fig.add_trace(go.Scatter(x=df.index, y=df['Rate'], mode='lines', name='JPY/USD Rate'))

# ホバーテキストを設定
hovertext_min_1990s = f"Date: {min_1990s.strftime('%Y-%m-%d')}<br>Rate: {min_rate_1990s}"
hovertext_min_2010s = f"Date: {min_2010s.strftime('%Y-%m-%d')}<br>Rate: {min_rate_2010s}"
hovertext_max_2020s = f"Date: {max_2020s.strftime('%Y-%m-%d')}<br>Rate: {max_rate_2020s}"

fig.add_trace(go.Scatter(x=[min_1990s, min_2010s], y=[min_rate_1990s, min_rate_2010s], mode='markers', marker=dict(color='red', size=10), name='Min Rate', text=[hovertext_min_1990s, hovertext_min_2010s], hoverinfo='text'))
fig.add_trace(go.Scatter(x=[max_2020s ], y=[max_rate_2020s ], mode='markers', marker=dict(color='red', size=10), name='Max Rate', text=[hovertext_max_2020s], hoverinfo='text'))

fig.update_layout(
    title='JPY/USD Spot Exchange Rate(1971/1/4-)',
    xaxis_title='Date',
    yaxis_title='Japanese Yen to One U.S. Dollar',
    hovermode='x',
    showlegend=False
)

fig.show()

おわりに

出力結果をここにアップすることができないが、昨年(2022年)の円安局面で1ドル=150円を突破した(みずほ銀行から公表されているデータhttps://www.mizuhobank.co.jp/market/csv/quote.csv では、10月21日に1ドル=150.26円)はずだったが、このグラフでは突破してないことが分かった(10月20日の1ドル=149.82円がこの時期の最円安)。
コードがおかしいのかと疑ったが、そこは問題なさそう。為替レートなのでどういうレートかもキチンと見る必要はあるが、東京市場とニューヨーク市場の違いなのだろう。
これには、可視化の効果はやはり大きいと気づかされたところ。次は、みずほ銀行のデータか日銀のデータを使って、試してみたい。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?