我正在尝试将脚本放入For-Loop中,以便计算多只股票的平均每日收益。
但是当我设置前循环时,出现以下错误:
getSymbols(Symbols = stock,src =“ yahoo”,from =“ 2005-01-01”,错误: 必须对多个符号请求使用auto.assign = TRUE
因此,我删除了env = NULL
并将其替换为auto.assign=TRUE
,但是当我运行脚本时,结果是NaN
。
任何建议将不胜感激。谢谢。
install.packages('quantmod')
library(quantmod)
stock <- c("AAPL")
{
stock.xts <- getSymbols(Symbols = stock, src = "yahoo",
from = "2005-01-01",
to = "2019-09-10",
env = NULL)
stock.xts <- as.data.frame(stock.xts)
N <- nrow(stock.xts)
todays.price <- stock.xts[2 : N,4]
yesterdays.price <- stock.xts[1 : N-1,4]
stock_dailyreturn <- (todays.price - yesterdays.price)/yesterdays.price
stock.xts$daily.return <- c(NA,(todays.price - yesterdays.price)/yesterdays.price)
print(mean(stock_dailyreturn))
}
使用前循环:
stock <- c("AAPL", "MSFT", "CRM", "ORCL", "NFLX", "GOOG")
for(i in 1:length(stock)){
stock.xts <- getSymbols(Symbols = stock, src = "yahoo",
from = "2005-01-01",
to = "2019-09-10",
auto.assign = TRUE)
stock.xts <- as.data.frame(stock.xts)
N <- nrow(stock.xts)
todays.price <- as.numeric(stock.xts[2 : N,4])
yesterdays.price <- as.numeric(stock.xts[1 : N-1,4])
stock_dailyreturn <- (todays.price - yesterdays.price)/yesterdays.price
stock.xts$daily.return <- c(NA,(todays.price - yesterdays.price)/yesterdays.price)
print(mean(stock.xts$daily.return))
}
答案 0 :(得分:0)
您的整个问题都在代码的最后一行mean(stock.xts$daily.return)
中,以及紧接其后的一行中。如果平均的向量包含mean
值NA
,则NaN
默认返回NA
或stock.xts$daily.return <- c(NA, ...
。可以通过在均值内设置na.rm = TRUE
来更改。
但是,可以进一步降低循环的复杂度。首先,您将在每次迭代中导入符号。这是不必要的。您也可以使用diff
和lag
函数,通过使用diff(...)/lag(...)
计算收益。
例如,您可以使用类似于以下示例的内容:
library("quantmod")
stock <- c("AAPL", "MSFT", "CRM", "ORCL", "NFLX", "GOOG")
#Create a place to store the symbols
stockenv <- new.env()
stuff <- getSymbols(Symbols = stock, src = "yahoo",
from = "2005-01-01",
to = "2019-09-10",
env = stockenv) #<=== note here, i import into the environment
ls(stockenv) #<== check that all the symbols were imported into the environemnt.
#[1] "AAPL" "CRM" "GOOG" "MSFT" "NFLX" "ORCL"
priceChange <- vector("list", length(ls(stockenv)))
meanPrice <- vector("list", length(ls(stockenv)))
#overwrite names
names(priceChange) <- names(meanPrice) <- ls(stockenv)
for(i in ls(stockenv)){
priceChange[[i]] <- diff(stockenv[[i]][,4]) / lag(stockenv[[i]][,4])
meanPrice[[i]] <- mean(priceChange[[i]], na.rm = TRUE)
}
simplify2array(meanPrice)
# AAPL CRM GOOG MSFT NFLX ORCL
#0.0012548717 0.0013114559 0.0008412977 0.0005783129 0.0019348552 0.0005236143
答案 1 :(得分:0)
您超级亲密! 尝试以下操作:
更改您的for循环以在每次迭代中保留单个值(请参见下面的for循环)
更新getSymbols函数的用法,使其正确自动分配(否则默认为NULL-请查阅文档以获取更多信息)
使用占位符矢量进行getSymbols函数创建对象(我现在称呼它为“ hello
祝你好运!
stock <- c("AAPL","MSFT","CRM")
hello <- c()
for(value in stock){
stock.xts <- getSymbols(Symbols = value, src = "yahoo",
from = "2005-01-01",
to = "2019-09-10",
env = hello,
auto.assign=TRUE)
stock.xts <- as.data.frame(stock.xts)
N <- nrow(stock.xts)
todays.price <- stock.xts[2 : N,4]
yesterdays.price <- stock.xts[1 : N-1,4]
stock_dailyreturn <- (todays.price - yesterdays.price)/yesterdays.price
stock.xts$daily.return <- c(NA,(todays.price - yesterdays.price)/yesterdays.price)
print(mean(stock_dailyreturn))
}