2

我收到“找不到回撤函数”错误。我已经加载了 PerformanceAnalytics 库。

代码:

install.packages("quantmod")
install.packages("PerformanceAnalytics")
require(quantmod)
require(PerformanceAnalytics)
getSymbols("AAPL")
AAPL.DF<-data.frame(Date=index(AAPL), coredata(AAPL[,1]))
AAPL.DF[,2]<-ROC(AAPL.DF[,2])
colnames(AAPL.DF)<-c("Date","rtn")
dailyRtn <- as.numeric(substring(AAPL.DF[,"rtn"],1,nchar(as.character(AAPL.DF[,"rtn"]))-1))
dailyDD <- as.vector(Drawdowns(dailyRtn/100))

下面是控制台窗口中的 o/p

> getSymbols("AAPL")
[1] "AAPL"
Warning message:
In download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m,  :
  downloaded length 109721 != reported length 200
> AAPL.DF<-data.frame(Date=index(AAPL), coredata(AAPL[,1]))
> AAPL.DF[,2]<-ROC(AAPL.DF[,2])
> colnames(AAPL.DF)<-c("Date","rtn")
> dailyRtn <- as.numeric(substring(AAPL.DF[,"rtn"],1,nchar(as.character(AAPL.DF[,"rtn"]))-1))
> dailyDD <- as.vector(Drawdowns(dailyRtn/100))
Error in as.vector(Drawdowns(dailyRtn/100)) : 
  could not find function "Drawdowns"

当我成功安装并加载 PerformanceAnalytics 库时,我不明白为什么我会收到无法找到 Drawdowns 函数的错误消息

4

1 回答 1

0

没有回撤功能。根据您所做的事情,您可能对使用 findDrawdowns 或 chart.Drawdown 感兴趣。您还可以稍微简化代码以计算回报,如下所示:

require(quantmod)
require(PerformanceAnalytics)
getSymbols("AAPL")
#  calculate returns based on Adjusted Close prices
AAPL.DF <- Return.calculate(AAPL[,6])[-1,]
dailyDD <- findDrawdowns(AAPL.DF)
chart.Drawdown(AAPL.DF)

我在原始代码中使用了 Adjusted_Close 价格(AAPL 列 6)而不是 Open 价格(AAPL 列 1),因为 Addjusted_Close 价格已针对 2014 年的 AAPL 拆分进行了更正,而 Open 价格未调整。

于 2015-01-15T12:35:20.293 回答