0.1 美國10年期公債(Treasury notes)
10
年期美國政府公債,是許多人觀察美債市場常用的指標,
多數人認為其收益率能顯示市場對美國總體經濟的長期看法。
0.2 抓取資料
rm(list=ls(all=TRUE)):清空之前的df
library(quantmod):載入套件
start =
“2010-01-01”:開始時間
end =
as.character(Sys.Date()):結束時間
Inflation_Rate=
getSymbols(“T10YIE”, from=start, to=end, src=“FRED”,
auto.assign=FALSE)
:從FRED抓取10年期通貨膨脹率資料
Inflation_Rate= Inflation_Rate/100:轉換單位
long_rate_ticker = “DGS10”:令DGS10為長天期公債利率
long_rate = getSymbols(long_rate_ticker, from=start, to=end ,
src=“FRED”, auto.assign=FALSE):從FRED抓取10年期公債利率資料
long_rate = long_rate/100:轉換單位
Asset =
merge(Inflation_Rate, long_rate) Asset =
na.omit(Asset):合併資料在Asset
# r, results='hide', fig.width=3, fig.height=3
rm(list=ls(all=TRUE))
library(quantmod)
start = "2010-01-01"
end = as.character(Sys.Date())
Inflation_Rate= getSymbols("T10YIE", from=start, to=end, src="FRED", auto.assign=FALSE)
Inflation_Rate= Inflation_Rate/100
start = "2010-01-01"
end = as.character(Sys.Date())
long_rate_ticker = "DGS10"
long_rate = getSymbols(long_rate_ticker, from=start, to=end , src="FRED", auto.assign=FALSE)
long_rate = long_rate/100
Asset = merge(Inflation_Rate, long_rate)
Asset = na.omit(Asset)0.3 Dygraph
library(dygraphs):載入Dygraph
fig1 = dygraph(Asset,
main=“Inflation Rate and T-Notes”):Asset放入fig,設定標題
dyLegend(show = “follow”,width = 200
):用dyLegend設定數據跟著滑鼠移動
dyAxis(‘x’, drawGrid=FALSE)
:用dyAxis設定軸
dySeries(“DGS10”, label=“T-Notes rate”,
color=“red”, stepPlot =
TRUE,axis=’y2:
用dySeries設定點跟著滑鼠移動以及樣式
dyAnnotation(“2022-3-17”, text=“升息”, width= 40
):用dyAnnotation在點上顯示升息降息日
dyEvent(“2022-5-4”, “QT”,
labelLoc = “bottom”,color=“LightPink”):用dyEvent在背景加入標籤
dyRangeSelector(dateWindow = c(start,
end)):用dyRangeSelector設定可選取滾輪
library(dygraphs)
fig1 = dygraph(Asset, main="Inflation Rate and T-Notes") %>%
dyLegend(show = "follow",width = 200 )%>%
dyAxis('x', drawGrid=FALSE) %>%
dyAxis("y", label="10Y Inflation rate") %>%
dyAxis("y2", label="10Y T-Notes rate") %>%
dySeries("T10YIE", label="Inflation rate", stepPlot = TRUE,color="blue", axis='y') %>%
dySeries("DGS10", label="T-Notes rate", color="red", stepPlot = TRUE,axis='y2') %>%
dyAnnotation("2020-3-3", text = "降息" ,width = 40) %>%
dyEvent("2010-11-3", "QE", labelLoc = "bottom",color="LightPink")%>%
dyOptions(axisLineColor="navy",
gridLineColor="lightblue") %>%
dyRangeSelector(dateWindow = c(start, end))
fig10.4 程式碼與雙Y軸資料圖
rm(list=ls(all=TRUE))
library(quantmod)
start = "2010-01-01"
end = as.character(Sys.Date())
Inflation_Rate= getSymbols("T10YIE", from=start, to=end, src="FRED", auto.assign=FALSE)
Inflation_Rate= Inflation_Rate/100
start = "2010-01-01"
end = as.character(Sys.Date())
long_rate_ticker = "DGS10"
long_rate = getSymbols(long_rate_ticker, from=start, to=end , src="FRED", auto.assign=FALSE)
long_rate = long_rate/100
Asset = merge(Inflation_Rate, long_rate)
Asset = na.omit(Asset)
library(dygraphs)
fig1 = dygraph(Asset, main="Inflation Rate and T-Notes") %>%
dyLegend(show = "follow",width = 200 )%>%
dyAxis('x', drawGrid=FALSE) %>%
dyAxis("y", label="10Y Inflation rate") %>%
dyAxis("y2", label="10Y T-Notes rate") %>%
dySeries("T10YIE", label="Inflation rate", stepPlot = TRUE,color="blue", axis='y') %>%
dySeries("DGS10", label="T-Notes rate", color="red", stepPlot = TRUE,axis='y2') %>%
dyAnnotation("2022-3-17", text = "升息" ,width = 40) %>%
dyAnnotation("2022-5-4", text = "升息",width = 40) %>%
dyAnnotation("2020-3-3", text = "降息" ,width = 40) %>%
dyAnnotation("2020-3-15",text = "降息",width = 40) %>%
dyAnnotation("2019-10-31", text = "降息",width = 40) %>%
dyAnnotation("2019-9-19", text = "降息",width = 40) %>%
dyAnnotation("2019-8-1", text = "降息",width = 40) %>%
dyAnnotation("2018-12-20",text = "升息" ,width = 40) %>%
dyAnnotation("2018-9-27", text = "升息" ,width = 40) %>%
dyAnnotation("2018-6-13",text = "升息" ,width = 40) %>%
dyAnnotation("2018-3-21",text = "升息" ,width = 40) %>%
dyAnnotation("2017-12-13",text = "升息",width = 40 ) %>%
dyAnnotation("2017-3-15", text = "升息" ,width = 40) %>%
dyAnnotation("2016-12-14", text = "升息" ,width = 40) %>%
dyAnnotation("2015-12-17", text = "升息",width = 40) %>%
dyEvent("2022-5-4", "QT", labelLoc = "bottom",color="LightPink")%>%
dyEvent("2020-3-15", "QE", labelLoc = "bottom",color="LightPink")%>%
dyEvent("2017-10-1", "QT", labelLoc = "bottom",color="LightPink")%>%
dyEvent("2013-1-1", "QE", labelLoc = "bottom",color="LightPink")%>%
dyEvent("2012-9-13", "QE", labelLoc = "bottom",color="LightPink")%>%
dyEvent("2010-11-3", "QE", labelLoc = "bottom",color="LightPink")%>%
dyOptions(axisLineColor="navy",
gridLineColor="lightblue") %>%
dyRangeSelector(dateWindow = c(start, end))
fig10.5 結論
當通貨膨脹率升太高時,美國聯準會會實施升息以抑制通貨膨脹,控制物價,必要時會進行縮表(QT),
短期利率因升息上升和長期利率的利差下降,當通貨膨脹率降低時,
美國聯準會則會實施降息以促進投資消費,必要時會進行量化寬鬆政策(QE),
短期利率因降息下跌和長期利率的利差上升,
藉由雙Y軸資料圖我們可以發現,長期公債利率會隨著通貨膨脹率浮動!
0.6 參考資料
NOTE:Treasury notes:https://rich01.com/t-bill-vs-t-note-vs-t-bond/
NOTE:10-Year Breakeven Inflation Rate
:https://fred.stlouisfed.org/series/T10YIE
NOTE:Market Yield on U.S. Treasury Securities
at 10-Year Constant
Maturity:https://fred.stlouisfed.org/series/DGS10
NOTE:DYGRAPH:https://rstudio.github.io/dygraphs/gallery-series-options.html
NOTE:美國升息除息QE QT歷史:https://www.macromicro.me/time_line?id=11&stat=483
0.7 自我評分
主題:10 年期通貨膨脹率升高時,10年期公債殖利率是否也同步走高
評分:子主題基本分+{dyLegend,dyEvent,dyAnnotation}R之dygraphs套件上課介紹外之其他函數+RMarkdown
主題:利用 R 之 plotly 套件建立 candlestick
chart+其他技術指標應用與結果分析
評分:子主題基本分+{annotations}R之dygraphs套件上課介紹外之其他函數+RMarkdown
總分:2個主題基本分(70分)+2個都用RMarkdown(5*2=10)分+上課套件外其他函數
RPub網址為:https://rpubs.com/realslimxuan/907864