I think I've managed to do something similar to what the question asks.
If it is not the following, you may be able to adapt to your problem.
The trick is to divide the data by month, using the split
base function and the as.yearmon
function of the zoo
package. This package is also used to create a time series, the series z
, only with two columns of the tickers
database.
library(BatchGetSymbols)
library(PerformanceAnalytics)
library(zoo)
ibvsp <- BatchGetSymbols('^BVSP', first.date = as.Date('2012-01-01'),
last.date = as.Date('2017-12-31'))
tickers <- ibvsp$df.tickers[complete.cases(ibvsp$df.tickers), ]
z <- zoo(tickers[9:10], order.by = tickers[, 7])
z_month <- split(z, as.yearmon(index(z)))
cvar <- as.data.frame(t(sapply(z_month, CVaR)))
names(cvar) <- names(tickers[9:10])
head(cvar)
# ret.adjusted.prices ret.closing.prices
#jan 2012 -0.01410941 -0.01410941
#fev 2012 -0.02108851 -0.02108851
#mar 2012 -0.02402775 -0.02402775
#abr 2012 -0.02146428 -0.02146428
#mai 2012 -0.03548744 -0.03548744
#jun 2012 -0.03252744 -0.03252744