rperformancesimulationportfolioback-testing

R riskParityPortfolio / portfolioBacktest library backtesting fail


belows are example of riskParityPortfolio and portfolioBacktest library

the result of sample is empty. anyone know why?

I'm suspicious about data type of input list 'list(faang_data)'. but fail to find out

library(portfolioBacktest)
library(riskParityPortfolio)

# download price data
faang_data <- stockDataDownload(c("GOOG", "NFLX", "AAPL", "AMZN", "FB"),
                            from = "2014-01-01", to = "2019-06-25")

# define portfolios to be backtested
# risk parity portfolio
risk_parity <- function(dataset) {
  prices <- dataset$adjusted
  log_returns <- diff(log(prices))[-1]
  return(riskParityPortfolio(cov(log_returns))$w)
}


bt <- portfolioBacktest(list("risk parity portfolio" = risk_parity),
                    list(faang_data),
                    lookback= 12*20, 
                    optimize_every = 3*20, rebalance_every = 3*20)


# check performance summary
backtestSummary(bt)$performance
#>                   risk parity portfolio tangency portfolio
#> Sharpe ratio                  1.3800144          0.8787596
#> max drawdown                  0.3062046          0.3516856
#> annual return                 0.3117200          0.2324203
#> annual volatility             0.2258817          0.2644868
#> Sterling ratio                1.0180122          0.6608751
#> Omega ratio                   1.2710283          1.1793760
#> ROT (bps)                  8310.1199557        793.0188434

Solution

  • Looking at the example of the documentation https://rdrr.io/cran/portfolioBacktest/man/portfolioBacktest.html, you need to specify the ... as part of the parameter in your risk_parity function, to allow that function to take in additional arguments to be used internally by portfolioBacktest function.

    risk_parity <- function(dataset,...) {
        prices <- dataset$adjusted
        log_returns <- diff(log(prices))[-1]
        return(riskParityPortfolio(cov(log_returns))$w)
    }