如何通过quantmod找到债券的每日收益率和累积收益率(算术)?
我分别在这两个操作中分别使用股票和指数尝试了“ dailyreturn”和“ cumprod”软件包,但是当我用债券进行交易时似乎还是错了??
任务1d)要求我找到每日收益,同时1e)要求我找到累积算术收益(投资100美元)。后来我必须把所有这些都画出来。
R脚本中提供了带有任务和注释的整个脚本
################1a)######################
####Task 1a) asks me to open security data. I should use stocks, indexes
#and bonds.
library(quantmod)
start <- as.Date("2012-12-31")
end <- as.Date("2018-12-31")
tckrs <- c("AAPL","TSLA")
Stocks <- getSymbols(tckrs, auto.assign = TRUE, from = start, to=end)
Index <- getSymbols("^NYA", auto.assign = TRUE, from = start, to=end)
DGS10 <- getSymbols(Symbols = "DGS10", src = "FRED", auto.assign = FALSE)
DGS10<- na.omit(DGS10)
Bond <-DGS10["2012-12-31/2018-12-31"]
View(Bond)
Bond
#####################################################################
################1b)###################
########Task 1b) asks me to use adjusted close and make a timeseries
#plot
####Stocks####
colnames(AAPL) <- c("Open","High","Low","Close","Volume","Adjusted")
colnames(TSLA) <- c("Open","High","Low","Close","Volume","Adjusted")
colnames(NYA) <- c("Open","High","Low","Close","Volume","Adjusted")
AAPL.Adj <- AAPL[,6]
TSLA.Adj <- TSLA[,6]
Stocks <-cbind(AAPL,TSLA)
head(Basket)
myColors <- c("red", "darkgreen")
plot(x = Stocks[,"Adjusted"],main = "Stock prices", ylim = c(0, 450),
major.ticks= "years",
minor.ticks = FALSE, col = "red")
lines(x = Stocks[,"Adjusted.1"], col = "darkgreen")
title(xlab="Time",ylab="Adjusted close" )
####Index####
colnames(NYA) <- c("Open","High","Low","Close","Volume","Adjusted")
plot(x = NYA[,"Adjusted"],main = "Index price",xlab= "Time",ylab="Adjusted
Close", major.ticks= "years",
minor.ticks = FALSE, col = "brown")
title(xlab = "Time", ylab="Adj. C.")
legend(x="center",col = "brown",legend = c("NYA"), lty = 1)
####Bond####
plot(x=Bond[,"DGS10"], main= "Bond", xlab="Time",ylab="Rent",major.ticks =
"years", col = "purple")
legend(x="topleft",col="purple",legend = c("DSG10"),lty=1)
#######################################
########################## 1c)########################################
#1c) wants me to find and plot the daily arithmetic return for
#each security (Im not sure how to cope with bonds in this case, but I
#think that I did it the right way)
TSLA_return_plot <- plot(dailyReturn(Ad(TSLA), type="arithmetic",
main="Daily return"),type="l")
title( xlab = "Time",ylab = "Percent")
AAPL_returnplot <- plot(dailyReturn(Ad(AAPL), type="arithmetic",
main="Daily return"),type="l")
title( xlab = "Time",ylab = "Percent")
NYA_returnplot <- plot(dailyReturn(Ad(NYA), type="arithmetic",
main="Daily return"),type="l")
title( xlab = "Time",ylab = "Percent")
NYA_returnplot
TB10YR_returnplot <- plot(dailyReturn(Bond)/(365*100), type = "l")
######################### 1d)########################################
######## Task 1d) asks me to find the cummulativ return for 100$ invested
#(Again, Im wondering if Im doing it the right way with the bond)
TSLA_cummulativR <- 100*cumprod(1+dailyReturn(Ad(TSLA)))
AAPL_cummulativR <- 100*cumprod(1+dailyReturn(Ad(AAPL)))
NYA_cummulativR <- 100*cumprod(1+dailyReturn(Ad(NYA)))
TB10YR_cummulativR <- 100*cumprod(1+(dailyReturn(Bond)))
TB10YR_cummulativR
plot(NYA_cummulativR,type = "l", main = "Cummulativ daily Return (100
invested)",col = "2", xlab="Time",ylab="Return")
title(xlab = "Time", ylab = "C. return", col="green")
plot(AAPL_cummulativR, type="l", main="Cummulativ daily Retun (100
invested)")
title(xlab = "Time", ylab = "C. return")
plot(TSLA_cummulativR, type="l",ylim = c(0,1400), main = "Cummulativ daily
Return (100 invested)")
title(xlab = "Time", ylab = "C. return")
plot(TB10YR_cummulativR, type = "l")