Как найти арифметическую доходность и совокупную доходность в R (для облигаций)? - PullRequest
0 голосов
/ 09 ноября 2019

Как найти дневной доход и совокупный доход (арифметический) по облигациям через квантмод? Я пробовал соответственно пакеты «dailyreturn» и «cumprod» для этих двух операций с акциями и индексами, но кажется, что это может быть неправильно, когда я делаю это с облигациями, или?

Задание 1d) спрашиваетя, чтобы найти ежедневный доход, в то время как 1e) просит меня найти кумулятивный арифметический доход (вкладывая 100 долларов). Позже я должен подготовить все это.

enter code here




################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")


#######################1e)###########################################
...