R 中的投资组合回测包:“价格包含 NA”,尽管调整后的价格已填充非 NA 值

portfolioBacktest package in R: 'Prices contain NAs' despite that adjusted prices have been filled with non-NA values

提问人:Zju Xi 提问时间:11/9/2023 更新时间:11/9/2023 访问量:16

问:

你能看看我的代码(只需要几分钟)并指出问题吗?非常感谢。这是我的代码:

# download price data
data(SP500_symbols)
allStock <- stockDataDownload(stock_symbols = SP500_symbols, 
                    from = "2010-01-01", to = "2019-12-31")  
#tried to download all 505 stocks, 
#but some cannot be downloaded so around 450+ stocks
#some stocks were not listed so I have to fill in the NAs with the earlist value

#fill NAs in adjusted prices with the next nearest value 
allStock$adjusted <- na.locf(allStock$adjusted, fromLast = TRUE)   
colSums(is.na(allStock$adjusted)) # all 0, indicating no NAs
anyNA(allStock$adjusted) #FALSE, indicating no NAs

#the following 2 functions have no problems as I copied the code from 
#*Fast Design of Risk Parity Portfolios*

# define portfolios to be backtested
# risk parity portfolio
#this func returns a weight vector
risk_parity <- function(dataset, ...) {
  prices <- dataset$adjusted
  log_returns <- diff(log(prices))[-1]

  Sigma <- cov(log_returns)
  rpp <- riskParityPortfolio(Sigma)
  return(rpp$w)
}

# tangency portfolio (maximum sharpe ratio)
# this func returns a weight vector
library(quadprog)
max_sharpe_ratio <- function(dataset, ...) {
    prices <- dataset$adjusted
    log_returns <- diff(log(prices))[-1]
    
    N <- ncol(prices)
    Sigma <- cov(log_returns)
    mu <- colMeans(log_returns)
    if (all(mu <= 1e-8))
        return(rep(0, N))
    Dmat <- 2 * Sigma
    Amat <- diag(N)
    Amat <- cbind(mu, Amat)
    bvec <- c(1, rep(0, N))
    dvec <- rep(0, N)
    res <- solve.QP(Dmat = Dmat, dvec = dvec, Amat = Amat, bvec = bvec, meq = 1)
    w <- res$solution
    return(w/sum(w))
}

#when I run this, 'Error in (function (portfolio_fun, data, price_name, shortselling, leverage,  : prices contain NAs.'
bt <- portfolioBacktest(list("risk parity portfolio" = risk_parity,
                             "tangency portfolio" = max_sharpe_ratio),
                        list(allStock),
                        benchmarks = c("index","1/N"),
                        lookback = 252,
                        optimize_every = 3*20, #quarterly
                        rebalance_every = 3*20,
                        shortselling = TRUE,
                        execution = 'next period')

鉴于这个问题,我尝试仅从变量中提取两个列表,这是一个包含“打开”“高”的 7 个列表......“调整”和“索引”。这是我的代码:allStock

adjustedAndIndex <- list('adjusted' = allStock$adjusted, 'index' = allStock$index)

#run portfolioBacktest again
bt <- portfolioBacktest(list("risk parity portfolio" = risk_parity,
                             "tangency portfolio" = max_sharpe_ratio),
                        list(adjustedAndIndex),
                        benchmarks = c("index","1/N"),
                        lookback = 252,
                        optimize_every = 3*20, #quarterly
                        rebalance_every = 3*20,
                        shortselling = TRUE,
                        execution = 'next period')

而这一次还有另一个问题,当我运行时,我希望得到这样的结果:round(backtestSummary(bt)$performance, 4)

#                   risk parity portfolio tangency portfolio       1/N     index
#Sharpe ratio                      1.1955             1.1966    1.2032    0.7928
#max drawdown                      0.1851             0.2785    0.2167    0.1978
#annual return                     0.1830             0.2493    0.2032    0.1139
#annual volatility                 0.1531             0.2083    0.1689    0.1437
#Sortino ratio                     1.6932             1.7286    1.7058    1.1063
#downside deviation                0.1081             0.1442    0.1191    0.1030
#Sterling ratio                    0.9889             0.8951    0.9377    0.5758
#Omega ratio                       1.2353             1.2321    1.2357    1.1565
#VaR (0.95)                        0.0151             0.0206    0.0168    0.0150
#CVaR (0.95)                       0.0226             0.0306    0.0249    0.0226
#rebalancing period               59.5526            59.5526   59.5526 2263.0000
#turnover                          0.0015             0.0157    0.0013    0.0000
#ROT (bps)                      4671.1538           577.7948 5974.2958        NA
#cpu time                          0.0007             0.0007    0.0004    0.0010
#failure rate                      0.0000             0.0000    0.0000    0.0000

但我只得到

> round(backtestSummary(bt)$performance, 4)
                   risk parity portfolio tangency portfolio       1/N     index
Sharpe ratio                          NA                 NA    1.0864    0.7928
max drawdown                          NA                 NA    0.2015    0.1978
annual return                         NA                 NA    0.1558    0.1139
annual volatility                     NA                 NA    0.1434    0.1437
Sortino ratio                         NA                 NA    1.5283    1.1063
downside deviation                    NA                 NA    0.1019    0.1030
Sterling ratio                        NA                 NA    0.7733    0.5758
Omega ratio                           NA                 NA    1.2153    1.1565
VaR (0.95)                            NA                 NA    0.0147    0.0150
CVaR (0.95)                           NA                 NA    0.0222    0.0226
rebalancing period                    NA                 NA   59.5526 2263.0000
turnover                              NA                 NA    0.0013    0.0000
ROT (bps)                             NA                 NA 4718.1301        NA
cpu time                              NA                 NA    0.0005    0.0010
r 量化金融 回测 R-PortfolioAnalytics

评论


答: 暂无答案