Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative to error when periodicity() called on one observation #230

joshuaulrich opened this issue Mar 21, 2018 · 5 comments


Copy link

There have been a few instances where it has caused issues and/or confusion when periodicity() throws an error when called on an object with only one observation (e.g. #155, #161, rstudio/dygraphs#66, braverock/PerformanceAnalytics#92).

Is there a better alternative to an error? Perhaps return NA, with a warning? Or return an "empty" periodicity object, with a warning? An "empty" periodicity object could look something like what I recommended JJ implement for dygraphs:

list(difftime = structure(0, units = "secs", class = "difftime"),
  frequency = 0,
  start = start(data),
  end = end(data),
  units = "secs",
  scale = "seconds",
  label = "second"),
class = "periodicity")
Copy link

I think I'd prefer returning an empty periodicity argument.

Copy link

JJHerranzs commented Apr 28, 2018

Dear Joshua, I have just received the following error "Error in periodicity(R): can not calculate periodicity of 1 observation" and I am stuck. The point of this code is to run a backtest on the performance of an optimised portfolio. Is there any way you know how to go about this situation?

Here is my code:


# Get data
asset.names <- c("IUSA.L", "CUSS.L", "IWRD.L", "IEEM.L", "LQDE.L", "SHYU.L", "ITPS.L", "IWDP.L", "SGLN.L")
getSymbols(c("IUSA.L", "CUSS.L", "IWRD.L", "IEEM.L", "LQDE.L", "SHYU.L", "ITPS.L", "IWDP.L", "SGLN.L"), freq = "daily", from = "2013-01-01", to = "2017-12-31")
na.omit(c("IUSA.L", "CUSS.L", "IWRD.L", "IEEM.L", "LQDE.L", "SHYU.L", "ITPS.L", "IWDP.L", "SGLN.L"),cols=default, invert=FALSE)

# Assign to dataframe
# Get adjusted prices <- merge.zoo(IUSA.L[,6], CUSS.L[,6], IWRD.L[,6], IEEM.L[,6], LQDE.L[,6], ITPS.L[,6], SHYU.L[,6], IWDP.L[,6], SGLN.L[,6])

# Calculate returns <- CalculateReturns( <- na.omit(

# Set names
colnames( <- c("IUSA.L", "CUSS.L", "IWRD.L", "IEEM.L", "LQDE.L", "SHYU.L", "ITPS.L", "IWDP.L", "SGLN.L")
funds <- colnames(

# Create initial portfolio
init <- portfolio.spec(assets=funds)
init <- add.constraint(portfolio=init, type="full_investment")

# Create minimun variance portfolio
minvar.portf <- add.objective(portfolio=init, type="risk", name="var")

# Create mean-variance portfolio
meanvar.portf <- add.objective(portfolio=minvar.portf, type="return", name="mean")
opt_meanvar <- optimize.portfolio(, portfolio = meanvar.portf, optimize_method = "ROI")

backtesting <- optimize.portfolio.rebalancing(, portfolio = meanvar.portf, optimize_method = "ROI", rebalance_on="quarters", training_period = 60)


Copy link
Owner Author

joshuaulrich commented Apr 28, 2018

@JJHerranzs this is meant to be an issue and feature request tracker for xts, not a support forum. The contributing guide points to StackOverflow and the R-SIG-Finance as places to ask general questions about how to use the software.

The error is likely because one of your rebalancing periods only contains a single observation. But please take your question to another forum. I would prefer comments on this issue stay on topic.

Repository owner deleted a comment from JJHerranzs Apr 28, 2018
Copy link

I apologise for this and thank you very much for your answer, I am still a beginner in R so I am not sure how I am going to solve it but I will have a go!

Copy link

as @joshuaulrich pointed out, this isn't a support forum, but I'll give you a few other hints about where your problem might be. Your problem is most likely in how you are using PortfolioAnalytics.

You are specifying rebalancing on quarters. Typically you would do this using daily or weekly or monthly returns input data. See, for example


for a monthly return series.

You've further specified a training period of 60 periods (quarters). That would be 15 years of data before you generated your first portfolio and started rebalancing. Is this correct? Your first symbol: IUSA.L , looks as though it may only have been listed in 2002.

If you're still having problems, I suggest posting to Stack Overflow or R-SIG-Finance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

No branches or pull requests

3 participants