有关 getSymbols 的更多信息下载多个符号的数据并每月计算 returns
more on getSymbols downloading data for multiple symbols and calculate monthly returns
对此进行了引用 post Similar question
我正在下载一系列代码,我需要每月计算 returns 调整后的收盘价,但我似乎无法让它正常工作。较早的 post 我有参考使用 ROC @Joshua Ulrich 来处理这个问题,但这是每天 returns。其他示例使用 'close',这对我没有好处。
我的起点:
library('quantmod')
tickers <- c("XOM","MSFT","JNJ","GE","CVX","WFC","PG","JPM","VZ","PFE",
"T","IBM","MRK","BAC","DIS","ORCL","PM","INTC","SLB")
#The following gives me what I need, but using Close instead of Adjusted
stocks_ret <- lapply(tickers, function(sym) monthlyReturn(type = 'log',(na.omit(getSymbols(sym, from='2013-01-01, auto.assign=FALSE)))))
Joshua Ulrich 解决方案(效果很好,但我不知道如何将其变成每月 returns):
# create environment to load data into
Data <- new.env()
getSymbols(c("^GSPC",Symbols), from="2007-01-01", env=Data)
# calculate returns, merge, and create data.frame (eapply loops over all
# objects in an environment, applies a function, and returns a list)
Returns <- eapply(Data, function(s) ROC(Ad(s), type="discrete"))
ReturnsDF <- as.data.frame(do.call(merge, Returns))
#adjust column names are re-order columns
colnames(ReturnsDF) <- gsub(".Adjusted","",colnames(ReturnsDF))
ReturnsDF <- ReturnsDF[,c("GSPC",Symbols)]
然后我尝试了:
Returns <- eapply(Data, function(s) ROC(Ad(s)[endpoints(tickers, on = 'months'),], type="discrete"))
后者给了我一条错误信息:“ try.xts(x, error = "must be either xts-coercible or timeBased")"
但是,我无法找到解决方案来解决如何按月而不是每天计算的问题。'n=21' 没有解决问题。
有人吗??
Chris,这是解决您问题的一种方法
library('quantmod')
library('dplyr')
tickers <- c("XOM","MSFT","JNJ","GE","CVX","WFC","PG","JPM","VZ","PFE",
"T","IBM","MRK","BAC","DIS","ORCL","PM","INTC","SLB")
Stock_Data <- tickers %>% lapply(function(x) getSymbols(x,auto.assign=FALSE)) %>%
lapply(function(x) monthlyReturn(Ad(x)))
对此进行了引用 post Similar question
我正在下载一系列代码,我需要每月计算 returns 调整后的收盘价,但我似乎无法让它正常工作。较早的 post 我有参考使用 ROC @Joshua Ulrich 来处理这个问题,但这是每天 returns。其他示例使用 'close',这对我没有好处。
我的起点:
library('quantmod')
tickers <- c("XOM","MSFT","JNJ","GE","CVX","WFC","PG","JPM","VZ","PFE",
"T","IBM","MRK","BAC","DIS","ORCL","PM","INTC","SLB")
#The following gives me what I need, but using Close instead of Adjusted
stocks_ret <- lapply(tickers, function(sym) monthlyReturn(type = 'log',(na.omit(getSymbols(sym, from='2013-01-01, auto.assign=FALSE)))))
Joshua Ulrich 解决方案(效果很好,但我不知道如何将其变成每月 returns):
# create environment to load data into
Data <- new.env()
getSymbols(c("^GSPC",Symbols), from="2007-01-01", env=Data)
# calculate returns, merge, and create data.frame (eapply loops over all
# objects in an environment, applies a function, and returns a list)
Returns <- eapply(Data, function(s) ROC(Ad(s), type="discrete"))
ReturnsDF <- as.data.frame(do.call(merge, Returns))
#adjust column names are re-order columns
colnames(ReturnsDF) <- gsub(".Adjusted","",colnames(ReturnsDF))
ReturnsDF <- ReturnsDF[,c("GSPC",Symbols)]
然后我尝试了:
Returns <- eapply(Data, function(s) ROC(Ad(s)[endpoints(tickers, on = 'months'),], type="discrete"))
后者给了我一条错误信息:“ try.xts(x, error = "must be either xts-coercible or timeBased")"
但是,我无法找到解决方案来解决如何按月而不是每天计算的问题。'n=21' 没有解决问题。
有人吗??
Chris,这是解决您问题的一种方法
library('quantmod')
library('dplyr')
tickers <- c("XOM","MSFT","JNJ","GE","CVX","WFC","PG","JPM","VZ","PFE",
"T","IBM","MRK","BAC","DIS","ORCL","PM","INTC","SLB")
Stock_Data <- tickers %>% lapply(function(x) getSymbols(x,auto.assign=FALSE)) %>%
lapply(function(x) monthlyReturn(Ad(x)))