美文网首页想法简友广场心理
R语言第四次作业(股票、债券收益率计算)

R语言第四次作业(股票、债券收益率计算)

作者: Cache_wood | 来源:发表于2021-07-12 23:22 被阅读0次

1.

library(quantmod)
library(timeSeries)                                
library(tseries)  
library(PerformanceAnalytics) 

setSymbolLookup(HUADONG = list(name="000963.sz", src="yahoo"))
getSymbols("HUADONG", from ="2012-01-04", to="2013-12-31")

assets <- merge.xts(HUADONG,all=FALSE)
dim(assets)
head(assets)       
class(assets)

assets.w <- to.weekly(assets)
print(assets.w)

simreturn = CalculateReturns(assets.w$assets.Close, method = c("discrete","log"))

chart.CumReturns(simreturn,main="cumulative return") 

2.

####  work2
library(quantmod)
library(timeSeries)                                
library(tseries)  
library(PerformanceAnalytics) 

setSymbolLookup(DONGHUA = list(name=c("002065.sz"), src="yahoo"))
getSymbols("DONGHUA", from = "2009-01-01", to="2012-12-31")

setSymbolLookup(RONGSHENG = list(name=c("002146.SZ"), src="yahoo"))
getSymbols("RONGSHENG", from = "2009-01-01", to="2012-12-31")


simreturn1 = CalculateReturns(DONGHUA$`002065.SZ.Close`, method = c("discrete","log"))
simreturn2 = CalculateReturns(RONGSHENG$`002146.SZ.Close`, method = c("discrete","log"))

chart.CumReturns(simreturn1,main="cumulative return")
chart.CumReturns(simreturn2,main="cumulative return")
东华软件
荣盛发展

4.

####  work4
#define a loss function
f_exp <- function(r,p,Cs,Cp){
  n<-length(Cs)
  tt <- 1:n
  loss <- p - sum(Cs/(1+r)^tt)-Cp/(1+r)^n
  loss
}
Cs <- rep(2.5,10)
Cp <- 100
p <- 105
uniroot(f_exp,c(0,1),p=p,Cs=Cs,Cp=Cp)

EAR <- function(r,m){
  res <- (1+r/m)^m-1
  res
}
r <- 0.05
m <- 2
EAR(r=r,m=m)
 uniroot(f_exp,c(0,1),p=p,Cs=Cs,Cp=Cp)
$`root`
[1] 0.01943179

$f.root
[1] -0.01659659

$iter
[1] 5

$init.it
[1] NA

$estim.prec
[1] 6.103516e-05
> r <- 0.05
> m <- 2
> EAR(r=r,m=m)
[1] 0.050625

相关文章

网友评论

    本文标题:R语言第四次作业(股票、债券收益率计算)

    本文链接:https://www.haomeiwen.com/subject/vctjsltx.html