我使用quantmod
-package从雅虎下载了调整后的收盘价,并用它创建了一个由50% AAPL
和50% FB
-stocks组成的投资组合。
当我绘制我的投资组合的累积绩效时,我得到的绩效(可疑地)很高,因为它超过了100%:
library(ggplot2)
library(quantmod)
cmp <- "AAPL"
getSymbols(Symbols = cmp)
tail(AAPL$AAPL.Adjusted)
cmp <- "FB"
getSymbols(Symbols = cmp)
tail(FB$FB.Adjusted)
df <- data.frame("AAPL" = tail(AAPL$AAPL.Adjusted, 1000),
"FB" = tail(FB$FB.Adjusted, 1000))
for(i in 2:nrow(df)){
df$AAPL.Adjusted_prc[i] <- df$AAPL.Adjusted[i]/df$AAPL.Adjusted[i-1]-1
df$FB.Adjusted_prc[i] <- df$FB.Adjusted[i]/df$FB.Adjusted[i-1]-1
}
df <- df[-1,]
df$portfolio <- (df$AAPL.Adjusted_prc + df$FB.Adjusted_prc)*0.5
df$performance <- cumprod(df$portfolio+1)-1
df$idu <- as.Date(row.names(df))
ggplot(data = df, aes(x = idu, y = performance)) + geom_line()
超过100%的累积性能在我看来是非常不现实的。这让我想到,也许有必要在使用之前调整/缩放从quantmod
下载的数据?
发布于 2019-03-04 11:22:05
我一直在为这个问题而苦苦挣扎,我有一种感觉,在它的背后有一个数据问题。为了演示这一点,我使用两种方法计算累积回报。结果显示了一些我不能真正解释的差异-因此,你可以先看看这些。
首先,我运行你的代码:
library(quantmod)
library(tidyverse
cmp <- "AAPL"
getSymbols(Symbols = cmp)
tail(AAPL$AAPL.Adjusted)
cmp <- "FB"
getSymbols(Symbols = cmp)
tail(FB$FB.Adjusted)
df <- data.frame("AAPL" = tail(AAPL$AAPL.Adjusted, 1000),
"FB" = tail(FB$FB.Adjusted, 1000))
for(i in 2:nrow(df)){
df$AAPL.Adjusted_prc[i] <- df$AAPL.Adjusted[i]/df$AAPL.Adjusted[i-1]-1
df$FB.Adjusted_prc[i] <- df$FB.Adjusted[i]/df$FB.Adjusted[i-1]-1
}
然后,我手动计算累计收益,方法是将当前值除以起始值(即第1行的价格),然后减1。此外,我还分别对两只股票的收益进行cumsum
。
df$aapl_man <- df$AAPL.Adjusted / df$AAPL.Adjusted[1] - 1
df$fb_man <- df$FB.Adjusted / df$FB.Adjusted[1] - 1
df <- df[-1,]
df$portfolio <- (df$AAPL.Adjusted_prc + df$FB.Adjusted_prc)*0.5
df$performance <- cumprod(df$portfolio+1)-1
df$idu <- as.Date(row.names(df))
df <- mutate_at(df, vars(contains("_prc")), cumsum)
现在,我正在绘制cumsum
回报(蓝色)和手动计算的回报(红色)。
df %>%
ggplot(aes(x = idu)) +
geom_line(aes(y = AAPL.Adjusted_prc), colour = "blue") +
geom_line(aes(y = aapl_man), colour = "red") +
ggtitle("Apple")
df %>%
ggplot(aes(x = idu)) +
geom_line(aes(y = FB.Adjusted_prc), colour = "blue") +
geom_line(aes(y = fb_man), colour = "red") +
ggtitle("Facebook")
特别是对于Facebook,我们看到了这两种方法之间的相当大的差异。很抱歉,我无法解决您的问题,但我希望这将引导您找到解决方案。
https://stackoverflow.com/questions/54979942
复制