ETFと利回りを比較

代表的なETFと債権利回りの推移を比較します。データは stooqから取得したものを取得しています。

メディアプログラミング入門 WebスクレイピングとWebAPI』の講義資料45pで紹介されていたPandas Datareaderを使用しています。

import numpy as np
import pandas as pd
import pandas_datareader.data as web
import matplotlib.pyplot as plt
import japanize_matplotlib
from IPython.display import display, HTML
from utils import get_finance_data, get_rsi

ETF

# 銘柄名、期間、保存先ファイル
start = "2021-08-01"
end = "2022-01-31"

# データを取得する
voo = get_finance_data("VOO", source="stooq", start=start, end=end)
display(HTML(f"<h3>VOO</h3>"))
display(voo.head())

vti = get_finance_data("VTI", source="stooq", start=start, end=end)
display(HTML(f"<h3>VTI</h3>"))
display(vti.head())

spx = get_finance_data("^SPX", source="stooq", start=start, end=end)
display(HTML(f"<h3>S&P500</h3>"))
display(spx.head())

ndq = get_finance_data("^NDQ", source="stooq", start=start, end=end)
display(HTML(f"<h3>Nasdaq</h3>"))
display(ndq.head())

VOO

OpenHighLowCloseVolume
Date
2022-01-31405.67413.9700404.3500413.699200435
2022-01-28397.82406.3300393.3000406.2612096807
2022-01-27402.93405.9700394.8200396.5412455226
2022-01-26405.66408.1954394.3400398.5614377346
2022-01-25398.17404.2600392.7325399.4616836326

VTI

OpenHighLowCloseVolume
Date
2022-01-31222.09226.950221.22226.815306436
2022-01-28217.50222.170214.93222.094961209
2022-01-27220.81222.400215.90216.755791524
2022-01-26222.72224.160216.05218.377096416
2022-01-25219.10222.015215.64219.226600378

S&P500

OpenHighLowCloseVolume
Date
2022-01-314431.794516.894414.024515.552960132803
2022-01-284336.194432.724292.464431.852926083817
2022-01-274380.584428.744309.504326.513070684348
2022-01-264408.434453.234304.804349.933239353450
2022-01-254366.644411.014287.114356.453069079477

Nasdaq

OpenHighLowCloseVolume
Date
2022-01-3113812.2014242.9013767.7114239.883268652504
2022-01-2813436.7113771.9113236.5513770.573092819850
2022-01-2713710.9913765.9113322.6613352.783373437394
2022-01-2613871.7714002.6513392.1913542.123664304374
2022-01-2513610.8713781.6313414.1413539.293265336637

債権利回り

usy10 = get_finance_data("10USY.B", source="stooq", start=start, end=end)
display(HTML(f"<h3>10-Year U.S. Bond Yield</h3>"))
display(usy10.head())

usy2 = get_finance_data("2USY.B", source="stooq", start=start, end=end)
display(HTML(f"<h3>2-Year U.S. Bond Yield</h3>"))
display(usy2.head())

10-Year U.S. Bond Yield

OpenHighLowClose
Date
2022-01-311.7891.8161.7711.780
2022-01-281.8301.8481.7731.777
2022-01-271.8401.8511.7831.799
2022-01-261.7691.8761.7691.867
2022-01-251.7531.7921.7351.776

2-Year U.S. Bond Yield

OpenHighLowClose
Date
2022-01-311.19861.21451.15871.1827
2022-01-281.21411.22601.16031.1703
2022-01-271.18221.20611.16031.1882
2022-01-261.03131.16031.01541.1544
2022-01-251.00551.03530.98971.0254
plt.figure(figsize=(12, 4))
plt.plot(usy10.Close, label="米国債券10年 年利回り")
plt.plot(usy2.Close, label="米国債券2年 年利回り")
plt.legend()
plt.tick_params(rotation=90)
plt.grid()
plt.show()

png

S&P500と10年債利回りの比較

import mplfinance as mpf

fig = mpf.figure(figsize=(16, 7), tight_layout=True, style="default")

ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
mpf.plot(spx, type="candle", style="yahoo", datetime_format="%Y/%m/%d", ax=ax1)
ax1.set_ylabel("S&P500")
ax1.invert_xaxis()

mpf.plot(
    usy10,
    type="line",
    style="starsandstripes",
    datetime_format="%Y/%m/%d",
    ax=ax2,
)

# 左側にラベルを表示する
ax2.tick_params(labelleft=True, labelright=False)
ax2.set_ylabel("10-Year U.S. Bond Yield")
ax2.yaxis.set_label_position("left")
ax2.legend(["10-Year U.S. Bond Yield"])
<matplotlib.legend.Legend at 0x7fcc4de60970>

png